Passed
Pull Request — 0.6.x (#198)
by Shinji
01:39
created

ProcessDescriptorCache::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the sj-i/php-profiler package.
5
 *
6
 * (c) sji <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace PhpProfiler\Inspector\Daemon\Searcher\Worker;
15
16
use PhpProfiler\Inspector\Daemon\Dispatcher\TargetProcessDescriptor;
17
18
final class ProcessDescriptorCache
19
{
20
    /** @var array<int, TargetProcessDescriptor> */
21
    private $cache = [];
22
23
    public function set(TargetProcessDescriptor $target_process_descriptor): void
24
    {
25
        $this->cache[$target_process_descriptor->pid] = $target_process_descriptor;
26
    }
27
28
    public function setInvalid(int $pid): void
29
    {
30
        $this->cache[$pid] = TargetProcessDescriptor::getInvalid();
31
    }
32
33
    public function get(int $pid): ?TargetProcessDescriptor
34
    {
35
        return $this->cache[$pid] ?? null;
36
    }
37
38
    public function removeDisappeared(int ...$pids): void
39
    {
40
        $cached = array_keys($this->cache);
41
        $diff = array_diff($cached, $pids);
42
        foreach ($diff as $disappeared) {
43
            unset($this->cache[$disappeared]);
44
        }
45
    }
46
}
47