Completed
Pull Request — master (#41)
by Wachter
20:46
created

SeparateProcessExecutor::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of php-task library.
5
 *
6
 * (c) php-task
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Task\TaskBundle\Executor;
13
14
use Symfony\Component\Process\ProcessBuilder;
15
use Task\Execution\TaskExecutionInterface;
16
use Task\Executor\ExecutorInterface;
17
18
/**
19
 * Uses a separate process to start the executions via console-command.
20
 */
21
class SeparateProcessExecutor implements ExecutorInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    private $consolePath;
27
28
    /**
29
     * @var string
30
     */
31
    private $environment;
32
33
    /**
34
     * @param string $consolePath
35
     * @param string $environment
36
     */
37
    public function __construct($consolePath, $environment)
38
    {
39
        $this->consolePath = $consolePath;
40
        $this->environment = $environment;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function execute(TaskExecutionInterface $execution)
47
    {
48
        $process = ProcessBuilder::create(
49
            [$this->consolePath, 'task:execute', $execution->getUuid(), '-e ' . $this->environment]
50
        )->getProcess();
51
52
        $process->run();
53
54
        if (!$process->isSuccessful()) {
55
            throw new SeparateProcessException($process->getErrorOutput());
56
        }
57
58
        return $process->getOutput();
59
    }
60
}
61