Judge   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 118
c 0
b 0
f 0
wmc 21
lcom 1
cbo 1
ccs 48
cts 48
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C decideRules() 0 30 11
D decideRule() 0 42 9
1
<?php
2
3
namespace Jlis\Judge\Judges;
4
5
use Jlis\Judge\Contracts\VoterInterface;
6
use Jlis\Judge\Adapters\AdapterInterface;
7
8
/**
9
 * @author Julius Ehrlich <[email protected]>
10
 */
11
class Judge
12
{
13
    const RULE_PARAM_DELIMITER = ':';
14
    const RULE_INVERTER = '!';
15
    const FIELD_VALUE = 'value';
16
    const FIELD_FILTERS = 'filters';
17
18
    /**
19
     * @var AdapterInterface
20
     */
21
    protected $adapter;
22
    /**
23
     * @var VoterInterface[]
24
     */
25
    protected $voters = [];
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param AdapterInterface $adapter
31
     * @param VoterInterface[] $voters
32
     */
33 59
    public function __construct(AdapterInterface $adapter, array $voters = [])
34
    {
35 59
        $this->adapter = $adapter;
36 59
        $this->voters = $voters;
37 59
    }
38
39
    /**
40
     * Decides multiple rules.
41
     *
42
     * @param string|array $rules
43
     * @param mixed        $user
44
     *
45
     * @return bool
46
     */
47 51
    protected function decideRules($rules, $user = null)
48
    {
49 51
        if (is_string($rules)) {
50 2
            return $this->decideRule($rules, $user);
51
        }
52
53 50
        if (! is_array($rules) || empty($rules)) {
54 4
            return false;
55
        }
56
57 48
        if (isset($rules[self::FIELD_VALUE]) && ! isset($rules[self::FIELD_FILTERS])) {
58 15
            return $rules[self::FIELD_VALUE];
59
        }
60
61 39
        if (array_key_exists(self::FIELD_VALUE, $rules) && array_key_exists(self::FIELD_FILTERS, $rules)) {
62 38
            if ($this->decideRules($rules[self::FIELD_FILTERS], $user)) {
63 18
                return $rules[self::FIELD_VALUE];
64
            }
65
66 23
            return false;
67
        }
68
69 38
        foreach ($rules as $subRules) {
70 38
            if (! $this->decideRule($subRules, $user)) {
71 23
                return false;
72
            }
73 20
        }
74
75 18
        return true;
76
    }
77
78
    /**
79
     * Decides a single rule.
80
     *
81
     * @param string $rule
82
     * @param mixed  $user
83
     *
84
     * @return bool
85
     */
86 40
    protected function decideRule($rule, $user = null)
87
    {
88 40
        $parameter = null;
89 40
        $additional = [];
90 40
        $negative = false;
91
92 40
        if (0 === strpos($rule, self::RULE_INVERTER)) {
93 4
            $negative = true;
94 4
            $rule = substr($rule, 1);
95 4
        }
96
97 40
        if (false !== strpos($rule, self::RULE_PARAM_DELIMITER)) {
98 8
            $splits = explode(self::RULE_PARAM_DELIMITER, $rule);
99 8
            $rule = array_shift($splits);
100 8
            $parameter = array_shift($splits);
101
102 8
            if (! empty($splits)) {
103 4
                $additional = $splits;
104 4
            }
105 8
        }
106
107 40
        if (! isset($this->voters[strtolower($rule)])) {
108 4
            return false;
109
        }
110
111 36
        $voter = $this->voters[strtolower($rule)];
112 36
        if (! is_object($voter)) {
113 4
            if (! class_exists($voter)) {
114 2
                return false;
115
            }
116
117 2
            $voter = new $voter();
118 2
        }
119
120 34
        if (! $voter instanceof VoterInterface) {
121 2
            return false;
122
        }
123
124 32
        $result = $voter->vote($parameter, $user, $additional);
125
126 32
        return $negative ? (false === $result) : $result;
127
    }
128
}
129