Completed
Pull Request — master (#1)
by
unknown
05:54
created

SystemdCommandBuilder::disable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
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
 * @license     MIT
8
 */
9
10
namespace Falc\Robo\Service\CommandBuilder;
11
12
/**
13
 * Systemd command builder.
14
 */
15
class SystemdCommandBuilder 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
     * Service.
33
     *
34
     * @var string
35
     */
36
    protected $service;
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 3
    public function start($service)
42
    {
43 3
        if (empty($service)) {
44 1
            throw new \InvalidArgumentException('No service selected to be started');
45
        }
46
47 2
        $this->service = $service;
48 2
        $this->action = 'start';
49
50 2
        return $this;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 2 View Code Duplication
    public function stop($service)
57
    {
58 2
        if (empty($service)) {
59 1
            throw new \InvalidArgumentException('No service selected to be started');
60
        }
61
62 1
        $this->service = $service;
63 1
        $this->action = 'stop';
64
65 1
        return $this;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 2
    public function restart($service)
72
    {
73 2
        if (empty($service)) {
74 1
            throw new \InvalidArgumentException('No service selected to be started');
75
        }
76
77 1
        $this->service = $service;
78 1
        $this->action = 'restart';
79
80 1
        return $this;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 2
    public function enable($service)
87
    {
88 2
        if (empty($service)) {
89 1
            throw new \InvalidArgumentException('No service selected to be enabled');
90
        }
91
92 1
        $this->service = $service;
93 1
        $this->action = 'enable';
94
95 1
        return $this;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 2 View Code Duplication
    public function disable($service)
102
    {
103 2
        if (empty($service)) {
104 1
            throw new \InvalidArgumentException('No service selected to be disabled');
105
        }
106
107 1
        $this->service = $service;
108 1
        $this->action = 'disable';
109
110 1
        return $this;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 1
    public function daemonReload()
117
    {
118 1
        $this->action = 'daemon-reload';
119
120 1
        return $this;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 1
    public function quiet()
127
    {
128 1
        $this->options[] = '-q';
129
130 1
        return $this;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 7
    public function getCommand()
137
    {
138 7
        $options = implode(' ', array_unique($this->options));
139
140 7
        $command = "systemctl {$options} {$this->action} {$this->service}";
141
142
        // Remove extra whitespaces
143 7
        $command = preg_replace('/\s+/', ' ', trim($command));
144
145 7
        return $command;
146
    }
147
}
148