Issues (61)

src/Commands/RunCommandInBackground.php (6 issues)

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 $before is correct as it would always require null to be passed?
Loading history...
29
     * @param null   $after
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...
30
     */
31 5
    public function __construct($command, $before = null, $after = null)
32
    {
33 5
        $this->command = $command;
34 5
        $this->before = $before;
35 5
        $this->after = $after;
36 5
        $this->phpBinary = (new PhpExecutableFinder())->find();
37 5
    }
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 5
    public static function factory($command, $before = null, $after = null)
49
    {
50 5
        return new self($command, $before, $after);
0 ignored issues
show
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...
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 5
    public function runInBackground()
59
    {
60 5
        exec($this->composeForRunInBackground(), $out, $pid);
61
62 5
        return count($out) ? (int) $out[0] : null;
63
    }
64
65
    /**
66
     * Prepare command for run ib background
67
     *
68
     * @return string
69
     */
70 5
    protected function composeForRunInBackground()
71
    {
72 5
        return "({$this->composeForRun()}) > /dev/null 2>&1 & echo $!";
73
    }
74
75
    /**
76
     * Prepare command for run
77
     *
78
     * @return string
79
     */
80 5
    protected function composeForRun()
81
    {
82 5
        $parts = [];
83 5
        if (!empty($this->before)) {
84
            $parts[] = (string) $this->before;
85
        }
86 5
        $parts[] = 'cd '.base_path();
0 ignored issues
show
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 5
        $parts[] = "{$this->phpBinary} {$this->getArtisan()} {$this->command}";
88 5
        if (!empty($this->after)) {
89
            $parts[] = (string) $this->after;
90
        }
91
92 5
        return implode(' && ', $parts);
93
    }
94
95
    /**
96
     * Get artisan
97
     *
98
     * @return string
99
     */
100 5
    protected function getArtisan()
101
    {
102 5
        return defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan';
0 ignored issues
show
The constant sonrac\WAMP\Commands\ARTISAN_BINARY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
103
    }
104
}
105