|
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; |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
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
will produce issues in the first and second line, while this second example
will produce no issues.