Failed Conditions
Push — master ( c8847f...26090e )
by Sébastien
03:53
created

ProcessConversionException::wasCausedByProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Exception;
6
7
use Symfony\Component\Process\Exception as SPException;
8
use Symfony\Component\Process\Process;
9
10
class ProcessConversionException extends RuntimeException implements ProcessExceptionInterface
11
{
12
    public const FAILURE_TYPE_PROCESS = 'PROCESS';
13
    public const FAILURE_TYPE_TIMEOUT = 'TIMEOUT';
14
    public const FAILURE_TYPE_SIGNAL  = 'SIGNAL';
15
    public const FAILURE_TYPE_RUNTIME = 'RUNTIME';
16
17
18
19
    /** @var Process */
20
    private $process;
21
22
    /**
23
     * @var string
24
     */
25
    private $failureType;
26
27 2
    public function __construct(Process $process, SPException\RuntimeException $previousException)
28
    {
29 2
        if ($previousException instanceof SPException\ProcessFailedException) {
30 2
            $code = $previousException->getProcess()->getExitCode();
31 2
            $type = self::FAILURE_TYPE_PROCESS;
32
        } elseif ($previousException instanceof SPException\ProcessTimedOutException) {
33
            $code = $previousException->getProcess()->getExitCode();
34
            $type = self::FAILURE_TYPE_SIGNAL;
35
        } elseif ($previousException instanceof SPException\ProcessSignaledException) {
36
            $type = self::FAILURE_TYPE_TIMEOUT;
37
            $code = $previousException->getProcess()->getExitCode();
38
        } else {
39
            $code = 1;
40
            $type = self::FAILURE_TYPE_RUNTIME;
41
        }
42
43 2
        parent::__construct(
44 2
            $previousException->getMessage(),
45 2
            $code,
46 2
            $previousException
47
        );
48
49 2
        $this->process = $process;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
50 2
        $this->failureType = $type;
51 2
    }
52
53
    /**
54
     * Return symfony process object.
55
     */
56
    public function getProcess(): Process
57
    {
58
        return $this->process;
59
    }
60
61 1
    public function wasCausedByProcess(): bool
62
    {
63 1
        return $this->failureType === self::FAILURE_TYPE_PROCESS;
64
    }
65
66
    public function wasCausedBySignal(): bool
67
    {
68
        return $this->failureType === self::FAILURE_TYPE_SIGNAL;
69
    }
70
71
    public function wasCausedByTimeout(): bool
72
    {
73
        return $this->failureType === self::FAILURE_TYPE_TIMEOUT;
74
    }
75
76
    /**
77
     * Whether the failure is due to 'PROCESS', 'TIMEOUT', 'SIGNAL' or (generic) 'RUNTIME' exception
78
     */
79
    public function getFailureType(): string
80
    {
81
        return $this->failureType;
82
    }
83
84
    /**
85
     * @return SPException\RuntimeException|SPException\ProcessFailedException|SPException\ProcessSignaledException|SPException\ProcessTimedOutException
86
     */
87
    public function getSymfonyProcessRuntimeException(): SPException\RuntimeException
88
    {
89
        /**
90
         * @var SPException\RuntimeException
91
         */
92
        $previous = $this->getPrevious();
93
94
        return $previous;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $previous returns the type Exception which includes types incompatible with the type-hinted return Symfony\Component\Proces...eption\RuntimeException.
Loading history...
95
    }
96
}
97