GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

FiniteCaller::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 23
cts 23
cp 1
rs 9.392
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 3
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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