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