Completed
Push — master ( 0276fe...fe00d6 )
by Sébastien
02:48
created

ProcessConversionException::getProcess()   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
    /** @var Process */
18
    private $process;
19
20
    /** @var string */
21
    private $failureType;
22
23 2
    public function __construct(Process $process, SPException\RuntimeException $previousException)
24
    {
25 2
        if ($previousException instanceof SPException\ProcessFailedException) {
26 2
            $code = $previousException->getProcess()->getExitCode();
27 2
            $type = self::FAILURE_TYPE_PROCESS;
28
        } elseif ($previousException instanceof SPException\ProcessTimedOutException) {
29
            $code = $previousException->getProcess()->getExitCode();
30
            $type = self::FAILURE_TYPE_SIGNAL;
31
        } elseif ($previousException instanceof SPException\ProcessSignaledException) {
32
            $type = self::FAILURE_TYPE_TIMEOUT;
33
            $code = $previousException->getProcess()->getExitCode();
34
        } else {
35
            $code = 1;
36
            $type = self::FAILURE_TYPE_RUNTIME;
37
        }
38
39 2
        parent::__construct(
40 2
            $previousException->getMessage(),
41 2
            $code,
42 2
            $previousException
43
        );
44
45 2
        $this->process     = $process;
46 2
        $this->failureType = $type;
47 2
    }
48
49
    /**
50
     * Return symfony process object.
51
     */
52 1
    public function getProcess(): Process
53
    {
54 1
        return $this->process;
55
    }
56
57 1
    public function wasCausedByProcess(): bool
58
    {
59 1
        return $this->failureType === self::FAILURE_TYPE_PROCESS;
60
    }
61
62
    public function wasCausedBySignal(): bool
63
    {
64
        return $this->failureType === self::FAILURE_TYPE_SIGNAL;
65
    }
66
67
    public function wasCausedByTimeout(): bool
68
    {
69
        return $this->failureType === self::FAILURE_TYPE_TIMEOUT;
70
    }
71
72
    /**
73
     * Whether the failure is due to 'PROCESS', 'TIMEOUT', 'SIGNAL' or (generic) 'RUNTIME' exception.
74
     */
75 1
    public function getFailureType(): string
76
    {
77 1
        return $this->failureType;
78
    }
79
80
    /**
81
     * @return SPException\RuntimeException|SPException\ProcessFailedException|SPException\ProcessSignaledException|SPException\ProcessTimedOutException
82
     */
83
    public function getSymfonyProcessRuntimeException(): SPException\RuntimeException
84
    {
85
        /**
86
         * @var SPException\RuntimeException
87
         */
88
        $previous = $this->getPrevious();
89
90
        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...
91
    }
92
}
93