RuntimeException   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 26 2
A getExitCodeText() 0 4 1
A getHostname() 0 4 1
A getCommand() 0 4 1
A getExitCode() 0 4 1
A getOutput() 0 4 1
A getErrorOutput() 0 4 1
1
<?php declare(strict_types=1);
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Exception;
9
10
use Symfony\Component\Process\Process;
11
12
/**
13
 * @codeCoverageIgnore
14
 */
15
class RuntimeException extends Exception
16
{
17
    /**
18
     * @var string
19
     */
20
    private $hostname;
21
22
    /**
23
     * @var string
24
     */
25
    private $command;
26
27
    /**
28
     * @var int
29
     */
30
    private $exitCode;
31
32
    /**
33
     * @var string
34
     */
35
    private $output;
36
37
    /**
38
     * @var string
39
     */
40
    private $errorOutput;
41
42
    public function __construct(
43
        string $hostname,
44
        string $command,
45
        int $exitCode,
46
        string $output,
47
        string $errorOutput
48
    ) {
49
        $this->hostname = $hostname;
50
        $this->command = $command;
51
        $this->exitCode = $exitCode;
52
        $this->output = $output;
53
        $this->errorOutput = $errorOutput;
54
55
        $message = sprintf(
56
            'The command "%s" failed.'.
57
            "\n\nExit Code: %d (%s)\n\nHost Name: %s".
58
            "\n\n================\n%s",
59
            $command,
60
            $exitCode,
61
            $this->getExitCodeText($exitCode),
62
            $hostname,
63
            strlen(trim($errorOutput)) !== 0 ? $errorOutput : $output
64
        );
65
66
        parent::__construct($message, $exitCode);
67
    }
68
69
    private function getExitCodeText(int $exitCode): string
70
    {
71
        return Process::$exitCodes[$exitCode] ?? 'Unknown error';
72
    }
73
74
    public function getHostname(): string
75
    {
76
        return $this->hostname;
77
    }
78
79
    public function getCommand(): string
80
    {
81
        return $this->command;
82
    }
83
84
    public function getExitCode(): int
85
    {
86
        return $this->exitCode;
87
    }
88
89
    public function getOutput(): string
90
    {
91
        return $this->output;
92
    }
93
94
    public function getErrorOutput(): string
95
    {
96
        return $this->errorOutput;
97
    }
98
}
99