Passed
Pull Request — master (#40)
by Marc
01:23
created

ProcessBuilder::setEnv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/*
4
 * This file is part of the asm\php-ansible package.
5
 *
6
 * (c) Marc Aschmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Asm\Ansible\Process;
13
14
use Symfony\Component\Process\Process;
15
16
/**
17
 * Wrapper for symfony process component to allow for command option/argument collection before execute
18
 *
19
 * @package Asm\Ansible\Process
20
 * @author Marc Aschmann <[email protected]>
21
 */
22
class ProcessBuilder implements ProcessBuilderInterface
23
{
24
    /**
25
     * @var array
26
     */
27
    private $arguments;
28
29
    /**
30
     * @var int
31
     */
32
    private $timeout;
33
34
    /**
35
     * @var string
36
     */
37
    private $path;
38
39
    /**
40
41
     * @var array
42
     */
43
    private $envVars;
44
45
    /**
46
47
     * ProcessBuilder constructor.
48
     *
49
     * @param string $prefix
50
     * @param string $path
51
     */
52
    public function __construct(string $prefix, string $path)
53
    {
54
        $this->arguments = [$prefix];
55
        $this->path = $path;
56
        $this->timeout = 900;
57
        $this->envVars = [];
58
    }
59
60
    /**
61
     * @param array $arguments arguments for process generation
62
     * @return ProcessBuilderInterface
63
     */
64
    public function setArguments(array $arguments): ProcessBuilderInterface
65
    {
66
        $this->arguments = $arguments;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @param int $timeout
73
     * @return ProcessBuilderInterface
74
     */
75
    public function setTimeout(int $timeout): ProcessBuilderInterface
76
    {
77
        $this->timeout = $timeout;
78
79
        return $this;
80
    }
81
82
    /**
83
     * @param string $name name of ENV VAR
84
     * @param string $value
85
     * @return ProcessBuilderInterface
86
     */
87
    public function setEnv(string $name, string $value): ProcessBuilderInterface
88
    {
89
        $this->envVars[$name] = $value;
90
91
        return $this;
92
    }
93
94
    /**
95
96
     * @return Process
97
     */
98
    public function getProcess(): Process
99
    {
100
        return (
101
            new Process(
102
                $this->arguments,
103
                $this->path
104
            )
105
        )
106
        ->setTimeout($this->timeout)
107
        ->setEnv($this->envVars);
108
    }
109
}
110