Completed
Pull Request — master (#1)
by
unknown
03:59
created

SysVinitCommandBuilder::stop()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
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   2016 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
 * SysVinit command builder.
18
 */
19
abstract class SysVinitCommandBuilder implements CommandBuilderInterface
20
{
21
    /**
22
     * Command name.
23
     *
24
     * @var string
25
     */
26
    protected $cmd;
27
28
    /**
29
     * Command action.
30
     *
31
     * @var string
32
     */
33
    protected $action;
34
35
    /**
36
     * Whether the command should be quiet or not.
37
     *
38
     * @var boolean
39
     */
40
    protected $quiet = false;
41
42
    /**
43
     * Service.
44
     *
45
     * @var string
46
     */
47
    protected $service;
48
49
    /**
50
     * Option list.
51
     *
52
     * @var string[]
53
     */
54
    protected $options = [];
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 6
    public function start($service)
60
    {
61 6
        if (empty($service)) {
62 2
            throw new \InvalidArgumentException('No service selected to be started');
63
        }
64
65 4
        $this->cmd = '/etc/init.d';
66 4
        $this->service = $service;
67 4
        $this->action = 'start';
68
69 4
        return $this;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 4 View Code Duplication
    public function stop($service)
0 ignored issues
show
Duplication introduced by
This method 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...
76
    {
77 4
        if (empty($service)) {
78 2
            throw new \InvalidArgumentException('No service selected to be started');
79
        }
80
81 2
        $this->cmd = '/etc/init.d';
82 2
        $this->service = $service;
83 2
        $this->action = 'stop';
84
85 2
        return $this;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 4
    public function restart($service)
92
    {
93 4
        if (empty($service)) {
94 2
            throw new \InvalidArgumentException('No service selected to be started');
95
        }
96
97 2
        $this->cmd = '/etc/init.d';
98 2
        $this->service = $service;
99 2
        $this->action = 'restart';
100
101 2
        return $this;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    abstract public function enable($service);
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    abstract public function disable($service);
113
114
    /**
115
     * {@inheritdoc}
116
     * @throws \BadMethodCallException
117
     */
118 2
    public function daemonReload()
119
    {
120 2
        throw new \BadMethodCallException('Action "daemon-reload" not supported for SysVinit');
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 2
    public function quiet()
127
    {
128 2
        $this->quiet = true;
129
130 2
        return $this;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 12
    public function getCommand()
137
    {
138 12
        $options = implode(' ', array_unique($this->options));
139
140 12
        $command = "{$this->cmd}/{$this->service} {$this->action} {$options}";
141
142 12
        if (!in_array($this->action, ['start', 'stop', 'restart'])) {
143 4
            $command = "{$this->cmd} {$options} {$this->service} {$this->action}";
144 4
        }
145
146 12
        if ($this->quiet === true) {
147 2
            $command = "{$command} > /dev/null";
148 2
        }
149
150
        // Remove extra whitespaces
151 12
        $command = preg_replace('/\s+/', ' ', trim($command));
152
153 12
        return $command;
154
    }
155
}
156