Queue   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 44
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A executeAsync() 0 6 2
A executeImmediate() 0 9 1
A executeReserved() 0 4 1
A dequeue() 0 6 2
1
<?php
2
3
namespace mpyw\HyperBuiltinServer\Internal;
4
5
class Queue
6
{
7
    protected $processes = [];
8
    protected $promisors = [];
9
10
    public function __construct(array $processes)
11
    {
12
        if (!$processes) {
13
            throw new \LengthException('At least 1 process required.');
14
        }
15
        array_walk($processes, function (BuiltinServer $process) {
16
            $this->processes[spl_object_hash($process)] = $process;
17
        });
18
    }
19
20
    public function executeAsync(\Closure $promisor)
21
    {
22
        $this->processes
23
        ? $this->executeImmediate($promisor)
24
        : $this->executeReserved($promisor);
25
    }
26
27
    protected function executeImmediate(\Closure $promisor)
28
    {
29
        $process = current($this->processes);
30
        unset($this->processes[spl_object_hash($process)]);
31
        $promisor($process)->always(function () use ($process) {
32
            $this->processes[spl_object_hash($process)] = $process;
33
            $this->dequeue();
34
        });
35
    }
36
37
    protected function executeReserved(\Closure $promisor)
38
    {
39
        $this->promisors[] = $promisor;
40
    }
41
42
    protected function dequeue()
43
    {
44
        if ($promisor = array_shift($this->promisors)) {
45
            $this->executeImmediate($promisor);
46
        }
47
    }
48
}
49