Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
CommandResult
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isSuccess
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOutputOrThrow
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 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;
18
19/**
20 * Represents the result of an executed ImageMagick command
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 CommandResult
28{
29    /**
30     * Create a new CommandResult
31     *
32     * @param string $output   Standard output from the command
33     * @param string $error    Standard error output from the command
34     * @param int    $exitCode Process exit code (0 = success)
35     */
36    public function __construct(
37        public readonly string $output,
38        public readonly string $error,
39        public readonly int $exitCode
40    ) {
41    }
42
43    /**
44     * Check whether the command succeeded
45     */
46    public function isSuccess(): bool
47    {
48        return $this->exitCode === 0;
49    }
50
51    /**
52     * Return the output or throw a RuntimeException on failure
53     *
54     * @throws \RuntimeException When the command exited with a non-zero code
55     */
56    public function getOutputOrThrow(): string
57    {
58        if (!$this->isSuccess()) {
59            throw new \RuntimeException(
60                "ImageMagick command failed (exit code {$this->exitCode}): {$this->error}"
61            );
62        }
63        return $this->output;
64    }
65}