GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( e5c60b...e71724 )
by Baptiste
02:14
created

UnixProcesses::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Server\Status\Server\Processes;
5
6
use Innmind\Server\Status\{
7
    Server\Processes,
8
    Server\Process,
9
    Server\Process\Pid,
10
    Server\Process\User,
11
    Server\Process\Command,
12
    Server\Process\Memory,
13
    Server\Cpu\Percentage,
14
    Exception\InformationNotAccessible
15
};
16
use Innmind\TimeContinuum\TimeContinuumInterface;
17
use Innmind\Immutable\{
18
    MapInterface,
19
    Str,
20
    StreamInterface,
21
    Sequence,
22
    Map
23
};
24
use Symfony\Component\Process\Process as SfProcess;
25
26
final class UnixProcesses implements Processes
27
{
28
    private $clock;
29
30 11
    public function __construct(TimeContinuumInterface $clock)
31
    {
32 11
        $this->clock = $clock;
33 11
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 1
    public function all(): MapInterface
39
    {
40 1
        return $this->parse(
41 1
            $this->run('ps aux')
42
        );
43
    }
44
45 1
    public function get(Pid $pid): Process
46
    {
47 1
        $flag = PHP_OS === 'Linux' ? 'q' : 'p';
48
49
        return $this
50 1
            ->parse($this->run(sprintf('ps ux -%s %s', $flag, $pid)))
51
            ->get($pid->toInt());
52
    }
53
54 2 View Code Duplication
    private function run(string $command): Str
55
    {
56 2
        $process = new SfProcess($command);
57 2
        $process->run();
58
59 2
        if (!$process->isSuccessful()) {
60 1
            throw new InformationNotAccessible;
61
        }
62
63 1
        return new Str($process->getOutput());
64
    }
65
66
    /**
67
     * @return MapInterface<int, Process>
68
     */
69 1
    private function parse(Str $output): MapInterface
70
    {
71
        $lines = $output
72 1
            ->trim()
73 1
            ->split("\n");
74
        $columns = $lines
75 1
            ->first()
76 1
            ->pregSplit('~ +~')
77 1
            ->reduce(
78 1
                new Sequence,
79
                static function(Sequence $columns, Str $column): Sequence {
80 1
                    return $columns->add((string) $column);
81 1
                }
82
            );
83
84
        return $lines
85 1
            ->drop(1)
86 1
            ->reduce(
87 1
                new Sequence,
88
                static function(Sequence $lines, Str $line) use ($columns): Sequence {
89 1
                    return $lines->add(
90 1
                        $line->pregSplit('~ +~', $columns->size())
91
                    );
92 1
                }
93
            )
94
            ->map(function(StreamInterface $parts) use ($columns): Process {
95 1
                return new Process(
96 1
                    new Pid((int) (string) $parts->get($columns->indexOf('PID'))),
97 1
                    new User((string) $parts->get($columns->indexOf('USER'))),
98 1
                    new Percentage((float) (string) $parts->get($columns->indexOf('%CPU'))),
99 1
                    new Memory((float) (string) $parts->get($columns->indexOf('%MEM'))),
100 1
                    $this->clock->at(
101 1
                        (string) $parts->get(
102 1
                            $columns->indexOf(PHP_OS === 'Linux' ? 'START' : 'STARTED')
103
                        )
104
                    ),
105 1
                    new Command((string) $parts->get($columns->indexOf('COMMAND')))
106
                );
107 1
            })
108 1
            ->reduce(
109 1
                new Map('int', Process::class),
110 1
                static function(Map $processes, Process $process): Map {
111 1
                    return $processes->put(
112 1
                        $process->pid()->toInt(),
113 1
                        $process
114
                    );
115 1
                }
116
            );
117
    }
118
}
119