Passed
Push — master ( fb5c02...277033 )
by Radu
01:30
created

Runner   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 21
dl 0
loc 45
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A start() 0 10 1
A hasPid() 0 3 1
A getPid() 0 6 2
A finish() 0 9 3
1
<?php
2
namespace WebServCo\Framework\Cli;
3
4
final class Runner
5
{
6
    protected $workDir;
7
    protected $token;
8
    protected $pids = [];
9
10
    public function __construct($workDir)
11
    {
12
        $this->workDir = $workDir;
13
    }
14
15
    public function start($token)
16
    {
17
        $this->pids[$token] = sprintf(
18
            '%s%s%s.pid',
19
            realpath($this->workDir),
20
            DIRECTORY_SEPARATOR,
21
            md5($token)
22
        );
23
        touch($this->pids[$token]);
24
        return true;
25
    }
26
27
    public function hasPid($token)
28
    {
29
        return is_readable($this->pids[$token]);
30
    }
31
32
    public function getPid($token)
33
    {
34
        if (!$this->hasPid($token)) {
35
            return false;
36
        }
37
        return $this->pids[$token];
38
    }
39
40
    public function finish($token)
41
    {
42
        if (is_file($this->pids[$token]) &&
43
            is_readable($this->pids[$token])
44
        ) {
45
            unlink($this->pids[$token]);
46
            return true;
47
        }
48
        return false;
49
    }
50
}
51