Passed
Push — master ( bf5028...3a1f66 )
by Radu
03:00
created

Runner::isRunning()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
namespace WebServCo\Framework\Cli\Runner;
3
4
final class Runner implements \WebServCo\Framework\Interfaces\CliRunnerInterface
5
{
6
    protected $pid;
7
    protected $statistics;
8
    protected $workDir;
9
10
    public function __construct($workDir)
11
    {
12
        if (!is_readable($workDir)) {
13
            throw new \WebServCo\Framework\Exceptions\ApplicationException('Working directory not readable');
14
        }
15
16
        $this->statistics = new Statistics();
17
        $this->workDir = $workDir;
18
    }
19
20
    public function finish()
21
    {
22
        $this->statistics->finish();
23
        if (empty($this->pid) || !is_file($this->pid) || !is_readable($this->pid)) {
24
            return false;
25
        }
26
        unlink($this->pid);
27
        $this->pid = null;
28
        return true;
29
    }
30
31
    public function getPid()
32
    {
33
        if (!$this->isRunning()) {
34
            return false;
35
        }
36
        return $this->pid;
37
    }
38
39
    public function getStatistics()
40
    {
41
        return $this->statistics;
42
    }
43
44
    public function isRunning()
45
    {
46
        if (empty($this->pid)) {
47
            return false;
48
        }
49
        return is_readable($this->pid);
50
    }
51
52
    public function start()
53
    {
54
        $this->statistics->start();
55
        $this->pid = sprintf(
56
            '%s%s%s.pid',
57
            realpath($this->workDir),
58
            DIRECTORY_SEPARATOR,
59
            microtime(true)
60
        );
61
        touch($this->pid);
62
        return true;
63
    }
64
}
65