Supervisor::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 3
1
<?php
2
3
namespace Ivan1986\SupervisorBundle\Service;
4
5
use Symfony\Bundle\TwigBundle\TwigEngine;
6
use Symfony\Component\Process\Process;
7
8
class Supervisor
9
{
10
    /** @var TwigEngine */
11
    protected $templating;
12
    private $appDir;
13
    private $name;
14
15
    public function __construct($templating, $appDir, $name)
16
    {
17
        $this->templating = $templating;
18
        $this->appDir = $appDir;
19
        $this->name = $name ? (' -i '.$name) : '';
20
    }
21
22
    /**
23
     * Сгенерировать конфиг для программы
24
     *
25
     * @param $fileName string file in app/supervisor dir
26
     * @param $vars array array(name,command,[numprocs])
27
     * @param bool $template string non default template
28
     */
29
    public function genProgrammConf($fileName, $vars, $template = false)
30
    {
31
        $template = $template?:'@Supervisor/programm.conf.twig';
32
        $content = $this->templating->render($template, $vars);
0 ignored issues
show
Bug introduced by
It seems like $template defined by $template ?: '@Supervisor/programm.conf.twig' on line 31 can also be of type boolean; however, Symfony\Bundle\TwigBundle\TwigEngine::render() does only seem to accept string|object<Symfony\Co...lateReferenceInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
33
        file_put_contents($this->appDir.'/supervisor/'.$fileName.'.conf', $content);
34
    }
35
36
    /**
37
     * Выполняет команду супервизора
38
     *
39
     * @param string $cmd string supervisorctl command
40
     * @return Process Завершенный процесс
41
     */
42
    public function execute($cmd)
43
    {
44
        $p = new Process("supervisorctl ".$cmd);
45
        $p->setWorkingDirectory($this->appDir);
46
        $p->run();
47
        $p->wait();
48
        return $p;
49
    }
50
51
    /**
52
     * Перечитать конфиг и перезапустить процессы
53
     */
54
    public function reloadAndUpdate()
55
    {
56
        $this->execute('reread');
57
        $this->execute('update');
58
    }
59
60
    /**
61
     * Запустить демона, если он не работает
62
     */
63
    public function run()
64
    {
65
        $result = $this->execute('status')->getOutput();
66
        if (strpos($result, 'sock no such file') || strpos($result, 'refused connection')) {
67
            $p = new Process('supervisord'.$this->name);
68
            $p->setWorkingDirectory($this->appDir);
69
            $p->run();
70
        }
71
    }
72
}
73