Process::discover()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 3
eloc 9
c 2
b 1
f 0
nc 1
nop 0
dl 0
loc 11
ccs 10
cts 10
cp 1
crap 3
rs 9.9666
1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Events\Common;
4
5
use ZoiloMora\ElasticAPM\Helper\Encoding;
6
7
class Process implements \JsonSerializable
8
{
9
    /**
10
     * Process ID of the service
11
     *
12
     * @var int
13
     */
14
    private $pid;
15
16
    /**
17
     * Parent process ID of the service
18
     *
19
     * @var int|null
20
     */
21
    private $ppid;
22
23
    /**
24
     * @var string|null
25
     */
26
    private $title;
27
28
    /**
29
     * Command line arguments used to start this process
30
     *
31
     * @var array|null
32
     */
33
    private $argv;
34
35
    /**
36
     * @param int $pid
37
     * @param int|null $ppid
38
     * @param string|null $title
39
     * @param array|null $argv
40
     */
41 17
    public function __construct($pid, $ppid = null, $title = null, array $argv = null)
42
    {
43 17
        $this->assertArgv($argv);
44
45 16
        $this->pid = $pid;
46 16
        $this->ppid = $ppid;
47 16
        $this->title = $title;
48 16
        $this->argv = $argv;
49 16
    }
50
51
    /**
52
     * @return int
53
     */
54 1
    public function pid()
55
    {
56 1
        return $this->pid;
57
    }
58
59
    /**
60
     * @return int|null
61
     */
62 1
    public function ppid()
63
    {
64 1
        return $this->ppid;
65
    }
66
67
    /**
68
     * @return string|null
69
     */
70 1
    public function title()
71
    {
72 1
        return $this->title;
73
    }
74
75
    /**
76
     * @return array|null
77
     */
78 1
    public function argv()
79
    {
80 1
        return $this->argv;
81
    }
82
83
    /**
84
     * @return Process
85
     */
86 13
    public static function discover()
87
    {
88 13
        return new self(
89 13
            getmypid(),
90 13
            null,
91 13
            true === array_key_exists('_', $_SERVER)
92 13
                ? $_SERVER['_']
93 13
                : null,
94 13
            true === array_key_exists('argv', $_SERVER)
95 13
                ? $_SERVER['argv']
96 13
                : null
97 13
        );
98
    }
99
100
    /**
101
     * @param array|null $argv
102
     *
103
     * @return void
104
     *
105
     * @throws \InvalidArgumentException
106
     */
107 17
    private function assertArgv($argv)
108
    {
109 17
        if (null === $argv) {
110 1
            return;
111
        }
112
113 16
        foreach ($argv as $item) {
114 14
            if (false === is_string($item)) {
115 1
                throw new \InvalidArgumentException('All elements must be of string type.');
116
            }
117 15
        }
118 15
    }
119
120
    /**
121
     * @return array
122
     */
123 1
    public function jsonSerialize()
124
    {
125
        return [
126 1
            'pid' => $this->pid,
127 1
            'ppid' => $this->ppid,
128 1
            'title' => Encoding::keywordField($this->title),
129 1
            'argv' => $this->argv,
130 1
        ];
131
    }
132
}
133