Completed
Push — master ( b2eff8...fc5182 )
by Edward
02:59 queued 49s
created

ProcessManager::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4286
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
/**
3
 * File ProcessManager.php
4
 *
5
 * @author Edward Pfremmer <[email protected]>
6
 */
7
namespace Epfremme\ProcessQueue\Process;
8
9
use GuzzleHttp\Promise\Promise;
10
use GuzzleHttp\Promise\PromiseInterface;
11
use Symfony\Component\Process\Process;
12
13
/**
14
 * Class ProcessManager
15
 *
16
 * @package Epfremme\ProcessQueue\Process
17
 */
18
class ProcessManager
19
{
20
    /**
21
     * @var ProcessQueue
22
     */
23
    private $queue;
24
25
    /**
26
     * @var ProcessFactory
27
     */
28
    private $factory;
29
30
    /**
31
     * ProcessFactory constructor
32
     *
33
     * @param ProcessFactory $factory
34
     * @param null $limit
35
     */
36
    public function __construct(ProcessFactory $factory, $limit = null)
37
    {
38
        $this->queue = new ProcessQueue($limit);
39
40
        $this->factory = $factory;
41
    }
42
43
    /**
44
     * @param \SplFileInfo|string $cwd
45
     * @return PromiseInterface
46
     */
47
    public function enqueue($cwd = null)
48
    {
49
        $process = $this->factory->make($cwd);
50
51
        /** @var Promise $promise */
52
        $promise = new Promise(function() use ($process, &$promise) {
53
            $process->wait();
54
            $process->getExitCode() > 0
55
                ? $promise->reject($process)
56
                : $promise->resolve($process);
57
        });
58
59
        $this->queue->add($process);
60
61
        $process->setOptions([ProcessQueue::PROMISE_KEY => $promise]);
62
63
        return $promise;
64
    }
65
66
    public function run(\Closure $tick = null)
67
    {
68
        $queue = $this->queue;
69
70
        /** @var Process $next */
71
        foreach ($queue() as $next) {
72
            $next->start();
73
            $tick && $tick();
74
        }
75
    }
76
}
77