Completed
Push — master ( f72769...eb1d3e )
by Sébastien
08:28 queued 06:05
created

ProcessException   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

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

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
/**
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 17
    public function __construct(Process $process, SPException\RuntimeException $previousException, ?string $message = null)
26
    {
27 17
        if ($previousException instanceof SPException\ProcessFailedException ||
28 1
            $previousException instanceof SPException\ProcessTimedOutException ||
29 17
            $previousException instanceof SPException\ProcessSignaledException
30
        ) {
31 17
            $code = $previousException->getProcess()->getExitCode();
32
        } else {
33
            $code = 1;
34
        }
35
36 17
        $msg = $message ?? $previousException->getMessage();
37
38 17
        parent::__construct(
39 17
            $msg,
40 17
            $code ?? 1,
41 17
            $previousException
42
        );
43
44 17
        $this->process = $process;
45 17
    }
46
47
    /**
48
     * Return symfony process object.
49
     */
50 1
    public function getProcess(): Process
51
    {
52 1
        return $this->process;
53
    }
54
55 1
    public function getErrorOutput(): string
56
    {
57 1
        return $this->process->getErrorOutput();
58
    }
59
60
    /**
61
     * @return SPException\RuntimeException|SPException\ProcessFailedException|SPException\ProcessSignaledException|SPException\ProcessTimedOutException
62
     */
63 1
    public function getSymfonyProcessRuntimeException(): SPException\RuntimeException
64
    {
65
        /**
66
         * @var SPException\RuntimeException $previous
67
         */
68 1
        $previous = $this->getPrevious();
69
70 1
        return $previous;
71
    }
72
}
73