Process   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 18
c 2
b 0
f 0
dl 0
loc 76
ccs 20
cts 20
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getProcess() 0 12 2
A isRunning() 0 3 1
A __construct() 0 7 2
A kill() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PjbServer\Tools\System;
6
7
use PjbServer\Tools\Exception;
8
use PjbServer\Tools\Exception\UnsupportedSystemException;
9
use Psr\Log\LoggerInterface;
10
use Psr\Log\NullLogger;
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
     *
29
     * @param string               $style
30 25
     * @param LoggerInterface|null $logger
31
     */
32 25
    public function __construct(string $style = self::LINUX_STYLE, LoggerInterface $logger = null)
33 25
    {
34 25
        if ($logger === null) {
35 25
            $logger = new NullLogger();
36 25
        }
37 25
        $this->process = $this->getProcess($style, $logger);
38
        $this->logger = $logger;
39
    }
40
41
    /**
42
     * Create "internal styled" process.
43
     *
44
     * @param string          $style
45
     * @param LoggerInterface $logger
46
     *
47 25
     * @throws UnsupportedSystemException
48
     */
49
    protected function getProcess(string $style, LoggerInterface $logger): ProcessInterface
50 25
    {
51 25
        switch ($style) {
52 25
            case self::LINUX_STYLE:
53 1
                $process = new Linux\LinuxProcess($logger);
54 1
                break;
55 1
            default:
56 1
                $msg = "System style '" . $style . "' is not supported";
57
                throw new UnsupportedSystemException($msg);
58 25
        }
59
60
        return $process;
61
    }
62
63
    /**
64
     * Check whether a pid is running.
65
     *
66
     * @throws Exception\InvalidArgumentException
67
     *
68
     * @param int $pid
69
     */
70 16
    public function isRunning(int $pid): bool
71
    {
72 16
        return $this->process->isRunning($pid);
73
    }
74
75
    /**
76
     * Kill a process.
77
     *
78
     * @throws Exception\InvalidArgumentException
79
     *
80
     * @param int  $pid
81
     * @param bool $wait
82
     *
83
     * @return bool
84
     */
85 14
    public function kill(int $pid, bool $wait = false): bool
86
    {
87 14
        return $this->process->kill($pid, $wait);
88
    }
89
}
90