Completed
Pull Request — master (#40)
by Wachter
12:13
created

ProcessExecutor::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
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\Runner\ExecutorInterface;
17
use Task\TaskBundle\Command\ExecuteCommand;
18
19
/**
20
 * Uses a process to start the executions via console-command.
21
 */
22
class ProcessExecutor implements ExecutorInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    private $consoleFile;
28
29
    /**
30
     * @param string $consoleFile
31
     */
32 13
    public function __construct($consoleFile)
33
    {
34 13
        $this->consoleFile = $consoleFile;
35 13
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 2
    public function execute(TaskExecutionInterface $execution)
41
    {
42 2
        $process = ProcessBuilder::create([$this->consoleFile, 'task:execute', $execution->getUuid()])->getProcess();
43
44 2
        $process->run();
45
46 2
        if (!$process->isSuccessful()) {
47 1
            throw new ProcessException($process->getErrorOutput());
48
        }
49
50 1
        return $this->extractResult($process->getOutput());
51
    }
52
53
    /**
54
     * Extract the result from output.
55
     *
56
     * @param string $output
57
     *
58
     * @return string
59
     */
60 1
    private function extractResult($output)
61
    {
62 1
        $match = preg_match(
63 1
            sprintf(
64 1
                '/%s(?<result>.*)%s/s',
65 1
                preg_quote(ExecuteCommand::START_RESULT),
66 1
                preg_quote(ExecuteCommand::END_RESULT)
67 1
            ),
68 1
            $output,
69
            $matches
70 1
        );
71
72 1
        if (!$match) {
73
            return;
74
        }
75
76 1
        return $matches['result'];
77
    }
78
}
79