Composer   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 241
Duplicated Lines 9.96 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 94.81%

Importance

Changes 0
Metric Value
wmc 27
lcom 2
cbo 5
dl 24
loc 241
ccs 73
cts 77
cp 0.9481
rs 10
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createProject() 0 18 3
A requirePackage() 8 8 2
A updatePackage() 8 8 2
A removePackage() 8 8 2
A getHomePath() 0 4 1
A getConfigPath() 0 7 2
A getConfig() 0 4 1
A setConfig() 0 4 1
A flushConfig() 0 4 1
A validateConfig() 0 4 1
A dumpAutoload() 0 4 1
A mapOptions() 0 8 2
A getComposerConfigManager() 0 7 2
A getExecutor() 0 4 1
A setExecutor() 0 4 1
A getBalloonFactory() 0 4 1
A setBalloonFactory() 0 4 1
A execute() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Samurai\Project\Composer;
3
4
use Balloon\Balloon;
5
use Balloon\Factory\BalloonFactory;
6
use InvalidArgumentException;
7
use Samurai\Project\Project;
8
use TRex\Cli\Executor;
9
10
/**
11
 * Class Composer
12
 * @package Samurai\Project\Composer
13
 * @author Raphaël Lefebvre <[email protected]>
14
 */
15
class Composer
16
{
17
    /**
18
     * @var Balloon[]
19
     */
20
    private $composerConfigManager = [];
21
22
    /**
23
     * @var Executor
24
     */
25
    private $executor;
26
27
    /**
28
     * @var BalloonFactory
29
     */
30
    private $balloonFactory;
31
32
    /**
33
     * @param Executor $executor
34
     * @param BalloonFactory $balloonFactory
35
     */
36 28
    public function __construct(Executor $executor, BalloonFactory $balloonFactory)
37
    {
38 28
        $this->setExecutor($executor);
39 28
        $this->setBalloonFactory($balloonFactory);
40 28
    }
41
42
    /**
43
     * @param Project $project
44
     * @param array $options
45
     * @return int
46
     */
47 9
    public function createProject(Project $project, array $options = array())
48
    {
49 9
        if(!$project->getBootstrap()){
50 1
            throw new InvalidArgumentException('The bootstrap of the project is not defined');
51
        }
52
53 8
        return $this->execute(
54 8
            trim(
55 8
                sprintf(
56 8
                    'composer create-project --prefer-dist %s %s %s',
57 8
                    $project->getBootstrap()->getPackage(),
58 8
                    $project->getDirectoryPath() ? '"' . $project->getDirectoryPath() .'"' : '',
59 8
                    $project->getBootstrap()->getVersion()
60 8
                )
61 8
            )
62 8
            .$this->mapOptions($options)
63 8
        );
64
    }
65
66
    /**
67
     * @param string $name
68
     * @param string $version
69
     * @param bool $isGlobal
70
     * @param array $options
71
     * @return int
72
     */
73 4 View Code Duplication
    public function requirePackage($name, $version = '', $isGlobal = false, array $options = array())
74
    {
75 4
        $global = $isGlobal ? 'global ' : '';
76 4
        return $this->execute(
77 4
            trim(sprintf('composer %srequire %s %s', $global, $name, $version))
78 4
            . $this->mapOptions($options)
79 4
        );
80
    }
81
82
    /**
83
     * @param $name
84
     * @param bool $isGlobal
85
     * @param array $options
86
     * @return int
87
     */
88 3 View Code Duplication
    public function updatePackage($name, $isGlobal = false, array $options = array())
89
    {
90 3
        $global = $isGlobal ? 'global ' : '';
91 3
        return $this->execute(
92 3
            trim(sprintf('composer %supdate %s', $global, $name))
93 3
            . $this->mapOptions($options)
94 3
        );
95
    }
96
97
    /**
98
     * @param string $name
99
     * @param bool $isGlobal
100
     * @param array $options
101
     * @return int
102
     */
103 3 View Code Duplication
    public function removePackage($name, $isGlobal = false, array $options = array())
104
    {
105 3
        $global = $isGlobal ? 'global ' : '';
106 3
        return $this->execute(
107 3
            trim(sprintf('composer %sremove %s', $global, $name))
108 3
            . $this->mapOptions($options)
109 3
        );
110
    }
111
112
    /**
113
     * @return string
114
     */
115 4
    public function getHomePath()
116
    {
117 4
        return $this->getExecutor()->read('composer config home --global --absolute');
118
    }
119
120
    /**
121
     * @param $cwd
122
     * @return string
123
     */
124 4
    public function getConfigPath($cwd = '')
125
    {
126 4
        if($cwd){
127 3
            $cwd = rtrim($cwd, '/') . '/';
128 3
        }
129 4
        return  $cwd . 'composer.json';
130
    }
131
132
    /**
133
     * @param string $cwd
134
     * @return array
135
     */
136 2
    public function getConfig($cwd = '')
137
    {
138 2
        return $this->getComposerConfigManager($cwd)->getAll();
139
    }
140
141
    /**
142
     * @param array $config
143
     * @param string $cwd
144
     * @return int
145
     */
146
    public function setConfig(array $config, $cwd = '')
147
    {
148
        return $this->getComposerConfigManager($cwd)->set($config);
149
    }
150
151
    /**
152
     * @param string $cwd
153
     * @return int
154
     */
155
    public function flushConfig($cwd = '')
156
    {
157
        return $this->getComposerConfigManager($cwd)->flush();
158
    }
159
160
    /**
161
     * @param string $cwd
162
     * @return int
163
     */
164 2
    public function validateConfig($cwd = '')
165
    {
166 2
        return $this->execute('composer validate', $cwd);
167
    }
168
169
    /**
170
     * @param string $cwd
171
     * @return int
172
     */
173 1
    public function dumpAutoload($cwd = '')
174
    {
175 1
        return $this->execute('composer dump-autoload', $cwd);
176
    }
177
178
    /**
179
     * @param array $options
180
     * @return string
181
     */
182 18
    private function mapOptions(array $options)
183
    {
184 18
        $result = '';
185 18
        foreach($options as $option => $value){
186 4
            $result .= ' --' . $option . '=' . $value;
187 18
        }
188 18
        return $result;
189
    }
190
191
    /**
192
     * Getter of $composerConfigManager
193
     *
194
     * @param $cwd
195
     * @return Balloon
196
     */
197 2
    public function getComposerConfigManager($cwd)
198
    {
199 2
        if(empty($this->composerConfigManager[$cwd])){
200 2
            $this->composerConfigManager[$cwd] = $this->getBalloonFactory()->create($this->getConfigPath($cwd));
201 2
        }
202 2
        return $this->composerConfigManager[$cwd];
203
    }
204
205
    /**
206
     * Getter of $executor
207
     *
208
     * @return Executor
209
     */
210 22
    private function getExecutor()
211
    {
212 22
        return $this->executor;
213
    }
214
215
    /**
216
     * Setter of $executor
217
     *
218
     * @param Executor $executor
219
     */
220 28
    private function setExecutor(Executor $executor)
221
    {
222 28
        $this->executor = $executor;
223 28
    }
224
225
    /**
226
     * Getter of $balloonFactory
227
     *
228
     * @return BalloonFactory
229
     */
230 2
    private function getBalloonFactory()
231
    {
232 2
        return $this->balloonFactory;
233
    }
234
235
    /**
236
     * Setter of $balloonFactory
237
     *
238
     * @param BalloonFactory $balloonFactory
239
     */
240 28
    private function setBalloonFactory(BalloonFactory $balloonFactory)
241
    {
242 28
        $this->balloonFactory = $balloonFactory;
243 28
    }
244
245
    /**
246
     * @param string $command
247
     * @param string $cwd
248
     * @return int
249
     */
250 21
    private function execute($command, $cwd = null)
251
    {
252 21
        $pipes = [];
253 21
        return $this->getExecutor()->flush($command, [STDIN, STDOUT, STDERR], $pipes, $cwd);
254
    }
255
}
256