Completed
Push — master ( bbaf98...054690 )
by Aitor
02:20
created

SysVinitCommandBuilder::getCommand()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.4285
cc 3
eloc 9
nc 4
nop 0
crap 3
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 View Code Duplication
    public function start($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...
56
    {
57 6
        if (empty($service)) {
58 2
            throw new \Exception('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 \Exception('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 View Code Duplication
    public function restart($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...
88
    {
89 4
        if (empty($service)) {
90 2
            throw new \Exception('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
     */
113 2
    public function quiet()
114
    {
115 2
        $this->quiet = true;
116
117 2
        return $this;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 12
    public function getCommand()
124
    {
125 12
        $options = implode(' ', array_unique($this->options));
126
127 12
        $command = "{$this->cmd}/{$this->service} {$this->action} {$options}";
128
129 12
        if (!in_array($this->action, ['start', 'stop', 'restart'])) {
130 4
            $command = "{$this->cmd} {$options} {$this->service} {$this->action}";
131 4
        }
132
133 12
        if ($this->quiet === true) {
134 2
            $command = "{$command} > /dev/null";
135 2
        }
136
137
        // Remove extra whitespaces
138 12
        $command = preg_replace('/\s+/', ' ', trim($command));
139
140 12
        return $command;
141
    }
142
}
143