Completed
Pull Request — master (#3)
by Harry
05:31
created

ProcessTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 4
c 2
b 1
f 0
lcom 1
cbo 1
dl 0
loc 66
ccs 11
cts 11
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setProcessFactory() 0 6 1
A getProcess() 0 11 1
log() 0 1 ?
A getProcessFactory() 0 8 2
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Helper\Process;
15
16
use Psr\Log\LogLevel;
17
use RuntimeException;
18
use Symfony\Component\Process\Process;
19
20
trait ProcessTrait
21
{
22
    /**
23
     * @var ProcessFactory
24
     */
25
    private $processFactory;
26
27
    /**
28
     * @param ProcessFactory $processFactory
29
     *
30
     * @return $this
31
     */
32 79
    public function setProcessFactory(ProcessFactory $processFactory)
33
    {
34 79
        $this->processFactory = $processFactory;
35
36 79
        return $this;
37
    }
38
39
    /**
40
     * @return ProcessFactory
41
     */
42 65
    public function getProcessFactory()
43
    {
44 65
        if (!$this->processFactory) {
45 18
            $this->processFactory = new ProcessFactory();
46 18
        }
47
48 65
        return $this->processFactory;
49
    }
50
51
    /**
52
     * @param string         $commandline The command line to run
53
     * @param string|null    $cwd         The working directory or null to use the working dir of the current PHP
54
     *                                    process
55
     * @param array|null     $env         The environment variables or null to inherit
56
     * @param string|null    $input       The input
57
     * @param int|float|null $timeout     The timeout in seconds or null to disable
58
     * @param array          $options     An array of options for proc_open
59
     *
60
     * @return Process
61
     * @throws RuntimeException When proc_open is not installed
62
     */
63 65
    public function getProcess(
64
        $commandline,
65
        $cwd = null,
66
        array $env = null,
67
        $input = null,
68
        $timeout = 60,
69
        array $options = []
70
    ) {
71 65
        $this->log(LogLevel::DEBUG, "Running command: {cmd}", ['cmd' => $commandline]);
72 65
        return $this->getProcessFactory()->createProcess($commandline, $cwd, $env, $input, $timeout, $options);
73
    }
74
75
    /**
76
     * Abstract Log function that might should be handed by the OptionalLoggerTrait or similar
77
     *
78
     * @param string $level
79
     * @param string $message
80
     * @param array  $context
81
     *
82
     * @return void
83
     */
84
    abstract protected function log($level, $message, array $context = []);
85
}
86