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
Pull Request — master (#6)
by Jaap
08:31
created

Factory::determineUserAgent()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
cc 6
eloc 15
nc 5
nop 2
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Foundation\Transport;
4
5
use ApiClients\Foundation\Middleware\Locator\Locator;
6
use Clue\React\Buzz\Browser;
7
use Clue\React\Buzz\Io\Sender;
8
use Interop\Container\ContainerInterface;
9
use InvalidArgumentException;
10
use React\Dns\Resolver\Factory as ResolverFactory;
11
use React\Dns\Resolver\Resolver;
12
use React\EventLoop\LoopInterface;
13
use React\HttpClient\Client as HttpClient;
14
use React\HttpClient\Factory as HttpClientFactory;
15
16
class Factory
17
{
18
    /**
19
     * @param ContainerInterface $container
20
     * @param LoopInterface $loop
21
     * @param array $options
22
     * @return Client
23
     */
24
    public static function create(
25
        ContainerInterface $container,
26
        LoopInterface $loop,
27
        array $options = []
28
    ): Client {
29
        if (!isset($options[Options::DNS])) {
30
            $options[Options::DNS] = '8.8.8.8';
31
        }
32
33
        $resolver = (new ResolverFactory())->createCached($options[Options::DNS], $loop);
34
        $httpClient = (new HttpClientFactory())->create($loop, $resolver);
35
36
        return self::createFromReactHttpClient(
37
            $container,
38
            $httpClient,
39
            $resolver,
40
            $loop,
41
            self::determineUserAgent($container, $options)
42
        );
43
    }
44
45
    /**
46
     * @param ContainerInterface $container
47
     * @param HttpClient $httpClient
48
     * @param Resolver $resolver
49
     * @param LoopInterface $loop
50
     * @param array $options
51
     * @return Client
52
     */
53
    public static function createFromReactHttpClient(
54
        ContainerInterface $container,
55
        HttpClient $httpClient,
0 ignored issues
show
Unused Code introduced by
The parameter $httpClient is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
        Resolver $resolver,
57
        LoopInterface $loop,
58
        array $options = []
59
    ): Client {
60
        return self::createFromBuzz(
61
            $container,
62
            $loop,
63
            (new Browser($loop, Sender::createFromLoopDns($loop, $resolver)))->withOptions([
64
                'streaming' => true,
65
            ]),
66
            self::determineUserAgent($container, $options)
67
        );
68
    }
69
70
    /**
71
     * @param ContainerInterface $container
72
     * @param LoopInterface $loop
73
     * @param Browser $buzz
74
     * @param array $options
75
     * @return Client
76
     */
77
    public static function createFromBuzz(
78
        ContainerInterface $container,
79
        LoopInterface $loop,
80
        Browser $buzz,
81
        array $options = []
82
    ): Client {
83
        return new Client(
84
            $loop,
85
            $container->get(Locator::class),
86
            $buzz,
87
            self::determineUserAgent($container, $options)
88
        );
89
    }
90
91
    private static function determineUserAgent(ContainerInterface $container, array $options) : array
92
    {
93
        if (!isset($options[Options::USER_AGENT]) && !isset($options[Options::USER_AGENT_STRATEGY])) {
94
            throw new InvalidArgumentException('No way to determine user agent');
95
        }
96
97
        if (!isset($options[Options::USER_AGENT_STRATEGY])) {
98
            return $options;
99
        }
100
101
        $strategy = $options[Options::USER_AGENT_STRATEGY];
102
103
        if (!class_exists($strategy)) {
104
            throw new InvalidArgumentException(sprintf('Strategy "%s", doesn\'t exist', $strategy));
105
        }
106
107
        if (!is_subclass_of($strategy, UserAgentStrategyInterface::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \ApiClients\Foundation\T...trategyInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
108
            throw new InvalidArgumentException(sprintf(
109
                'Strategy "%s", doesn\'t implement "%s"',
110
                $strategy,
111
                UserAgentStrategyInterface::class
112
            ));
113
        }
114
115
        $options[Options::USER_AGENT] = $container->get($strategy)->determineUserAgent($options);
116
117
        return $options;
118
    }
119
}
120