Pipeline   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 74
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 11 2
A isFree() 0 4 1
A isTerminated() 0 8 2
A triggerTermination() 0 14 3
A getNumber() 0 4 1
A handleProcessTermination() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Paraunit\Runner;
6
7
use Paraunit\Lifecycle\ProcessEvent;
8
use Paraunit\Process\AbstractParaunitProcess;
9
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10
11
/**
12
 * Class Pipeline
13
 * @package Paraunit\Runner
14
 */
15
class Pipeline
16
{
17
    /** @var EventDispatcherInterface */
18
    private $dispatcher;
19
20
    /** @var AbstractParaunitProcess|null */
21
    private $process;
22
23
    /** @var int */
24
    private $number;
25
26
    /**
27
     * Pipeline constructor.
28
     * @param EventDispatcherInterface $dispatcher
29
     * @param int $number
30
     */
31 48
    public function __construct(EventDispatcherInterface $dispatcher, int $number)
32
    {
33 48
        $this->dispatcher = $dispatcher;
34 48
        $this->number = $number;
35
    }
36
37 29
    public function execute(AbstractParaunitProcess $process)
38
    {
39 29
        if (! $this->isFree()) {
40 1
            throw new \RuntimeException('This pipeline is not free');
41
        }
42
43 29
        $this->process = $process;
44 29
        $this->process->start($this->number);
45
46 29
        $this->dispatcher->dispatch(ProcessEvent::PROCESS_STARTED, new ProcessEvent($this->process));
47
    }
48
49 30
    public function isFree(): bool
50
    {
51 30
        return $this->process === null;
52
    }
53
54 3
    public function isTerminated(): bool
55
    {
56 3
        if ($this->isFree()) {
57 1
            return true;
58
        }
59
60 2
        return $this->process->isTerminated();
61
    }
62
63 25
    public function triggerTermination(): bool
64
    {
65 25
        if ($this->isFree()) {
66 24
            return false;
67
        }
68
69 25
        if ($this->process->isTerminated()) {
70 25
            $this->handleProcessTermination();
71
72 25
            return true;
73
        }
74
75 24
        return false;
76
    }
77
78 2
    public function getNumber(): int
79
    {
80 2
        return $this->number;
81
    }
82
83 25
    private function handleProcessTermination()
84
    {
85 25
        $this->dispatcher->dispatch(ProcessEvent::PROCESS_TERMINATED, new ProcessEvent($this->process));
0 ignored issues
show
Bug introduced by
It seems like $this->process can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
86 25
        $this->process = null;
87
    }
88
}
89