Completed
Pull Request — master (#94)
by Alessandro
04:33
created

PipelineCollection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Paraunit\Runner;
4
5
use Paraunit\Process\AbstractParaunitProcess;
6
7
/**
8
 * Class PipelineCollection
9
 * @package Paraunit\Runner
10
 */
11
class PipelineCollection
12
{
13
    /** @var Pipeline[] | \SplFixedArray */
14
    private $pipelines;
15
16 40
    public function __construct(PipelineFactory $pipelineFactory, int $maxProcessNumber = 10)
17
    {
18 40
        $this->pipelines = new \SplFixedArray($maxProcessNumber);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \SplFixedArray($maxProcessNumber) of type object<SplFixedArray> is incompatible with the declared type array<integer,object<Paraunit\Runner\Pipeline>> of property $pipelines.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
19
20 40
        for ($pipelineNumber = 0; $pipelineNumber < $maxProcessNumber; $pipelineNumber++) {
21 40
            $this->pipelines->offsetSet($pipelineNumber, $pipelineFactory->create($pipelineNumber));
22
        }
23
    }
24
25
    /**
26
     * @param AbstractParaunitProcess $process
27
     * @return Pipeline
28
     * @throws \RuntimeException
29
     */
30 17
    public function push(AbstractParaunitProcess $process): Pipeline
31
    {
32 17
        foreach ($this->pipelines as $pipeline) {
33 17
            if ($pipeline->isFree()) {
34 16
                $pipeline->execute($process);
35
36 17
                return $pipeline;
37
            }
38
        }
39
40 1
        throw new \RuntimeException('Cannot find an available pipeline');
41
    }
42
43 19
    public function hasEmptySlots(): bool
44
    {
45 19
        foreach ($this->pipelines as $pipeline) {
46 19
            if ($pipeline->isFree()) {
47 19
                return true;
48
            }
49
        }
50
51 2
        return false;
52
    }
53
54 19
    public function checkRunningState(): bool
55
    {
56 19
        $isRunning = false;
57
58 19
        foreach ($this->pipelines as $pipeline) {
59 19
            if (! $pipeline->isTerminated()) {
60 19
                $isRunning = true;
61
            }
62
        }
63
64 19
        return $isRunning;
65
    }
66
}
67