Completed
Push — master ( 6bf413...bf3a2c )
by Alexey
37:40
created

Process.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace yiicod\socketio;
4
5
use Yii;
6
use yii\helpers\HtmlPurifier;
7
8
/**
9
 * Class Process
10
 *
11
 * @package SfCod\SocketIoBundle
12
 */
13
14
/**
15
 * Class Process
16
 *
17
 * @package yiicod\socketio
18
 */
19
class Process
20
{
21
    /**
22
     * @var array
23
     */
24
    private static $_inWork = [];
25
26
    /**
27
     * @var
28
     */
29
    public $yiiAlias;
30
31
    /**
32
     * @return int
33
     */
34
    public function getParallelEnv(): int
35
    {
36
        return getenv('SOCKET_IO.PARALLEL') ? getenv('SOCKET_IO.PARALLEL') : 10;
37
    }
38
39
    /**
40
     * Run process. If more then limit then wait and try run process on more time.
41
     *
42
     * @param string $handle
43
     * @param array $data
44
     *
45
     * @return \Symfony\Component\Process\Process
46
     */
47
    public function run(string $handle, array $data)
48
    {
49
        $this->inWork();
50
51
        while (count(self::$_inWork) >= $this->getParallelEnv()) {
52
            usleep(100);
53
54
            $this->inWork();
55
        }
56
57
        return $this->push($handle, $data);
58
    }
59
60
    /**
61
     * In work processes
62
     */
63
    private function inWork()
64
    {
65
        foreach (self::$_inWork as $i => $proccess) {
66
            if (false === $proccess->isRunning()) {
67
                unset(self::$_inWork[$i]);
68
            }
69
        }
70
    }
71
72
    /**
73
     * Create cmd process and push to queue.
74
     *
75
     * @param string $handle
76
     * @param array $data
77
     *
78
     * @return \Symfony\Component\Process\Process
79
     */
80
    private function push(string $handle, array $data): \Symfony\Component\Process\Process
81
    {
82
        $cmd = HtmlPurifier::process(sprintf('php yii socketio/process %s %s', escapeshellarg($handle), escapeshellarg(serialize($data))));
83
84
        if (is_null($this->yiiAlias)) {
85
            if (file_exists(Yii::getAlias('@app/yii'))) {
86
                $this->yiiAlias = '@app';
87
            } elseif (file_exists(Yii::getAlias('@app/../yii'))) {
88
                $this->yiiAlias = '@app/../';
89
            }
90
        }
91
92
        $process = new \Symfony\Component\Process\Process($cmd, Yii::getAlias($this->yiiAlias));
0 ignored issues
show
It seems like $cmd defined by \yii\helpers\HtmlPurifie...arg(serialize($data)))) on line 82 can also be of type false or null; however, Symfony\Component\Process\Process::__construct() does only seem to accept string|array, 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...
93
        $process->setTimeout(10);
94
        $process->start();
95
96
        self::$_inWork[] = $process;
97
98
        return $process;
99
    }
100
}
101