InMemoryRules   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 204
c 0
b 0
f 0
wmc 37
lcom 1
cbo 3
rs 9.44

12 Methods

Rating   Name   Duplication   Size   Complexity  
A addRule() 0 8 1
A removeRule() 0 10 2
B canRunTask() 0 20 8
A getRulesForTask() 0 6 2
A hasTooManyProcessesRunning() 0 4 2
A withinConstraints() 0 4 2
A withinProcessorConstraints() 0 8 4
A withinMemoryConstraints() 0 8 4
A hasTooManySiblingProcessesRunning() 0 4 2
A withinSiblingConstraints() 0 4 2
A withinSiblingProcessorConstraints() 0 8 4
A withinSiblingMemoryConstraints() 0 8 4
1
<?php
2
3
namespace AsyncPHP\Doorman\Rules;
4
5
use AsyncPHP\Doorman\Profile;
6
use AsyncPHP\Doorman\Rule;
7
use AsyncPHP\Doorman\Rules;
8
use AsyncPHP\Doorman\Task;
9
10
class InMemoryRules implements Rules
11
{
12
    /**
13
     * @var Rule[]
14
     */
15
    protected $rules = array();
16
17
    /**
18
     * @inheritdoc
19
     *
20
     * @param Rule $rule
21
     *
22
     * @return $this
23
     */
24
    public function addRule(Rule $rule)
25
    {
26
        $hash = spl_object_hash($rule);
27
28
        $this->rules[$hash] = $rule;
29
30
        return $this;
31
    }
32
33
    /**
34
     * @inheritdoc
35
     *
36
     * @param Rule $rule
37
     *
38
     * @return $this
39
     */
40
    public function removeRule(Rule $rule)
41
    {
42
        $hash = spl_object_hash($rule);
43
44
        if (isset($this->rules[$hash])) {
45
            unset($this->rules[$hash]);
46
        }
47
48
        return $this;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     *
54
     * @param Task    $task
55
     * @param Profile $profile
56
     *
57
     * @return bool
58
     */
59
    public function canRunTask(Task $task, Profile $profile)
60
    {
61
        $rules = $this->getRulesForTask($task);
62
63
        if (count($rules) === 0) {
64
            return true;
65
        }
66
67
        foreach ($rules as $rule) {
68
            if ($rule->getProcesses() === null) {
69
                continue;
70
            }
71
72
            if (($rule->getHandler() === null || $rule->getHandler() === $task->getHandler()) && ($this->hasTooManyProcessesRunning($rule, $profile) || $this->hasTooManySiblingProcessesRunning($rule, $profile))) {
73
                return false;
74
            }
75
        }
76
77
        return true;
78
    }
79
80
    /**
81
     * Gets all the rules that apply to a task.
82
     *
83
     * @param Task $task
84
     *
85
     * @return Rule[]
86
     */
87
    protected function getRulesForTask(Task $task)
88
    {
89
        return array_filter($this->rules, function (Rule $rule) use ($task) {
90
            return $rule->getHandler() === null || $rule->getHandler() === $task->getHandler();
91
        });
92
    }
93
94
    /**
95
     * Checks whether there are too many processes running to start a new one.
96
     *
97
     * @param Rule    $rule
98
     * @param Profile $profile
99
     *
100
     * @return bool
101
     */
102
    protected function hasTooManyProcessesRunning(Rule $rule, Profile $profile)
103
    {
104
        return $this->withinConstraints($rule, $profile) && count($profile->getProcesses()) >= $rule->getProcesses();
105
    }
106
107
    /**
108
     * Checks whether the current profile is within the constraints of a rule.
109
     *
110
     * @param Rule    $rule
111
     * @param Profile $profile
112
     *
113
     * @return bool
114
     */
115
    protected function withinConstraints(Rule $rule, Profile $profile)
116
    {
117
        return $this->withinProcessorConstraints($rule, $profile) || $this->withinMemoryConstraints($rule, $profile);
118
    }
119
120
    /**
121
     * Checks whether the current profile is within the processor constraints of a rule.
122
     *
123
     * @param Rule    $rule
124
     * @param Profile $profile
125
     *
126
     * @return bool
127
     */
128
    protected function withinProcessorConstraints(Rule $rule, Profile $profile)
129
    {
130
        if ($rule->getMinimumProcessorUsage() !== null && $rule->getMaximumProcessorUsage() !== null) {
131
            return $profile->getProcessorLoad() >= $rule->getMinimumProcessorUsage() && $profile->getProcessorLoad() <= $rule->getMaximumProcessorUsage();
132
        }
133
134
        return false;
135
    }
136
137
    /**
138
     * Checks whether the current profile is within the memory constraints of a rule.
139
     *
140
     * @param Rule    $rule
141
     * @param Profile $profile
142
     *
143
     * @return bool
144
     */
145
    protected function withinMemoryConstraints(Rule $rule, Profile $profile)
146
    {
147
        if ($rule->getMinimumMemoryUsage() !== null && $rule->getMaximumMemoryUsage() !== null) {
148
            return $profile->getMemoryLoad() >= $rule->getMinimumMemoryUsage() && $profile->getMemoryLoad() <= $rule->getMaximumMemoryUsage();
149
        }
150
151
        return false;
152
    }
153
154
    /**
155
     * Checks whether there are too many sibling processes running to start a new one.
156
     *
157
     * @param Rule    $rule
158
     * @param Profile $profile
159
     *
160
     * @return bool
161
     */
162
    protected function hasTooManySiblingProcessesRunning(Rule $rule, Profile $profile)
163
    {
164
        return $this->withinSiblingConstraints($rule, $profile) && count($profile->getSiblingProcesses()) >= $rule->getProcesses();
165
    }
166
167
    /**
168
     * Checks whether the profile or sibling processes is within the constraints of a rule.
169
     *
170
     * @param Rule    $rule
171
     * @param Profile $profile
172
     *
173
     * @return bool
174
     */
175
    protected function withinSiblingConstraints(Rule $rule, Profile $profile)
176
    {
177
        return $this->withinSiblingProcessorConstraints($rule, $profile) || $this->withinSiblingMemoryConstraints($rule, $profile);
178
    }
179
180
    /**
181
     * Checks whether the profile or sibling processes is within the processor constraints of a rule.
182
     *
183
     * @param Rule    $rule
184
     * @param Profile $profile
185
     *
186
     * @return bool
187
     */
188
    protected function withinSiblingProcessorConstraints(Rule $rule, Profile $profile)
189
    {
190
        if ($rule->getMinimumSiblingProcessorUsage() !== null && $rule->getMaximumSiblingProcessorUsage() !== null) {
191
            return $profile->getSiblingProcessorLoad() >= $rule->getMinimumSiblingProcessorUsage() && $profile->getSiblingProcessorLoad() <= $rule->getMaximumSiblingProcessorUsage();
192
        }
193
194
        return false;
195
    }
196
197
    /**
198
     * Checks whether the profile or sibling processes is within the memory constraints of a rule.
199
     *
200
     * @param Rule    $rule
201
     * @param Profile $profile
202
     *
203
     * @return bool
204
     */
205
    protected function withinSiblingMemoryConstraints(Rule $rule, Profile $profile)
206
    {
207
        if ($rule->getMinimumSiblingMemoryUsage() !== null && $rule->getMaximumSiblingMemoryUsage() !== null) {
208
            return $profile->getSiblingMemoryLoad() >= $rule->getMinimumSiblingMemoryUsage() && $profile->getSiblingMemoryLoad() <= $rule->getMaximumSiblingMemoryUsage();
209
        }
210
211
        return false;
212
    }
213
}
214