Code Duplication    Length = 100-100 lines in 2 locations

src/CommandBuilder/AptCommandBuilder.php 1 location

@@ 15-114 (lines=100) @@
12
/**
13
 * Apt command builder.
14
 */
15
class AptCommandBuilder implements CommandBuilderInterface
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
    public function install(array $packages)
42
    {
43
        if (empty($packages)) {
44
            throw new \Exception('No packages selected to be installed');
45
        }
46
47
        $this->packages = $packages;
48
        $this->action = 'install';
49
50
        return $this;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function update(array $packages = [])
57
    {
58
        $this->packages = $packages;
59
        $this->action = empty($packages) ? 'upgrade' : 'install --only-upgrade';
60
61
        return $this;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function uninstall(array $packages)
68
    {
69
        if (empty($packages)) {
70
            throw new \Exception('No packages selected to be uninstalled');
71
        }
72
73
        $this->packages = $packages;
74
        $this->action = 'remove';
75
76
        return $this;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function assumeYes()
83
    {
84
        $this->options[] = '-y';
85
86
        return $this;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function quiet()
93
    {
94
        $this->options[] = '-qq';
95
96
        return $this;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getCommand()
103
    {
104
        $packages = implode(' ', array_unique($this->packages));
105
        $options = implode(' ', array_unique($this->options));
106
107
        $command = "apt-get {$options} {$this->action} {$packages}";
108
109
        // Remove extra whitespaces
110
        $command = preg_replace('/\s+/', ' ', trim($command));
111
112
        return $command;
113
    }
114
}
115

src/CommandBuilder/YumCommandBuilder.php 1 location

@@ 15-114 (lines=100) @@
12
/**
13
 * Yum command builder.
14
 */
15
class YumCommandBuilder implements CommandBuilderInterface
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
    public function install(array $packages)
42
    {
43
        if (empty($packages)) {
44
            throw new \Exception('No packages selected to be installed');
45
        }
46
47
        $this->packages = $packages;
48
        $this->action = 'install';
49
50
        return $this;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function update(array $packages = [])
57
    {
58
        $this->packages = $packages;
59
        $this->action = 'update';
60
61
        return $this;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function uninstall(array $packages)
68
    {
69
        if (empty($packages)) {
70
            throw new \Exception('No packages selected to be uninstalled');
71
        }
72
73
        $this->packages = $packages;
74
        $this->action = 'remove';
75
76
        return $this;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function assumeYes()
83
    {
84
        $this->options[] = '-y';
85
86
        return $this;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function quiet()
93
    {
94
        $this->options[] = '-q';
95
96
        return $this;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getCommand()
103
    {
104
        $packages = implode(' ', array_unique($this->packages));
105
        $options = implode(' ', array_unique($this->options));
106
107
        $command = "yum {$options} {$this->action} {$packages}";
108
109
        // Remove extra whitespaces
110
        $command = preg_replace('/\s+/', ' ', trim($command));
111
112
        return $command;
113
    }
114
}
115