Supervisor::getProcessId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace MyOnlineStore\Bundle\RabbitMqManagerBundle\Supervisor;
4
5
use MyOnlineStore\Bundle\RabbitMqManagerBundle\Exception\Supervisor\SupervisorAlreadyRunningException;
6
use MyOnlineStore\Bundle\RabbitMqManagerBundle\Process\ProcessBuilderFactoryInterface;
7
use MyOnlineStore\Bundle\RabbitMqManagerBundle\Process\ProcessInterface;
8
9
final class Supervisor implements SupervisorInterface
10
{
11
    /**
12
     * @var ProcessBuilderFactoryInterface
13
     */
14
    private $processBuilderFactory;
15
16
    /**
17
     * @var string
18
     */
19
    private $path;
20
21
    /**
22
     * @param ProcessBuilderFactoryInterface $processBuilderFactory
23
     * @param array                          $config
24
     */
25 10
    public function __construct(ProcessBuilderFactoryInterface $processBuilderFactory, array $config)
26
    {
27 10
        $this->processBuilderFactory = $processBuilderFactory;
28 10
        $this->path = $config['path'];
29 10
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34 8
    public function isRunning()
35
    {
36 8
        $result = $this->execute('status')->getOutput();
37
38 8
        return !(false !== strpos($result, 'sock no such file') || false !== strpos($result, 'refused connection'));
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44 2
    public function start()
45
    {
46 2
        if ($this->isRunning()) {
47 1
            throw new SupervisorAlreadyRunningException('supervisor is already running.');
48
        }
49
50 1
        $processBuilder = $this->processBuilderFactory->create();
51 1
        $processBuilder->setWorkingDirectory($this->path);
52 1
        $processBuilder->setPrefix('supervisord');
53 1
        $processBuilder->add(sprintf('--configuration=%s/%s', $this->path, 'supervisord.conf'));
54 1
        $processBuilder->add(sprintf('--identifier=%s', sha1($this->path)));
55
56 1
        $processBuilder->getProcess()->run();
57 1
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62 2
    public function stop()
63
    {
64 2
        if (!$this->isRunning()) {
65 1
            return;
66
        }
67
68 1
        $this->execute('shutdown');
69 1
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 2
    public function reload()
75
    {
76 2
        if (!$this->isRunning()) {
77 1
            return;
78
        }
79
80 1
        $this->execute('reread');
81 1
        $this->execute('reload');
82 1
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87 1
    public function restart()
88
    {
89 1
        $this->execute('restart');
90 1
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 1
    public function getProcessId() {
96
97 1
        return (int) $this->execute('pid')->getOutput();
98
    }
99
100
    /**
101
     * @param string $cmd supervisorctl command
102
     *
103
     * @return ProcessInterface
104
     */
105 10
    private function execute($cmd)
106
    {
107 10
        $processBuilder = $this->processBuilderFactory->create();
108 10
        $processBuilder->setWorkingDirectory($this->path);
109 10
        $processBuilder->setPrefix('supervisorctl');
110 10
        $processBuilder->add(sprintf('--configuration=%s/%s', $this->path, 'supervisord.conf'));
111 10
        $processBuilder->add($cmd);
112
113 10
        $process = $processBuilder->getProcess();
114
115 10
        $process->run();
116 10
        $process->wait();
117
118 10
        return $process;
119
    }
120
}
121