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 ( ed3764...0d105b )
by Baptiste
02:23
created

Graphs   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 52
dl 0
loc 86
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A __invoke() 0 37 4
A load() 0 10 1
A open() 0 18 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\LabStation\Trigger;
5
6
use Innmind\LabStation\{
7
    Trigger,
8
    Activity,
9
    Activity\Type,
10
};
11
use Innmind\OperatingSystem\Filesystem;
12
use Innmind\Server\Control\Server\{
13
    Processes,
14
    Command,
15
};
16
use Innmind\CLI\{
17
    Environment,
18
    Question\Question,
19
};
20
use Innmind\Json\Json;
21
use Innmind\Url\PathInterface;
22
use Innmind\Stream\Writable;
23
use Innmind\Immutable\Str;
24
25
final class Graphs implements Trigger
26
{
27
    private $filesystem;
28
    private $processes;
29
    private $tmp;
30
31 12
    public function __construct(
32
        Filesystem $filesystem,
33
        Processes $processes,
34
        PathInterface $tmp
35
    ) {
36 12
        $this->filesystem = $filesystem;
37 12
        $this->processes = $processes;
38 12
        $this->tmp = $tmp;
39 12
    }
40
41 8
    public function __invoke(Activity $activity, Environment $env): void
42
    {
43 8
        if (!$activity->is(Type::start())) {
44 2
            return;
45
        }
46
47 6
        $ask = new Question('Render dependency graphs? [Y/n]');
48 6
        $response = (string) $ask($env->input(), $env->output());
49
50 6
        if (($response ?: 'y') === 'n') {
51 2
            return;
52
        }
53
54 4
        $name = $this->load($env);
55 4
        [$vendor, $package] = $name->split('/');
56
57 4
        $this->open(
58 4
            Command::foreground('dependency-graph')
59 4
                ->withArgument('depends-on')
60 4
                ->withArgument((string) $name)
61 4
                ->withArgument((string) $vendor)
62 4
                ->withWorkingDirectory((string) $this->tmp),
63 4
            $env->error()
64
        );
65 4
        $this->open(
66 4
            Command::foreground('dependency-graph')
67 4
                ->withArgument('of')
68 4
                ->withArgument((string) $name)
69 4
                ->withWorkingDirectory((string) $this->tmp),
70 4
            $env->error()
71
        );
72 4
        $this->open(
73 4
            Command::foreground('dependency-graph')
74 4
                ->withArgument('vendor')
75 4
                ->withArgument((string) $vendor)
76 4
                ->withWorkingDirectory((string) $this->tmp),
77 4
            $env->error()
78
        );
79 4
    }
80
81 4
    private function load(Environment $env): Str
82
    {
83
        $composer = $this
84 4
            ->filesystem
85 4
            ->mount($env->workingDirectory())
86 4
            ->get('composer.json')
87 4
            ->content();
88 4
        $package = Json::decode((string) $composer);
89
90 4
        return Str::of($package['name']);
91
    }
92
93 4
    private function open(Command $command, Writable $error): void
94
    {
95 4
        $process = $this->processes->execute($command)->wait();
96
97 4
        if (!$process->exitCode()->isSuccessful()) {
98 2
            $error->write(Str::of((string) $process->output()));
99
100 2
            return;
101
        }
102
103
        $this
104 4
            ->processes
105 4
            ->execute(
106 4
                Command::foreground('open')
107 4
                    ->withArgument((string) $process->output())
108 4
                    ->withWorkingDirectory((string) $this->tmp)
109
            )
110 4
            ->wait();
111 4
    }
112
}
113