Passed
Push — master ( ec995b...7ad6ff )
by Dmitry
37s
created

Fork::getProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace SymfonyBundles\Fork;
4
5
class Fork implements ForkInterface
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $tasks;
11
12
    /**
13
     * @var ProcessInterface
14
     */
15
    protected $process;
16
17
    /**
18
     * @param ProcessInterface $process
19
     */
20 3
    public function __construct(ProcessInterface $process = null)
21
    {
22 3
        if (null === $process) {
23 2
            $process = new Process();
24
        }
25
26 3
        $this->tasks = [];
27 3
        $this->process = $process;
28 3
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 1
    public function getProcess(): ProcessInterface
34
    {
35 1
        return $this->process;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 1
    public function exists(TaskInterface $task): bool
42
    {
43 1
        return false !== in_array($task, $this->tasks, true);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 2
    public function attach(TaskInterface $task): ForkInterface
50
    {
51 2
        $this->tasks[] = $task;
52
53 2
        return $this;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 1
    public function detach(TaskInterface $task): ForkInterface
60
    {
61 1
        if ($this->exists($task)) {
62 1
            $key = array_search($task, $this->tasks, true);
63
64 1
            unset($this->tasks[$key]);
65
        }
66
67 1
        return $this;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function each(): \Closure
74
    {
75 2
        return function () {
76 1
            foreach ($this->tasks as $task) {
77 1
                $task->execute($this->process);
78
            }
79 2
        };
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 2
    public function run(int $processesCount = ProcessInterface::AUTO_DETECT_OF_PROCESSES_QUANTITY): ProcessInterface
86
    {
87 2
        return $this->process->setCountOfChildProcesses($processesCount)->create($this->each());
88
    }
89
}
90