1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\Recoil; |
4
|
|
|
|
5
|
|
|
use Recoil\Kernel; |
6
|
|
|
use Rx\ObservableInterface; |
7
|
|
|
use Rx\Subject\Subject; |
8
|
|
|
|
9
|
|
|
final class FiniteCaller implements QueueCallerPoolInterface |
10
|
|
|
{ |
11
|
|
|
/** @var State */ |
12
|
|
|
private $state; |
13
|
|
|
|
14
|
|
|
/** @var QueueCallerInterface[] */ |
15
|
|
|
private $caller = []; |
16
|
|
|
|
17
|
|
|
/** @var State[] */ |
18
|
|
|
private $callerState = []; |
19
|
|
|
|
20
|
|
|
/** @var Subject */ |
21
|
|
|
private $callerStream = []; |
22
|
|
|
|
23
|
|
|
/** @var array */ |
24
|
|
|
private $queue = []; |
25
|
|
|
|
26
|
|
|
/** @var int */ |
27
|
|
|
private $callersBusy = 0; |
28
|
|
|
|
29
|
|
|
/** @var int */ |
30
|
|
|
private $callersCount = 0; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Kernel $kernel |
34
|
|
|
* @param int $size |
35
|
|
|
*/ |
36
|
3 |
|
public function __construct(Kernel $kernel, int $size) |
37
|
|
|
{ |
38
|
3 |
|
$this->callersCount = $size; |
39
|
3 |
|
$this->state = new State(); |
40
|
3 |
|
for ($i = 0; $i < $size; $i++) { |
41
|
3 |
|
$subject = new Subject(); |
42
|
3 |
|
$caller = new QueueCaller($kernel); |
43
|
3 |
|
$hash = \spl_object_hash($caller); |
44
|
3 |
|
$this->caller[$hash] = $caller; |
45
|
3 |
|
$this->callerStream[$hash] = $subject; |
46
|
3 |
|
$this->callerState[$hash] = $caller->call($subject); |
47
|
|
|
$this->callerState[$hash]->filter(function () { |
48
|
3 |
|
return \count($this->queue) > 0; |
49
|
|
|
})->filter(function (int $state) { |
50
|
2 |
|
return $state === State::WAITING; |
51
|
|
|
})->subscribe(function () use ($hash): void { |
52
|
2 |
|
$this->callerStream[$hash]->onNext(\array_shift($this->queue)); |
53
|
3 |
|
}); |
54
|
|
|
$this->callerState[$hash]->subscribe(function () use ($hash): void { |
55
|
|
|
$this->callersBusy = \count(\array_filter($this->callerState, function (State $state) { |
56
|
3 |
|
return $state->getState() === State::BUSY; |
57
|
3 |
|
})); |
58
|
3 |
|
if ($this->callersBusy === $this->callersCount) { |
59
|
3 |
|
$this->state->onNext(State::BUSY); |
60
|
|
|
|
61
|
3 |
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
$this->state->onNext(State::WAITING); |
65
|
3 |
|
}); |
66
|
|
|
} |
67
|
3 |
|
$this->state->onNext(State::STARTED); |
68
|
3 |
|
} |
69
|
|
|
|
70
|
3 |
|
public function call(ObservableInterface $observable): State |
71
|
|
|
{ |
72
|
|
|
$observable->subscribe(function (Call $call): void { |
73
|
3 |
|
$this->delegateCall($call); |
74
|
3 |
|
}); |
75
|
|
|
|
76
|
3 |
|
return $this->state; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
public function info(): iterable |
80
|
|
|
{ |
81
|
2 |
|
yield 'callers' => $this->callersCount; |
82
|
2 |
|
yield 'busy' => $this->callersBusy; |
83
|
2 |
|
yield 'waiting' => $this->callersCount - $this->callersBusy; |
84
|
2 |
|
} |
85
|
|
|
|
86
|
3 |
|
private function delegateCall(Call $call): void |
87
|
|
|
{ |
88
|
3 |
View Code Duplication |
foreach ($this->callerState as $qcHash => $state) { |
|
|
|
|
89
|
3 |
|
if ($state->getState() !== State::WAITING) { |
90
|
3 |
|
continue; |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
$this->callerStream[$qcHash]->onNext($call); |
94
|
|
|
|
95
|
3 |
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
$this->state->onNext(State::BUSY); |
99
|
2 |
|
$this->queue[] = $call; |
100
|
2 |
|
} |
101
|
|
|
} |
102
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.