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

EnforcementTrait::enforce()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Dgame\Ensurance\Enforcement;
4
5
use Throwable;
6
7
/**
8
 * Trait EnforcementTrait
9
 * @package Dgame\Ensurance\Enforcement
10
 */
11
trait EnforcementTrait
12
{
13
    /**
14
     * @var Enforcement|null
15
     */
16
    private $enforcement;
17
18
    /**
19
     * @param string|Throwable $throwable
20
     * @param array            ...$args
21
     *
22
     * @return $this
23
     */
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $args a bit more specific; maybe use array[].
Loading history...
24
    final public function orThrow($throwable, ...$args)
25
    {
26
        if ($this->hasEnforcement()) {
27
            $this->enforcement->orThrow($throwable, ...$args);
28
        }
29
30
        return $this;
31
    }
32
33
    /**
34
     * @return bool
35
     */
36
    final public function verify(): bool
37
    {
38
        if (!$this->isEnforcementFulfilled()) {
39
            $this->approveEnforcement();
40
41
            return false;
42
        }
43
44
        return true;
45
    }
46
47
    /**
48
     * @param bool $condition
49
     *
50
     * @return $this
51
     */
52
    final protected function enforce(bool $condition)
53
    {
54
        $this->enforcement = new Enforcement($condition);
55
56
        return $this;
57
    }
58
59
    /**
60
     *
61
     */
62
    private function approveEnforcement()
63
    {
64
        if ($this->hasEnforcement()) {
65
            $this->enforcement->approve();
66
        }
67
    }
68
69
    /**
70
     * @return bool
71
     */
72
    final protected function isEnforcementFulfilled(): bool
73
    {
74
        return $this->hasEnforcement() && $this->enforcement->isFulfilled();
75
    }
76
77
    /**
78
     * @return bool
79
     */
80
    private function hasEnforcement(): bool
81
    {
82
        return $this->enforcement !== null;
83
    }
84
}