Process::stop()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
namespace PHPieces\Framework\Util;
4
5
class Process
6
{
7
    private $pid;
8
    private $command;
9
10
    public function __construct($cl = false)
11
    {
12
        if ($cl != false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
13
            $this->command = $cl;
14
            $this->runCom();
15
        }
16
    }
17
18
    private function runCom()
19
    {
20
        $command = 'nohup '.$this->command.' > /dev/null 2>&1 & echo $!';
21
        exec($command, $op);
22
        $this->pid = (int)$op[0];
23
    }
24
25
    public function setPid($pid)
26
    {
27
        $this->pid = $pid;
28
    }
29
30
    public function getPid()
31
    {
32
        return $this->pid;
33
    }
34
35
    public function status()
36
    {
37
        $command = 'ps -p '.$this->pid;
38
        exec($command, $op);
39
        if (!isset($op[1])) {
40
            return false;
41
        } else {
42
            return true;
43
        }
44
    }
45
46
    public function start()
47
    {
48
        if ($this->command != '') {
49
            $this->runCom();
50
        } else {
51
            return true;
52
        }
53
    }
54
55
    public function stop()
56
    {
57
        $command = 'kill '.$this->pid;
58
        exec($command);
59
        if ($this->status() == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
60
            return true;
61
        } else {
62
            return false;
63
        }
64
    }
65
}
66