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

SysVinitCommandBuilder::restart()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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