Failed Conditions
Push — master ( 65f0dd...191693 )
by Sébastien
02:09
created

ProcessException::__construct()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7.0071

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 30
ccs 18
cts 19
cp 0.9474
rs 8.8333
c 0
b 0
f 0
cc 7
nc 6
nop 3
crap 7.0071
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @see       https://github.com/soluble-io/soluble-mediatools for the canonical repository
7
 *
8
 * @copyright Copyright (c) 2018-2019 Sébastien Vanvelthem. (https://github.com/belgattitude)
9
 * @license   https://github.com/soluble-io/soluble-mediatools/blob/master/LICENSE.md MIT
10
 */
11
12
namespace Soluble\MediaTools\Common\Exception;
13
14
use Symfony\Component\Process\Exception as SPException;
15
use Symfony\Component\Process\Process;
16
17
class ProcessException extends RuntimeException implements ProcessExceptionInterface
18
{
19
    /** @var Process */
20
    protected $process;
21
22
    /**
23
     * @param string|null $message if not set will use the previousException message
24
     */
25 16
    public function __construct(Process $process, SPException\RuntimeException $previousException, ?string $message = null)
26
    {
27 16
        if ($previousException instanceof SPException\ProcessFailedException ||
28 5
            $previousException instanceof SPException\ProcessTimedOutException ||
29 16
            $previousException instanceof SPException\ProcessSignaledException
30
        ) {
31 16
            $code = $previousException->getProcess()->getExitCode();
32
        } else {
33
            $code = 1;
34
        }
35
36 16
        if ($message === null) {
37 16
            $errOutput = $process->isStarted() ? trim($process->getErrorOutput()) : '';
38
39 16
            $message = sprintf(
40 16
                '%s, exit %s: %s (%s)',
41 16
                $process->getExitCodeText(),
42 16
                $process->getExitCode(),
43 16
                $process->getCommandLine(),
44 16
                $errOutput !== '' ? $errOutput : $previousException->getMessage()
45
            );
46
        }
47
48 16
        parent::__construct(
49 16
            $message,
50 16
            $code ?? 1,
51 16
            $previousException
52
        );
53
54 16
        $this->process = $process;
55 16
    }
56
57
    /**
58
     * Return symfony process object.
59
     */
60 1
    public function getProcess(): Process
61
    {
62 1
        return $this->process;
63
    }
64
65 1
    public function getErrorOutput(): string
66
    {
67 1
        return $this->process->getErrorOutput();
68
    }
69
70
    /**
71
     * @return SPException\RuntimeException|SPException\ProcessFailedException|SPException\ProcessSignaledException|SPException\ProcessTimedOutException
72
     */
73 1
    public function getSymfonyProcessRuntimeException(): SPException\RuntimeException
74
    {
75
        /**
76
         * @var SPException\RuntimeException $previous
77
         */
78 1
        $previous = $this->getPrevious();
79
80 1
        return $previous;
81
    }
82
}
83