Passed
Push — master ( 913299...dd4b11 )
by Tom
04:05
created

StatusException::ok()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Utility;
6
7
use Exception;
8
9
/**
10
 * Signal utility exit status, optionally with an (error) message
11
 */
12
class StatusException extends Exception
13
{
14
    /**
15
     * fatal with message and non-zero (1)
16
     *
17
     * @param string $message
18
     * @param int $code
19
     * @param null|Exception $previous
20
     *
21
     * @throws StatusException
22
     */
23 2
    public static function fatal($message = '', $code = 1, Exception $previous = null)
24
    {
25 2
        if ('' === $message) {
26 1
            $message = 'fatal abort';
27
        }
28
29 2
        throw new self($message, $code, $previous);
30
    }
31
32
    /**
33
     * ok (success, zero)
34
     *
35
     * @param null|Exception $previous
36
     *
37
     * @throws StatusException
38
     */
39 1
    public static function ok(Exception $previous = null)
40
    {
41 1
        throw new self('', 0, $previous);
42
    }
43
44
    /**
45
     * StatusException constructor.
46
     *
47
     * @param string $message
48
     * @param int|string $code
49
     * @param null|Exception $previous
50
     */
51 9
    public function __construct($message = '', $code = 0, Exception $previous = null)
52
    {
53 9
        if (!is_int($code) || $code < 0 || $code > 255) {
54 2
            throw new \InvalidArgumentException(sprintf(
55 2
                'Code must be integer in range from 0 to 255, %s given',
56 2
                var_export($code, true)
57
            ));
58
        }
59
60 7
        parent::__construct($message, $code, $previous);
61 7
    }
62
}
63