Passed
Push — master ( d0bcc1...2a69c8 )
by Sébastien
02:28
created

ProcessException   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 7
eloc 18
dl 0
loc 54
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrorOutput() 0 3 1
A getProcess() 0 3 1
A getSymfonyProcessRuntimeException() 0 8 1
A __construct() 0 20 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Common\Exception;
6
7
use Symfony\Component\Process\Exception as SPException;
8
use Symfony\Component\Process\Process;
9
10
class ProcessException extends RuntimeException implements ProcessExceptionInterface
11
{
12
    /** @var Process */
13
    protected $process;
14
15
    /**
16
     * @param string|null $message if not set will use the previousException message
17
     */
18 12
    public function __construct(Process $process, SPException\RuntimeException $previousException, ?string $message = null)
19
    {
20 12
        if ($previousException instanceof SPException\ProcessFailedException ||
21 5
            $previousException instanceof SPException\ProcessTimedOutException ||
22 12
            $previousException instanceof SPException\ProcessSignaledException
23
        ) {
24 12
            $code = $previousException->getProcess()->getExitCode();
25
        } else {
26
            $code = 1;
27
        }
28
29 12
        $msg = $message ?? $previousException->getMessage();
30
31 12
        parent::__construct(
32 12
            $msg,
33 12
            $code ?? 1,
34
            $previousException
35
        );
36
37 12
        $this->process = $process;
38 12
    }
39
40
    /**
41
     * Return symfony process object.
42
     */
43 1
    public function getProcess(): Process
44
    {
45 1
        return $this->process;
46
    }
47
48 1
    public function getErrorOutput(): string
49
    {
50 1
        return $this->process->getErrorOutput();
51
    }
52
53
    /**
54
     * @return SPException\RuntimeException|SPException\ProcessFailedException|SPException\ProcessSignaledException|SPException\ProcessTimedOutException
55
     */
56 1
    public function getSymfonyProcessRuntimeException(): SPException\RuntimeException
57
    {
58
        /**
59
         * @var SPException\RuntimeException $previous
60
         */
61 1
        $previous = $this->getPrevious();
62
63 1
        return $previous;
64
    }
65
}
66