Completed
Pull Request — master (#40)
by Wachter
05:27
created

ProcessExecutor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.83%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 4
dl 0
loc 66
ccs 23
cts 24
cp 0.9583
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 14 2
A extractResult() 0 18 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
     * @var string
31
     */
32
    private $environment;
33
34
    /**
35
     * @param string $consoleFile
36
     * @param string $environment
37
     */
38 13
    public function __construct($consoleFile, $environment)
39
    {
40 13
        $this->consoleFile = $consoleFile;
41 13
        $this->environment = $environment;
42 13
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 2
    public function execute(TaskExecutionInterface $execution)
48
    {
49 2
        $process = ProcessBuilder::create(
50 2
            [$this->consoleFile, 'task:execute', $execution->getUuid(), '-e ' . $this->environment]
51 2
        )->getProcess();
52
53 2
        $process->run();
54
55 2
        if (!$process->isSuccessful()) {
56 1
            throw new ProcessException($process->getErrorOutput());
57
        }
58
59 1
        return $this->extractResult($process->getOutput());
60
    }
61
62
    /**
63
     * Extract the result from output.
64
     *
65
     * @param string $output
66
     *
67
     * @return string
68
     */
69 1
    private function extractResult($output)
70
    {
71 1
        $match = preg_match(
72 1
            sprintf(
73 1
                '/%s(?<result>.*)%s/s',
74 1
                preg_quote(ExecuteCommand::START_RESULT),
75 1
                preg_quote(ExecuteCommand::END_RESULT)
76 1
            ),
77 1
            $output,
78
            $matches
79 1
        );
80
81 1
        if (!$match) {
82
            return;
83
        }
84
85 1
        return $matches['result'];
86
    }
87
}
88