Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Identify
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 5
240
0.00% covered (danger)
0.00%
0 / 1
 in
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 execute
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
56
 verbose
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getCommand
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 raw
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Karla ImageMagick wrapper library
5 *
6 * PHP Version 8.0<
7 *
8 * @category Utility
9 * @author   Johannes Skov Frandsen <jsf@greenoak.dk>
10 * @license  http://www.opensource.org/licenses/mit-license.php MIT
11 * @link     https://github.com/localgod/karla Karla
12 * @since    2012-04-05
13 */
14
15declare(strict_types=1);
16
17namespace Karla\Program;
18
19use Karla\PathValidator;
20
21/**
22 * Class for wrapping ImageMagicks identify tool
23 *
24 * @category Utility
25 * @author   Johannes Skov Frandsen <jsf@greenoak.dk>
26 * @license  http://www.opensource.org/licenses/mit-license.php MIT
27 * @link     https://github.com/localgod/karla Karla
28 */
29class Identify extends ImageMagick
30{
31    /**
32     * Input file
33     *
34     * @var string
35     */
36    protected string $inputFile;
37
38    /**
39     * Add input argument
40     *
41     * @param string $filePath Input file path
42     *
43     * @throws \InvalidArgumentException
44     */
45    public function in(string $filePath): self
46    {
47        if (str_contains($filePath, "\0")) {
48            throw new \InvalidArgumentException('Path contains null bytes');
49        }
50        if (! file_exists($filePath)) {
51            $message = 'The input file path (' . $filePath . ') is invalid or the file could not be located.';
52            throw new \InvalidArgumentException($message);
53        }
54        $filePath = PathValidator::validatePath($filePath);
55        $file = new \SplFileObject($filePath);
56        if ($file->isReadable()) {
57            $this->inputFile = escapeshellarg($file->getPathname());
58        }
59
60        return $this;
61    }
62
63    /**
64     * Execute the command
65     *
66     * @param bool $reset Reset the query
67     * @param bool $raw Get the raw output
68     *
69     * @see Imagemagick#execute()
70     */
71    public function execute(bool $reset = true, bool $raw = true): string|object
72    {
73        $result = parent::execute(false);
74        // Ensure result is a string
75        $resultStr = is_string($result) ? $result : '';
76
77        if (! $raw) {
78            if ($this->getQuery()->isOptionSet('verbose', $this->getQuery()->getInputOptions())) {
79                $reset ? $this->getQuery()->reset() : null;
80                return new \Karla\MetaData($resultStr, true);
81            }
82            $reset ? $this->getQuery()->reset() : null;
83            return new \Karla\MetaData($resultStr);
84        }
85
86        $reset ? $this->getQuery()->reset() : null;
87        return trim($resultStr);
88    }
89
90    /**
91     * Add verbose argument
92     */
93    public function verbose(): self
94    {
95        $this->getQuery()->notWith('verbose', \Karla\Query::ARGUMENT_TYPE_INPUT);
96        $this->getQuery()->setInputOption("-verbose ");
97
98        return $this;
99    }
100
101    /**
102     * (non-PHPdoc)
103     *
104     * @see Imagemagick#getCommand()
105     */
106    public function getCommand(): string
107    {
108        $options = $this->getQuery()->prepareOptions($this->getQuery()->getInputOptions());
109
110        return parent::getCommand() . ' ' . ($options == '' ? '' : $options . ' ') . $this->inputFile;
111    }
112
113    /**
114     * Raw arguments directly to ImageMagick
115     *
116     * @param string $arguments Arguments
117     * @param bool $input Defaults to an input option, use false to use it as an output option
118     *
119     * @see ImageMagick::raw()
120     */
121    public function raw(string $arguments, bool $input = true): self
122    {
123        parent::raw($arguments, $input);
124
125        return $this;
126    }
127}