Completed
Pull Request — master (#2)
by
unknown
38:19
created

JobProcess   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 93
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getProcess() 0 8 1
A getBackgroundCommand() 0 8 2
A getPhpBinary() 0 14 3
1
<?php
2
3
namespace SfCod\QueueBundle\Service;
4
5
use SfCod\QueueBundle\Job\JobContractInterface;
6
use Symfony\Component\Process\Process;
7
8
/**
9
 * Class JobProcess
10
 *
11
 * @author Virchenko Maksim <[email protected]>
12
 *
13
 * @package SfCod\QueueBundle
14
 */
15
class JobProcess
16
{
17
    /**
18
     * @var string
19
     */
20
    public $binPath;
21
22
    /**
23
     * @var string
24
     */
25
    public $scriptName;
26
27
    /**
28
     * @var string
29
     */
30
    public $binary;
31
32
    /**
33
     * @var string
34
     */
35
    public $binaryArgs;
36
37
    /**
38
     * JobProcess constructor.
39
     *
40
     * @param string $scriptName
41
     * @param string $binPath
42
     * @param string $binary
43
     * @param string $binaryArgs
44
     */
45
    public function __construct(
46
        string $scriptName,
47
        string $binPath,
48
        string $binary = 'php',
49
        string $binaryArgs = '')
50
    {
51
        $this->scriptName = $scriptName;
52
        $this->binPath = $binPath;
53
        $this->binary = $binary;
54
        $this->binaryArgs = $binaryArgs;
55
    }
56
57
    /**
58
     * Get the Artisan process for the job id.
59
     *
60
     * @param JobContractInterface $job
61
     * @param string $connectionName
62
     *
63
     * @return Process
64
     */
65
    public function getProcess(JobContractInterface $job, string $connectionName): Process
66
    {
67
        $cmd = '%s %s job-queue:run-job %s --connection=%s --queue=%s --env=%s';
68
        $cmd = $this->getBackgroundCommand($cmd);
69
        $cmd = sprintf($cmd, $this->getPhpBinary(), $this->scriptName, (string)$job->getJobId(), $connectionName, $job->getQueue(), getenv('APP_ENV'));
70
71
        return new Process($cmd, $this->binPath);
72
    }
73
74
    /**
75
     * @param $cmd
76
     *
77
     * @return string
78
     */
79
    protected function getBackgroundCommand(string $cmd): string
80
    {
81
        if (defined('PHP_WINDOWS_VERSION_BUILD')) {
82
            return 'start /B ' . $cmd . ' > NUL';
83
        } else {
84
            return $cmd . ' > /dev/null 2>&1 &';
85
        }
86
    }
87
88
    /**
89
     * Get the escaped PHP Binary from the configuration
90
     *
91
     * @return string
92
     */
93
    protected function getPhpBinary(): string
94
    {
95
        $path = $this->binary;
96
        if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
97
            $path = escapeshellarg($path);
98
        }
99
100
        $args = $this->binaryArgs;
101
        if (is_array($args)) {
102
            $args = implode(' ', $args);
103
        }
104
105
        return trim($path . ' ' . $args);
106
    }
107
}
108