ServiceTask::run()   C
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 46
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 37
nc 7
nop 1
1
<?php
2
3
/**
4
 * This file is part of Bldr.io
5
 *
6
 * (c) Aaron Scherer <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE
10
 */
11
12
namespace Bldr\Block\Miscellaneous\Task;
13
14
use Bldr\Block\Execute\Task\ExecuteTask;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * @author Aaron Scherer <[email protected]>
19
 */
20
class ServiceTask extends ExecuteTask
21
{
22
    private static $MANAGERS = ['service', 'init.d', 'launchctl'];
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    public function configure()
28
    {
29
        $this->setName('service')
30
            ->setDescription('Used to stop/start/restart services')
31
            ->addParameter(
32
                'manager',
33
                true,
34
                sprintf(
35
                    'Method used to manage service: %s',
36
                    implode(', ', static::$MANAGERS)
37
                ),
38
                'service'
39
            )
40
            ->addParameter('service', true, 'Service to manage')
41
            ->addParameter('method', true, 'Method to run on the service manager. <restart>', 'restart')
42
            ->addParameter('sudo', true, 'Run as sudo?', true)
43
            ->addParameter('dry_run', true, 'If set, will not run command', false)
44
        ;
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50
    public function run(OutputInterface $output)
51
    {
52
        $arguments  = [];
53
        $sudo       = $this->getParameter('sudo');
54
        $manager    = $this->getParameter('manager');
55
        switch ($manager) {
56
            case 'service':
57
                $executable = $manager;
58
                if ($sudo) {
59
                    $executable  = 'sudo';
60
                    $arguments[] = $manager;
61
                }
62
                $arguments[] = $this->getParameter('service');
63
                $arguments[] = $this->getParameter('method');
64
                break;
65
            case 'init.d':
66
                $executable = '/etc/init.d/'.$this->getParameter('service');
67
                if ($sudo) {
68
                    $executable  = 'sudo';
69
                    $arguments[] = $manager;
70
                }
71
                $arguments[] = $this->getParameter('method');
72
                break;
73
            case 'launchctl':
74
                $executable = $manager;
75
                if ($sudo) {
76
                    $executable  = 'sudo';
77
                    $arguments[] = $manager;
78
                }
79
                $arguments[] = $this->getParameter('method');
80
                $arguments[] = $this->getParameter('service');
81
                break;
82
            default:
83
                throw new \Exception(
84
                    $manager.' is not a valid manager for services. Feel free to '.
85
                    'create a pull request, if you think it should be.'
86
                );
87
        }
88
89
        $this->addParameter('executable', true)
0 ignored issues
show
Bug introduced by
The method addParameter cannot be called on $this->addParameter('exe...ecutable', $executable) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
90
            ->setParameter('executable', $executable)
91
            ->addParameter('arguments', true)
92
            ->setParameter('arguments', $arguments);
93
94
        parent::run($output);
95
    }
96
}
97