Completed
Push — master ( 60648a...356503 )
by Randy
01:14
created

Enforcement   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 116
rs 10
c 0
b 0
f 0
wmc 15
lcom 2
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A __destruct() 0 6 2
A approve() 0 4 1
A isFulfilled() 0 4 1
A hasThrowable() 0 4 1
A getThrowable() 0 4 1
A emplaceThrowable() 0 4 1
A orThrow() 0 8 2
A setThrowMessage() 0 13 3
A setThrowable() 0 4 1
1
<?php
2
3
namespace Dgame\Ensurance\Enforcement;
4
5
use AssertionError;
6
use Dgame\Ensurance\Exception\EnsuranceException;
7
use Throwable;
8
9
/**
10
 * Class Enforcement
11
 * @package Dgame\Ensurance\Enforcement
12
 */
13
final class Enforcement
14
{
15
    /**
16
     * @var bool
17
     */
18
    private $condition;
19
    /**
20
     * @var null|Throwable
21
     */
22
    private $throwable;
23
24
    /**
25
     * Enforcement constructor.
26
     *
27
     * @param bool        $condition
28
     * @param string|null $message
29
     */
30
    public function __construct(bool $condition, string $message = null)
31
    {
32
        $this->condition = $condition;
33
        if ($message !== null) {
34
            $this->setThrowMessage($message);
35
        }
36
    }
37
38
    /**
39
     * @throws Throwable
40
     */
41
    public function __destruct()
42
    {
43
        if (!$this->isFulfilled()) {
44
            throw $this->getThrowable();
45
        }
46
    }
47
48
    /**
49
     * Enforce approvement of the Enforcement
50
     */
51
    final public function approve()
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
52
    {
53
        $this->condition = true;
54
    }
55
56
    /**
57
     * @return bool
58
     */
59
    final public function isFulfilled(): bool
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
60
    {
61
        return $this->condition;
62
    }
63
64
    /**
65
     * @return bool
66
     */
67
    final public function hasThrowable(): bool
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
68
    {
69
        return $this->throwable !== null;
70
    }
71
72
    /**
73
     * @return Throwable
74
     */
75
    private function getThrowable(): Throwable
76
    {
77
        return $this->throwable ?? $this->emplaceThrowable('Assertion failed');
78
    }
79
80
    /**
81
     * @param string $message
82
     *
83
     * @return Throwable
84
     */
85
    private function emplaceThrowable(string $message): Throwable
86
    {
87
        return new EnsuranceException($message);
88
    }
89
90
    /**
91
     * @param string|Throwable $throwable
92
     * @param array            ...$args
93
     */
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $args a bit more specific; maybe use array[].
Loading history...
94
    final public function orThrow($throwable, ...$args)
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
95
    {
96
        if ($throwable instanceof Throwable) {
97
            $this->setThrowable($throwable);
98
        } else {
99
            $this->setThrowMessage((string) $throwable, ...$args);
100
        }
101
    }
102
103
    /**
104
     * @param string $message
105
     * @param array  ...$args
106
     */
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $args a bit more specific; maybe use array[].
Loading history...
107
    final public function setThrowMessage(string $message, ...$args)
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
108
    {
109
        if (!empty($args)) {
110
            $args    = array_map(function ($arg) {
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $args. This often makes code more readable.
Loading history...
111
                return !is_string($arg) ? var_export($arg, true) : $arg;
112
            }, $args);
113
            $message = sprintf($message, ...$args);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
114
        }
115
116
        $throwable = $this->emplaceThrowable($message);
117
118
        $this->setThrowable($throwable);
119
    }
120
121
    /**
122
     * @param Throwable $throwable
123
     */
124
    final public function setThrowable(Throwable $throwable)
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
125
    {
126
        $this->throwable = $throwable;
127
    }
128
}