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

Pipeline   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 68
ccs 23
cts 23
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 11 2
A isFree() 0 4 1
A isTerminated() 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 35
    public function __construct(EventDispatcherInterface $dispatcher, int $number)
31
    {
32 35
        $this->dispatcher = $dispatcher;
33 35
        $this->number = $number;
34
    }
35
36 19
    public function execute(AbstractParaunitProcess $process)
37
    {
38 19
        if (! $this->isFree()) {
39 1
            throw new \RuntimeException('This pipeline is not free');
40
        }
41
42 19
        $this->process = $process;
43 19
        $this->process->start([
44 19
            EnvVariables::PIPELINE_NUMBER => $this->number,
45
        ]);
46
    }
47
48 20
    public function isFree(): bool
49
    {
50 20
        return $this->process === null;
51
    }
52
53
    /**
54
     * @return bool
55
     */
56 18
    public function isTerminated(): bool
57
    {
58 18
        if ($this->isFree()) {
59 16
            return true;
60
        }
61
62 17
        if ($this->process->isTerminated()) {
63 16
            $this->handleProcessTermination();
64
65 16
            return true;
66
        }
67
68 16
        return false;
69
    }
70
71 1
    public function getNumber(): int
72
    {
73 1
        return $this->number;
74
    }
75
76 16
    private function handleProcessTermination()
77
    {
78 16
        $this->dispatcher->dispatch(ProcessEvent::PROCESS_TERMINATED, new ProcessEvent($this->process));
79 16
        $this->process = null;
80
    }
81
}
82