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