Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Profile
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 2
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
110
 perform
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
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    2013-05-26
13 */
14
15declare(strict_types=1);
16
17namespace Karla\Action;
18
19use Karla\Query;
20use Karla\Action;
21use Karla\PathValidator;
22
23/**
24 * Class for handling profile action
25 *
26 * @category Utility
27 * @author   Johannes Skov Frandsen <jsf@greenoak.dk>
28 * @license  http://www.opensource.org/licenses/mit-license.php MIT
29 * @link     https://github.com/localgod/karla Karla
30 */
31class Profile implements Action
32{
33    /**
34     * Profile path
35     *
36     * @var string
37     */
38    private string $profilePath;
39
40    /**
41     * Profile name
42     *
43     * @var string
44     */
45    private string $profileName;
46
47    /**
48     * Remove profile
49     *
50     * @var bool
51     */
52    private bool $remove;
53
54    /**
55     * Construct a new profile action
56     *
57     * @param string $profilePath Profile path
58     * @param string $profileName Profile name
59     * @param bool $remove Should the profile be removed? (default is false)
60     *
61     * @throws \LogicException profilePath or profileName must be set, but not both.
62     * @throws \InvalidArgumentException If profile input file (' . $profilePath . ') could not be found.
63     */
64    public function __construct(string $profilePath = "", string $profileName = "", $remove = false)
65    {
66        if (($profilePath == '' && $profileName == '') || ($profilePath != '' && $profileName != '')) {
67            $message = 'profilePath or profileName must be set, but not both.';
68            throw new \LogicException($message);
69        }
70        if ($profilePath != '' && str_contains($profilePath, "\0")) {
71            throw new \InvalidArgumentException('Path contains null bytes');
72        }
73        if ($profilePath != '' && ! file_exists($profilePath)) {
74            $message = 'Profile input file (' . $profilePath . ') could not be found';
75            throw new \InvalidArgumentException($message);
76        }
77        if ($profilePath !== '') {
78            $profilePath = PathValidator::validatePath($profilePath);
79        }
80
81        $this->profilePath = $profilePath;
82        $this->profileName = $profileName;
83        $this->remove = $remove;
84    }
85
86    /**
87     * (non-PHPdoc)
88     *
89     * @param Query $query The query to add the action to
90     *
91     * @see Action::perform()
92     */
93    public function perform(Query $query): Query
94    {
95        if ($this->profilePath != '') {
96            $option = ' ' . ($this->remove ? '+' : '-') . 'profile ' . escapeshellarg($this->profilePath) . ' ';
97            $query->setOutputOption($option);
98        } else {
99            $option = ' ' . ($this->remove ? '+' : '-') . 'profile ' . $this->profileName;
100            $query->setOutputOption($option);
101        }
102
103        return $query;
104    }
105}