Completed
Push — master ( 9bf1e4...223d51 )
by Sergii
04:53
created

composeForRunInBackground()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Donii Sergii <[email protected]>
4
 */
5
6
namespace sonrac\WAMP\Commands;
7
8
use Symfony\Component\Process\PhpExecutableFinder;
9
10
/**
11
 * Class RunCommandInBackground
12
 *
13
 * @package sonrac\WAMP\Commands
14
 *
15
 * @see     https://github.com/dmitry-ivanov/laravel-helper-functions
16
 */
17
class RunCommandInBackground
18
{
19
    protected $command = null;
20
    protected $before = null;
21
    protected $after = null;
22
    protected $phpBinary = null;
23
24
    /**
25
     * RunCommandInBackground constructor.
26
     *
27
     * @param string $command
28
     * @param null $before
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $after is correct as it would always require null to be passed?
Loading history...
Documentation Bug introduced by
Are you sure the doc-type for parameter $before is correct as it would always require null to be passed?
Loading history...
29
     * @param null $after
30
     */
31 1
    public function __construct($command, $before = null, $after = null)
32
    {
33 1
        $this->command = $command;
34 1
        $this->before = $before;
35 1
        $this->after = $after;
36 1
        $this->phpBinary = (new PhpExecutableFinder())->find();
37 1
    }
38
39
    /**
40
     * Create new command
41
     *
42
     * @param string      $command
43
     * @param string|null $before
44
     * @param string|null $after
45
     *
46
     * @return \sonrac\WAMP\Commands\RunCommandInBackground
47
     */
48 1
    public static function factory($command, $before = null, $after = null)
49
    {
50 1
        return new self($command, $before, $after);
0 ignored issues
show
Bug introduced by
It seems like $after can also be of type string; however, parameter $after of sonrac\WAMP\Commands\Run...ckground::__construct() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        return new self($command, $before, /** @scrutinizer ignore-type */ $after);
Loading history...
Bug introduced by
It seems like $before can also be of type string; however, parameter $before of sonrac\WAMP\Commands\Run...ckground::__construct() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        return new self($command, /** @scrutinizer ignore-type */ $before, $after);
Loading history...
51
    }
52
53
    /**
54
     * Run command in background and return process pid
55
     *
56
     * @return int|null
57
     */
58 1
    public function runInBackground()
59
    {
60 1
        exec($this->composeForRunInBackground(), $out, $pid);
61
62 1
        return count($out) ? (int) $out[0] : null;
63
    }
64
65
    /**
66
     * Prepare command for run ib background
67
     *
68
     * @return string
69
     */
70 1
    protected function composeForRunInBackground()
71
    {
72 1
        return "({$this->composeForRun()}) > /dev/null 2>&1 & echo $!";
73
    }
74
75
    /**
76
     * Prepare command for run
77
     *
78
     * @return string
79
     */
80 1
    protected function composeForRun()
81
    {
82 1
        $parts = [];
83 1
        if (!empty($this->before)) {
84
            $parts[] = (string)$this->before;
85
        }
86 1
        $parts[] = 'cd ' . base_path();
0 ignored issues
show
Bug introduced by
The function base_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
        $parts[] = 'cd ' . /** @scrutinizer ignore-call */ base_path();
Loading history...
87 1
        $parts[] = "{$this->phpBinary} {$this->getArtisan()} {$this->command}";
88 1
        if (!empty($this->after)) {
89
            $parts[] = (string)$this->after;
90
        }
91 1
        return implode(' && ', $parts);
92
    }
93
94
    /**
95
     * Get artisan
96
     *
97
     * @return string
98
     */
99 1
    protected function getArtisan()
100
    {
101 1
        return defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan';
0 ignored issues
show
Bug introduced by
The constant sonrac\WAMP\Commands\ARTISAN_BINARY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
102
    }
103
}