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 — master ( 709c6a...063dd9 )
by Robert
32:19 queued 28:25
created

CommandBuilder::addDriver()   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 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the "php-ipfs" package.
7
 *
8
 * (c) Robert Schönthal <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace IPFS\Console;
15
16
use ArgumentsResolver\NamedArgumentsResolver;
17
use IPFS\Api;
18
use IPFS\Client;
19
use IPFS\Driver\Driver;
20
use IPFS\Utils\AnnotationReader;
21
use IPFS\Utils\CaseFormatter;
22
use Symfony\Component\Console\Command\Command;
23
use Symfony\Component\Console\Input\InputArgument;
24
use Symfony\Component\Console\Input\InputDefinition;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Input\InputOption;
27
use Symfony\Component\Console\Output\OutputInterface;
28
29
class CommandBuilder
30
{
31
    /**
32
     * @var array|Api\Api[]
33
     */
34
    private $apis;
35
    /**
36
     * @var AnnotationReader
37
     */
38
    private $reader;
39
    /**
40
     * @var array|Driver[]
41
     */
42
    private $drivers = [];
43
44 3
    public function __construct(array $apis, AnnotationReader $reader)
45
    {
46 3
        $this->apis = $apis;
47 3
        $this->reader = $reader;
48 3
    }
49
50
    /**
51
     * @return array|ApiCommand[]
52
     */
53 2
    public function generateCommands(): array
54
    {
55 2
        $commands = [];
56
57 2
        foreach ($this->apis as $class) {
58 2
            $api = new \ReflectionClass($class);
59
60 2
            foreach ($api->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
61 2
                if ($this->reader->isApi($method)) {
62 2
                    $command = $this->generateCommand($class, $api->getShortName(), $method);
63 2
                    $commands[$command->getName()] = $command;
64
                }
65
            }
66
        }
67
68 2
        return $commands;
69
    }
70
71 2
    private function generateCommand(Api\Api $api, string $name, \ReflectionMethod $method): Command
72
    {
73 2
        $command = new ApiCommand(strtolower($this->reader->getName($method)));
74
75
        return $command
76 2
            ->setDefinition($this->buildDefinition($method))
77 2
            ->setDescription($this->reader->getDescription($method))
78 2
            ->setCode($this->createCode($api, $name, $method))
79
        ;
80
    }
81
82 2
    private function buildDefinition(\ReflectionMethod $method): InputDefinition
83
    {
84 2
        $definition = new InputDefinition();
85
86 2
        $parameters = $this->reader->getParameters($method);
87
88 2
        foreach ($parameters as $name => $param) {
89 2
            $name = CaseFormatter::camelToDash($name);
90
91 2
            if ($param->hasDefault()) {
92 2
                $mode = InputOption::VALUE_NONE;
93 2
                $default = $param->getDefault();
94
95 2
                if (true === $default || is_string($default)) {
96
                    $mode = InputOption::VALUE_OPTIONAL;
97 2
                } elseif (is_bool($default)) {
98 2
                    $default = null;
99 2
                } elseif (!is_string($default)) {
100 2
                    $mode = InputOption::VALUE_REQUIRED;
101
                }
102
103 2
                $definition->addOption(new InputOption($name, null, $mode, $param->getDescription(), $default));
104
105 2
                continue;
106
            }
107
108 2
            $definition->addArgument(new InputArgument($name, InputArgument::REQUIRED, $param->getDescription(), null));
109
        }
110
111 2
        return $definition;
112
    }
113
114
    private function createCode(Api\Api $api, $name, \ReflectionMethod $method): \Closure
115
    {
116 2
        return function (InputInterface $input, OutputInterface $output) use ($name, $method, $api) {
117 1
            $fn = $method->getClosure($api);
118
119 1
            $options = CaseFormatter::dashToCamelArray($input->getOptions());
120 1
            $arguments = CaseFormatter::dashToCamelArray($input->getArguments());
121
122 1
            $args = (new NamedArgumentsResolver($method))->resolve(array_merge($options, $arguments));
123 1
            $args = $this->sanitizeArguments($args);
124
125 1
            $client = new Client($this->chooseClient($input->getOption('driver')));
126 1
            $response = $client->execute($fn(...$args));
127
128 1
            $output->writeln($response);
129 2
        };
130
    }
131
132 1
    public function addDriver(Driver $driver): CommandBuilder
133
    {
134 1
        $this->drivers[get_class($driver)] = $driver;
135
136 1
        return $this;
137
    }
138
139 1
    private function chooseClient(string $class): Driver
140
    {
141 1
        if (!isset($this->drivers[$class])) {
142
            throw new \InvalidArgumentException(sprintf('"%s" is an unknown Driver, please add it with "addDriver"', $class));
143
        }
144
145 1
        return $this->drivers[$class];
146
    }
147
148 1
    private function sanitizeArguments(array $args): array
149
    {
150 1
        foreach ($args as $index => $value) {
151 1
            $args[$index] = CaseFormatter::stringToBool($value);
152
        }
153
154 1
        return $args;
155
    }
156
}
157