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 ( a93f30...046bca )
by Baptiste
04:23
created

UnixProcesses::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
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 15
    public function __construct(TimeContinuumInterface $clock)
31
    {
32 15
        $this->clock = $clock;
33 15
    }
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 2
    public function get(Pid $pid): Process
46
    {
47
        try {
48 2
            $processes = $this->parse(
49 2
                $this->run(sprintf('ps ux -p %s', $pid))
50
            );
51
        } catch (InformationNotAccessible $e) {
52
            $processes = $this->parse(
53
                $this->run(sprintf('ps ux -q %s', $pid))
54
            );
55
        }
56
57 2
        if (!$processes->contains($pid->toInt())) {
58 1
            throw new InformationNotAccessible;
59
        }
60
61 1
        return $processes->get($pid->toInt());
62
    }
63
64 3 View Code Duplication
    private function run(string $command): Str
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66 3
        $process = new SfProcess($command);
67 3
        $process->run();
68
69 3
        if (!$process->isSuccessful()) {
70
            throw new InformationNotAccessible;
71
        }
72
73 3
        return new Str($process->getOutput());
74
    }
75
76
    /**
77
     * @return MapInterface<int, Process>
78
     */
79 3
    private function parse(Str $output): MapInterface
80
    {
81
        $lines = $output
82 3
            ->trim()
83 3
            ->split("\n");
84
        $columns = $lines
85 3
            ->first()
86 3
            ->pregSplit('~ +~')
87 3
            ->reduce(
88 3
                new Sequence,
89 3
                static function(Sequence $columns, Str $column): Sequence {
90 3
                    return $columns->add((string) $column);
91 3
                }
92
            );
93
94
        return $lines
95 3
            ->drop(1)
96 3
            ->reduce(
97 3
                new Sequence,
98 3
                static function(Sequence $lines, Str $line) use ($columns): Sequence {
99 3
                    return $lines->add(
100 3
                        $line->pregSplit('~ +~', $columns->size())
101
                    );
102 3
                }
103
            )
104 3
            ->map(function(StreamInterface $parts) use ($columns): Process {
105 3
                return new Process(
106 3
                    new Pid((int) (string) $parts->get($columns->indexOf('PID'))),
107 3
                    new User((string) $parts->get($columns->indexOf('USER'))),
108 3
                    new Percentage((float) (string) $parts->get($columns->indexOf('%CPU'))),
109 3
                    new Memory((float) (string) $parts->get($columns->indexOf('%MEM'))),
110 3
                    $this->clock->at(
111 3
                        (string) $parts->get(
112 3
                            $columns->indexOf(PHP_OS === 'Linux' ? 'START' : 'STARTED')
113
                        )
114
                    ),
115 3
                    new Command((string) $parts->get($columns->indexOf('COMMAND')))
116
                );
117 3
            })
118 3
            ->reduce(
119 3
                new Map('int', Process::class),
120 3
                static function(Map $processes, Process $process): Map {
121 3
                    return $processes->put(
122 3
                        $process->pid()->toInt(),
123 3
                        $process
124
                    );
125 3
                }
126
            );
127
    }
128
}
129