Background   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 144
rs 10
c 0
b 0
f 0
ccs 41
cts 41
cp 1
wmc 18
lcom 1
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 5
A init() 0 5 1
A start() 0 15 2
A stop() 0 11 2
A pid() 0 4 1
A getPid() 0 4 1
A getLog() 0 4 1
A setLog() 0 10 4
A getErrors() 0 4 1
1
<?php
2
3
namespace NwLaravel\Process;
4
5
use Symfony\Component\Process\Process;
6
use \RuntimeException;
7
use \Exception;
8
9
/**
10
 * Class Background
11
 */
12
class Background
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $errors;
18
19
    /**
20
     * @var string
21
     */
22
    protected $pid;
23
24
    /**
25
     * @var string
26
     */
27
    protected $log = '/dev/null';
28
29
    /**
30
     * Construct
31
     *
32
     * @param string $log String Log
33
     */
34 4
    public function __construct($log = null)
35
    {
36 4
        if ((file_exists($log) && is_writable($log)) ||
37 4
            (!file_exists($log) && is_writable(dirname($log)))
38 4
        ) {
39 1
            $this->setLog($log);
40 1
        }
41 4
    }
42
43
    /**
44
     * Initialize
45
     *
46
     * @return void
47
     */
48 2
    protected function init()
49
    {
50 2
        $this->pid = null;
51 2
        $this->errors = null;
52 2
    }
53
54
    /**
55
     * Start Process Background
56
     *
57
     * @param string $cmd String Cmd
58
     *
59
     * @return bool
60
     */
61 2
    public function start($cmd)
62
    {
63 2
        $this->init();
64
65 2
        $process = new Process($cmd.' >> '.$this->log.' 2>&1');
0 ignored issues
show
Documentation introduced by
$cmd . ' >> ' . $this->log . ' 2>&1' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66 2
        $process->disableOutput();
67 2
        $process->start();
68
69 2
        if ($process->isRunning()) {
70 2
            sleep(1);
71 2
            $this->pid = $this->pid($cmd);
72 2
        }
73
74 2
        return true;
75
    }
76
77
    /**
78
     * Stop Process Background
79
     *
80
     * @param string $cmd String Cmd
81
     *
82
     * @return bool
83
     */
84 1
    public function stop($cmd)
85
    {
86 1
        $pid = $this->pid($cmd);
87 1
        if ($pid) {
88 1
            exec(sprintf("kill -9 %s", $pid));
89 1
        }
90
91 1
        $this->pid = null;
92
93 1
        return true;
94
    }
95
96
    /**
97
     * Busca Pid do Cmd
98
     *
99
     * @param string $cmd String Cmd
100
     *
101
     * @return this
102
     */
103 2
    public function pid($cmd)
104
    {
105 2
        return exec(sprintf("ps -aefw | grep '%s' | grep -v ' grep ' | awk '{print $2}'", $cmd));
106
    }
107
108
    /**
109
     * Get Pid
110
     *
111
     * @return int
112
     */
113 1
    public function getPid()
114
    {
115 1
        return $this->pid;
116
    }
117
118
    /**
119
     * Gets the value of log.
120
     *
121
     * @return mixed
122
     */
123 1
    public function getLog()
124
    {
125 1
        return $this->log;
126
    }
127
128
    /**
129
     * Sets the value of log.
130
     *
131
     * @param mixed $log Mixed Log
132
     *
133
     * @return self
134
     */
135 2
    public function setLog($log)
136
    {
137 2
        if ($log != '/dev/null' && (!is_writable(dirname($log)) || !is_writable(dirname($log)))) {
138 1
            throw new RuntimeException(sprintf('Error: Path "%s" not writable.', $log));
139
        }
140
141 1
        $this->log = $log;
142
143 1
        return $this;
144
    }
145
146
    /**
147
     * Gets the value of error
148
     *
149
     * @return mixed
150
     */
151 1
    public function getErrors()
152
    {
153 1
        return $this->errors;
154
    }
155
}
156