PhingService::getPhingOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BsbPhingService\Service;
4
5
use BsbPhingService\Options\PhingOptions;
6
use BsbPhingService\Options\ServiceOptions;
7
use Symfony\Component\Process\Process;
8
use Symfony\Component\Process\ProcessBuilder;
9
use Traversable;
10
11
class PhingService
12
{
13
14
    /**
15
     * @var ServiceOptions
16
     */
17
    protected $options;
18
19
    /**
20
     * @var PhingOptions
21
     */
22
    protected $phingOptions;
23
24
    /**
25
     * @param ServiceOptions $options
26
     * @param PhingOptions   $phingOptions
27
     */
28 7
    public function __construct(ServiceOptions $options, PhingOptions $phingOptions)
29
    {
30 7
        $this->options      = $options;
31 7
        $this->phingOptions = $phingOptions;
32 7
    }
33
34
    /**
35
     * @return ServiceOptions
36
     */
37 1
    public function getOptions()
38
    {
39 1
        return $this->options;
40
    }
41
42
    /**
43
     * @return PhingOptions
44
     */
45 1
    public function getPhingOptions()
46
    {
47 1
        return $this->phingOptions;
48
    }
49
50
    /**
51
     * Configure phing and run a build.
52
     *
53
     * @param string                 $targets   space separated list of targets
54
     * @param null|array|Traversable $options   override the default phing options
55
     * @param bool                   $immediate call run on the process instance, set to false if you need to do
56
     *                                          advanced process management
57
     *
58
     * @return Process
59
     */
60 4
    public function build($targets = "", $options = null, $immediate = true)
61
    {
62 4
        $phingOptions = clone $this->phingOptions;
63
64 4
        if ($options !== null) {
65 2
            $phingOptions->setFromArray($options);
66 2
        }
67
68 4
        $process = $this->createProcessInstance($targets, $phingOptions);
69
70 4
        if ($immediate) {
71 4
            $process->run();
72 4
        }
73
74 4
        return $process;
75
    }
76
77
    /**
78
     * Constructs a configured Process instance
79
     *
80
     * @param string       $targets space separated list of targets
81
     * @param PhingOptions $options
82
     *
83
     * @return Process
84
     */
85 4
    private function createProcessInstance($targets, PhingOptions $options)
86
    {
87 4
        $builder = new ProcessBuilder();
88
89 4
        $builder->setPrefix($this->options->getPhingBin());
90 4
        $builder->setArguments($options->toArgumentsArray());
91
92 4
        foreach (explode(' ', $targets) as $target) {
93 4
            if (strlen(trim($target))) {
94 2
                $builder->add(trim($target));
95 2
            }
96 4
        }
97
98 4
        return $builder->getProcess();
99
    }
100
}
101