Phing::getBuildFilePath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Fabrica\Tools\Plugin;
4
5
use Fabrica\Tools\Builder;
6
use Fabrica\Models\Infra\Ci\Build;
7
use Fabrica\Tools\Plugin;
8
9
/**
10
 * Phing Plugin - Provides access to Phing functionality.
11
 *
12
 * @author Pavel Pavlov <[email protected]>
13
 */
14
class Phing extends Plugin
15
{
16
    protected $buildFile  = 'build.xml';
17
    protected $targets    = ['build'];
18
    protected $properties = [];
19
    protected $propertyFile;
20
21
    /**
22
     * @return string
23
     */
24
    public static function pluginName()
25
    {
26
        return 'phing';
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function __construct(Builder $builder, Build $build, array $options = [])
33
    {
34
        parent::__construct($builder, $build, $options);
35
36
        /*
37
         * Sen name of a non default build file
38
         */
39
        if (isset($options['build_file'])) {
40
            $this->setBuildFile($options['build_file']);
41
        }
42
43
        if (isset($options['targets'])) {
44
            $this->setTargets($options['targets']);
45
        }
46
47
        if (isset($options['properties'])) {
48
            $this->setProperties($options['properties']);
49
        }
50
51
        if (isset($options['property_file'])) {
52
            $this->setPropertyFile($options['property_file']);
53
        }
54
55
        $this->executable = $this->findBinary('phing');
56
    }
57
58
    /**
59
     * Executes Phing and runs a specified targets
60
     */
61
    public function execute()
62
    {
63
        $phingExecutable = $this->executable;
64
65
        $cmd[] = $phingExecutable . ' -f ' . $this->getBuildFilePath();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$cmd was never initialized. Although not strictly required by PHP, it is generally a good practice to add $cmd = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
66
67
        if ($this->getPropertyFile()) {
68
            $cmd[] = '-propertyfile ' . $this->getPropertyFile();
69
        }
70
71
        $cmd[] = $this->propertiesToString();
72
73
        $cmd[] = '-logger phing.listener.DefaultLogger';
74
        $cmd[] = $this->targetsToString();
75
        $cmd[] = '2>&1';
76
77
        return $this->builder->executeCommand(implode(' ', $cmd), $this->directory, $this->targets);
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    public function getTargets()
84
    {
85
        return $this->targets;
86
    }
87
88
    /**
89
     * Converts an array of targets into a string.
90
     *
91
     * @return string
92
     */
93
    private function targetsToString()
94
    {
95
        return implode(' ', $this->targets);
96
    }
97
98
    /**
99
     * @param array|string $targets
100
     *
101
     * @return $this
102
     */
103
    public function setTargets($targets)
104
    {
105
        if (is_string($targets)) {
106
            $targets = [$targets];
107
        }
108
109
        $this->targets = $targets;
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function getBuildFile()
116
    {
117
        return $this->buildFile;
118
    }
119
120
    /**
121
     * @param mixed $buildFile
122
     *
123
     * @return $this
124
     * @throws \Exception
125
     */
126
    public function setBuildFile($buildFile)
127
    {
128
        if (!file_exists($this->directory . $buildFile)) {
129
            throw new \Exception('Specified build file does not exist.');
130
        }
131
132
        $this->buildFile = $buildFile;
133
    }
134
135
    /**
136
     * Get phing build file path.
137
     *
138
     * @return string
139
     */
140
    public function getBuildFilePath()
141
    {
142
        return $this->directory . $this->buildFile;
143
    }
144
145
    /**
146
     * @return mixed
147
     */
148
    public function getProperties()
149
    {
150
        return $this->properties;
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    public function propertiesToString()
157
    {
158
        /**
159
         * fix the problem when execute phing out of the build dir
160
         *
161
         * @ticket 748
162
         */
163
        if (!isset($this->properties['project.basedir'])) {
164
            $this->properties['project.basedir'] = $this->getDirectory();
0 ignored issues
show
Bug introduced by
The method getDirectory() does not seem to exist on object<Fabrica\Tools\Plugin\Phing>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
165
        }
166
167
        $propertiesString = [];
168
169
        foreach ($this->properties as $name => $value) {
170
            $propertiesString[] = '-D' . $name . '="' . $value . '"';
171
        }
172
173
        return implode(' ', $propertiesString);
174
    }
175
176
    /**
177
     * @param array|string $properties
178
     *
179
     * @return $this
180
     */
181
    public function setProperties($properties)
182
    {
183
        if (is_string($properties)) {
184
            $properties = [$properties];
185
        }
186
187
        $this->properties = $properties;
188
    }
189
190
    /**
191
     * @return string
192
     */
193
    public function getPropertyFile()
194
    {
195
        return $this->propertyFile;
196
    }
197
198
    /**
199
     * @param string $propertyFile
200
     *
201
     * @return $this
202
     * @throws \Exception
203
     */
204
    public function setPropertyFile($propertyFile)
205
    {
206
        if (!file_exists($this->getDirectory() . '/' . $propertyFile)) {
0 ignored issues
show
Bug introduced by
The method getDirectory() does not seem to exist on object<Fabrica\Tools\Plugin\Phing>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
207
            throw new \Exception('Specified property file does not exist.');
208
        }
209
210
        $this->propertyFile = $propertyFile;
211
    }
212
}
213