AptCommandBuilder::update()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of RoboSystemPackage.
4
 *
5
 * @author      Aitor García Martínez (Falc) <[email protected]>
6
 * @copyright   2015 Aitor García Martínez (Falc) <[email protected]>
7
 * @license     MIT
8
 */
9
10
namespace Falc\Robo\Package\CommandBuilder;
11
12
/**
13
 * Apt command builder.
14
 */
15 View Code Duplication
class AptCommandBuilder implements CommandBuilderInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
{
17
    /**
18
     * Command action.
19
     *
20
     * @var string
21
     */
22
    protected $action;
23
24
    /**
25
     * Option list.
26
     *
27
     * @var string[]
28
     */
29
    protected $options = [];
30
31
    /**
32
     * Package list.
33
     *
34
     * @var string[]
35
     */
36
    protected $packages = [];
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 5
    public function install(array $packages)
42
    {
43 5
        if (empty($packages)) {
44 1
            throw new \Exception('No packages selected to be installed');
45
        }
46
47 4
        $this->packages = $packages;
48 4
        $this->action = 'install';
49
50 4
        return $this;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 3
    public function update(array $packages = [])
57
    {
58 3
        $this->packages = $packages;
59 3
        $this->action = empty($packages) ? 'upgrade' : 'install --only-upgrade';
60
61 3
        return $this;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 3
    public function uninstall(array $packages)
68
    {
69 3
        if (empty($packages)) {
70 1
            throw new \Exception('No packages selected to be uninstalled');
71
        }
72
73 2
        $this->packages = $packages;
74 2
        $this->action = 'remove';
75
76 2
        return $this;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 1
    public function assumeYes()
83
    {
84 1
        $this->options[] = '-y';
85
86 1
        return $this;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 1
    public function quiet()
93
    {
94 1
        $this->options[] = '-qq';
95
96 1
        return $this;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 9
    public function getCommand()
103
    {
104 9
        $packages = implode(' ', array_unique($this->packages));
105 9
        $options = implode(' ', array_unique($this->options));
106
107 9
        $command = "apt-get {$options} {$this->action} {$packages}";
108
109
        // Remove extra whitespaces
110 9
        $command = preg_replace('/\s+/', ' ', trim($command));
111
112 9
        return $command;
113
    }
114
}
115