Completed
Push — master ( 799de4...6f590f )
by Aitor
10s
created

SystemdCommandBuilder::getCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of RoboSystemService.
4
 *
5
 * @author      Aitor García Martínez (Falc) <[email protected]>
6
 * @copyright   2015 Aitor García Martínez (Falc) <[email protected]>
7
 *
8
 * @author      Polyvaniy Oleksii (alexndlm) <[email protected]>
9
 * @copyright   2016 Polyvaniy Oleksii (alexndlm) <[email protected]>
10
 *
11
 * @license     MIT
12
 */
13
14
namespace Falc\Robo\Service\CommandBuilder;
15
16
/**
17
 * Systemd command builder.
18
 */
19
class SystemdCommandBuilder implements CommandBuilderInterface
20
{
21
    /**
22
     * Command action.
23
     *
24
     * @var string
25
     */
26
    protected $action;
27
28
    /**
29
     * Option list.
30
     *
31
     * @var string[]
32
     */
33
    protected $options = [];
34
35
    /**
36
     * Service.
37
     *
38
     * @var string
39
     */
40
    protected $service;
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 3
    public function start($service)
46
    {
47 3
        if (empty($service)) {
48 1
            throw new \InvalidArgumentException('No service selected to be started');
49
        }
50
51 2
        $this->service = $service;
52 2
        $this->action = 'start';
53
54 2
        return $this;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2 View Code Duplication
    public function stop($service)
61
    {
62 2
        if (empty($service)) {
63 1
            throw new \InvalidArgumentException('No service selected to be stopped');
64
        }
65
66 1
        $this->service = $service;
67 1
        $this->action = 'stop';
68
69 1
        return $this;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 2
    public function restart($service)
76
    {
77 2
        if (empty($service)) {
78 1
            throw new \InvalidArgumentException('No service selected to be restarted');
79
        }
80
81 1
        $this->service = $service;
82 1
        $this->action = 'restart';
83
84 1
        return $this;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 2
    public function reload($service)
91
    {
92 2
        if (empty($service)) {
93 1
            throw new \InvalidArgumentException('No service selected to be reloaded');
94
        }
95
96 1
        $this->service = $service;
97 1
        $this->action = 'reload';
98
99 1
        return $this;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 2
    public function enable($service)
106
    {
107 2
        if (empty($service)) {
108 1
            throw new \InvalidArgumentException('No service selected to be enabled');
109
        }
110
111 1
        $this->service = $service;
112 1
        $this->action = 'enable';
113
114 1
        return $this;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 2 View Code Duplication
    public function disable($service)
121
    {
122 2
        if (empty($service)) {
123 1
            throw new \InvalidArgumentException('No service selected to be disabled');
124
        }
125
126 1
        $this->service = $service;
127 1
        $this->action = 'disable';
128
129 1
        return $this;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 1
    public function daemonReload()
136
    {
137 1
        $this->action = 'daemon-reload';
138
139 1
        return $this;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 1
    public function quiet()
146
    {
147 1
        $this->options[] = '-q';
148
149 1
        return $this;
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 8
    public function getCommand()
156
    {
157 8
        $options = implode(' ', array_unique($this->options));
158
159 8
        $command = "systemctl {$options} {$this->action} {$this->service}";
160
161
        // Remove extra whitespaces
162 8
        $command = preg_replace('/\s+/', ' ', trim($command));
163
164 8
        return $command;
165
    }
166
}
167