Completed
Push — master ( b61e2e...b32211 )
by Alessandro
11s
created

Pipeline   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

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
namespace Paraunit\Runner;
4
5
use Paraunit\Configuration\EnvVariables;
6
use Paraunit\Lifecycle\ProcessEvent;
7
use Paraunit\Process\AbstractParaunitProcess;
8
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
10
/**
11
 * Class Pipeline
12
 * @package Paraunit\Runner
13
 */
14
class Pipeline
15
{
16
    /** @var EventDispatcherInterface */
17
    private $dispatcher;
18
19
    /** @var AbstractParaunitProcess */
20
    private $process;
21
22
    /** @var int */
23
    private $number;
24
25
    /**
26
     * Pipeline constructor.
27
     * @param EventDispatcherInterface $dispatcher
28
     * @param int $number
29
     */
30 37
    public function __construct(EventDispatcherInterface $dispatcher, int $number)
31
    {
32 37
        $this->dispatcher = $dispatcher;
33 37
        $this->number = $number;
34
    }
35
36 20
    public function execute(AbstractParaunitProcess $process)
37
    {
38 20
        if (! $this->isFree()) {
39 1
            throw new \RuntimeException('This pipeline is not free');
40
        }
41
42 20
        $this->process = $process;
43 20
        $this->process->start([
44 20
            EnvVariables::PIPELINE_NUMBER => $this->number,
45
        ]);
46
    }
47
48 21
    public function isFree(): bool
49
    {
50 21
        return $this->process === null;
51
    }
52
53 3
    public function isTerminated(): bool
54
    {
55 3
        if ($this->isFree()) {
56 1
            return true;
57
        }
58
59 2
        return $this->process->isTerminated();
60
    }
61
62 16
    public function triggerTermination(): bool
63
    {
64 16
        if ($this->isFree()) {
65 15
            return false;
66
        }
67
68 16
        if ($this->process->isTerminated()) {
69 16
            $this->handleProcessTermination();
70
71 16
            return true;
72
        }
73
74 15
        return false;
75
    }
76
77 2
    public function getNumber(): int
78
    {
79 2
        return $this->number;
80
    }
81
82 16
    private function handleProcessTermination()
83
    {
84 16
        $this->dispatcher->dispatch(ProcessEvent::PROCESS_TERMINATED, new ProcessEvent($this->process));
85 16
        $this->process = null;
86
    }
87
}
88