Completed
Push — master ( 852d76...8d66d4 )
by Sébastien
03:25
created

Process   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 73
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A isRunning() 0 4 1
A kill() 0 4 1
A getProcess() 0 12 2
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 25
    public function __construct($style = self::LINUX_STYLE, LoggerInterface $logger = null)
32
    {
33 25
        if ($logger === null) {
34 25
            $logger = new NullLogger();
35 25
        }
36 25
        $this->process = $this->getProcess($style, $logger);
37 25
        $this->logger = $logger;
38 25
    }
39
40
    /**
41
     * Create "internal styled" process
42
     *
43
     * @param string $style
44
     * @param LoggerInterface $logger
45
     * @return ProcessInterface
46
     */
47 25
    protected function getProcess($style, LoggerInterface $logger)
48
    {
49
        switch ($style) {
50 25
            case self::LINUX_STYLE:
51 25
                $process = new Linux\LinuxProcess($logger);
52 25
                break;
53 1
            default:
54 1
                $msg = "System style '" . (string) $style . "' is not supported";
55 1
                throw new UnsupportedSystemException($msg);
56 1
        }
57 25
        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