Completed
Pull Request — master (#94)
by Alessandro
03:18
created

Pipeline::isTerminated()   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
c 0
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
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 36
    public function __construct(EventDispatcherInterface $dispatcher, int $number)
31
    {
32 36
        $this->dispatcher = $dispatcher;
33 36
        $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 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 15
    public function triggerTermination(): bool
63
    {
64 15
        if ($this->isFree()) {
65 14
            return false;
66
        }
67
68 15
        if ($this->process->isTerminated()) {
69 15
            $this->handleProcessTermination();
70
71 15
            return true;
72
        }
73
74 14
        return false;
75
    }
76
77 2
    public function getNumber(): int
78
    {
79 2
        return $this->number;
80
    }
81
82 15
    private function handleProcessTermination()
83
    {
84 15
        $this->dispatcher->dispatch(ProcessEvent::PROCESS_TERMINATED, new ProcessEvent($this->process));
85 15
        $this->process = null;
86
    }
87
}
88