JobProcess::getPhpBinary()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 0
1
<?php
2
3
namespace yiicod\jobqueue;
4
5
use Illuminate\Queue\Jobs\Job;
6
use SfCod\QueueBundle\Job\MongoJob;
7
use Symfony\Component\Process\Process;
8
use Yii;
9
10
/**
11
 * Class JobProcess
12
 *
13
 * @author Orlov Alexey <[email protected]>
14
 *
15
 * @package SfCod\QueueBundle
16
 */
17
class JobProcess
18
{
19
    /**
20
     * @var string
21
     */
22
    public $binPath;
23
24
    /**
25
     * @var string
26
     */
27
    public $scriptName;
28
29
    /**
30
     * @var string
31
     */
32
    public $binary;
33
34
    /**
35
     * @var string
36
     */
37
    public $binaryArgs;
38
39
    public function __construct(
40
        string $scriptName = 'yii',
41
        string $binPath = null,
42
        string $binary = 'php',
43
        string $binaryArgs = '')
44
    {
45
        $this->scriptName = $scriptName;
46
        $this->binPath = $binPath;
47
        $this->binary = $binary;
48
        $this->binaryArgs = $binaryArgs;
49
    }
50
51
    /**
52
     * Get the Artisan process for the job id.
53
     *
54
     * @param Job|MongoJob $job
55
     * @param string $connectionName
56
     *
57
     * @return Process
58
     */
59
    public function getProcess(Job $job, string $connectionName): Process
60
    {
61
        $cmd = '%s %s job-queue/process %s --connection=%s --queue=%s';
62
        $cmd = $this->getBackgroundCommand($cmd);
63
        $cmd = sprintf($cmd, $this->getPhpBinary(), $this->scriptName, (string)$job->getJobId(), $connectionName, $job->getQueue());
64
65
        $alias = '';
66
        if (is_null($this->binPath)) {
67
            if (file_exists(Yii::getAlias('@app/yii'))) {
68
                $alias = '@app';
69
            } elseif (file_exists(Yii::getAlias('@app/../yii'))) {
70
                $alias = '@app/../';
71
            }
72
        }
73
74
        return new Process($cmd, Yii::getAlias($this->binPath ?? $alias));
0 ignored issues
show
Bug introduced by
It seems like \Yii::getAlias($this->binPath ?? $alias) targeting yii\BaseYii::getAlias() can also be of type boolean; however, Symfony\Component\Process\Process::__construct() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
75
    }
76
77
    /**
78
     * @param $cmd
79
     *
80
     * @return string
81
     */
82
    protected function getBackgroundCommand(string $cmd): string
83
    {
84
        if (defined('PHP_WINDOWS_VERSION_BUILD')) {
85
            return 'start /B ' . $cmd . ' > NUL';
86
        } else {
87
            return $cmd . ' > /dev/null 2>&1 &';
88
        }
89
    }
90
91
    /**
92
     * Get the escaped PHP Binary from the configuration
93
     *
94
     * @return string
95
     */
96
    protected function getPhpBinary(): string
97
    {
98
        $path = $this->binary;
99
        if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
100
            $path = escapeshellarg($path);
101
        }
102
103
        $args = $this->binaryArgs;
104
        if (is_array($args)) {
105
            $args = implode(' ', $args);
106
        }
107
108
        return trim($path . ' ' . $args);
109
    }
110
}
111