Completed
Push — master ( f7aa2f...852d76 )
by Sébastien
04:02
created

Process::isRunning()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PjbServer\Tools\System;
4
5
use PjbServer\Tools\Exception;
6
use PjbServer\Tools\Exception\UnsupportedSystemException;
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\NullLogger;
9
10
11
12
class Process implements ProcessInterface
13
{
14
    const LINUX_STYLE = 'linux';
15
16
    /**
17
     * @var ProcessInterface
18
     */
19
    protected $process;
20
21
    /**
22
     * @var LoggerInterface
23
     */
24
    protected $logger;
25
26
    /**
27
     * Process constructor.
28
     * @param string $style
29
     * @param LoggerInterface|null $logger
30
     */
31 24
    public function __construct($style = self::LINUX_STYLE, LoggerInterface $logger = null)
32
    {
33 24
        if ($logger === null) {
34 24
            $logger = new NullLogger();
35 24
        }
36 24
        $this->process = $this->getProcess($style, $logger);
37 24
        $this->logger = $logger;
38 24
    }
39
40
    /**
41
     * Create "internal styled" process
42
     *
43
     * @param string $style
44
     * @param LoggerInterface $logger
45
     * @return ProcessInterface
46
     */
47 24
    protected function getProcess($style, LoggerInterface $logger)
48
    {
49
        switch ($style) {
50 24
            case self::LINUX_STYLE:
51 24
                $process = new Linux\LinuxProcess($logger);
52 24
                break;
53
            default:
54
                $msg = "System style '" . (string) $style . "' is not supported";
55
                throw new UnsupportedSystemException($msg);
56
        }
57 24
        return $process;
58
    }
59
60
    /**
61
     * Check whether a pid is running
62
     *
63
     * @throws Exception\InvalidArgumentException
64
     * @param int $pid
65
     * @return boolean
66
     */
67 16
    public function isRunning($pid)
68
    {
69 16
        return $this->process->isRunning($pid);
70
    }
71
72
    /**
73
     * Kill a process
74
     *
75
     * @throws Exception\InvalidArgumentException
76
     * @param int $pid
77
     * @param bool $wait
78
     * @return void
79
     */
80 14
    public function kill($pid, $wait = false)
81
    {
82 14
        return $this->process->kill($pid, $wait);
83
    }
84
}
85