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 ( 8f32c7...acfbdb )
by Cees-Jan
03:16
created

AsyncClient::withOutAccessToken()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 6
Ratio 31.58 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 6
loc 19
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 4
nop 0
crap 3
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Client\Twitter;
4
5
use ApiClients\Foundation\Client;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ApiClients\Client\Twitter\Client.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use ApiClients\Foundation\Factory;
7
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand;
8
use ApiClients\Foundation\Options;
9
use ApiClients\Foundation\Transport\CommandBus\Command\RequestCommand;
10
use ApiClients\Foundation\Transport\Options as TransportOptions;
11
use ApiClients\Middleware\Oauth1\Oauth1Middleware;
12
use ApiClients\Middleware\Oauth1\Options as Oauth1Options;
13
use ApiClients\Tools\CommandBus\CommandBusInterface;
14
use ApiClients\Tools\Psr7\Oauth1\Definition;
15
use GuzzleHttp\Psr7\Request;
16
use Psr\Http\Message\ResponseInterface;
17
use React\EventLoop\LoopInterface;
18
use React\Promise\PromiseInterface;
19
use function React\Promise\resolve;
20
21
final class AsyncClient implements AsyncClientInterface
22
{
23
24
    /**
25
     * @var Client
26
     */
27
    protected $client;
28
29
    /**
30
     * @var array
31
     */
32
    protected $options;
33
34
    /**
35
     * @var AsyncStreamingClient
36
     */
37
    protected $streamingClient;
38
    /**
39
     * @var string
40
     */
41
    private $consumerKey;
42
43
    /**
44
     * @var string
45
     */
46
    private $consumerSecret;
47
48
    /**
49
     * @var LoopInterface
50
     */
51
    private $loop;
52
53
    /**
54
     * AsyncClient constructor.
55
     * @param string        $consumerKey
56
     * @param string        $consumerSecret
57
     * @param LoopInterface $loop
58
     * @param array         $options
59
     * @param Client|null   $client
60
     */
61 2
    public function __construct(
62
        string $consumerKey,
63
        string $consumerSecret,
64
        LoopInterface $loop,
65
        array $options = [],
66
        Client $client = null
67
    ) {
68 2
        $this->consumerKey = $consumerKey;
69 2
        $this->consumerSecret = $consumerSecret;
70 2
        $this->loop = $loop;
71
72 2
        if (!($client instanceof Client)) {
73 2
            $this->options = ApiSettings::getOptions(
74 2
                $consumerKey,
75 2
                $consumerSecret,
76 2
                'Async',
77 2
                $options
78
            );
79
80 2
            $client = Factory::create($this->loop, $this->options);
81
        }
82
83 2
        $this->client = $client;
84 2
    }
85
86 2
    public function withAccessToken(string $accessToken, string $accessTokenSecret): AsyncClient
87
    {
88 2
        $options = $this->options;
89
        // @codingStandardsIgnoreStart
90 2
        $options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN] = new Definition\AccessToken($accessToken);
91 2
        $options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET] = new Definition\TokenSecret($accessTokenSecret);
92
        // @codingStandardsIgnoreEnd
93
94 2
        return new self(
95 2
            $this->consumerKey,
96 2
            $this->consumerSecret,
97 2
            $this->loop,
98 2
            $options
99
        );
100
    }
101
102 1
    public function withOutAccessToken(): AsyncClient
103
    {
104 1
        $options = $this->options;
105
        // @codingStandardsIgnoreStart
106 1 View Code Duplication
        if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
107 1
            unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN]);
108
        }
109 1 View Code Duplication
        if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
110 1
            unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET]);
111
        }
112
        // @codingStandardsIgnoreEnd
113
114 1
        return new self(
115 1
            $this->consumerKey,
116 1
            $this->consumerSecret,
117 1
            $this->loop,
118 1
            $options
119
        );
120
    }
121
122
    public function getCommandBus(): CommandBusInterface
123
    {
124
        return $this->client->getFromContainer(CommandBusInterface::class);
125
    }
126
127 View Code Duplication
    public function profile(): PromiseInterface
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...
128
    {
129
        return $this->client->handle(new RequestCommand(
130
            new Request('GET', 'account/verify_credentials.json')
131
        ))->then(function (ResponseInterface $response) {
132
            return resolve($this->client->handle(new HydrateCommand('Profile', $response->getBody()->getJson())));
0 ignored issues
show
Bug introduced by
The method getJson() does not seem to exist on object<Psr\Http\Message\StreamInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
        });
134
    }
135
136 View Code Duplication
    public function user(string $user): PromiseInterface
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...
137
    {
138
        return $this->client->handle(new RequestCommand(
139
            new Request('GET', 'users/show.json?screen_name=' . $user)
140
        ))->then(function (ResponseInterface $response) {
141
            return resolve($this->client->handle(new HydrateCommand('User', $response->getBody()->getJson())));
0 ignored issues
show
Bug introduced by
The method getJson() does not seem to exist on object<Psr\Http\Message\StreamInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
        });
143
    }
144
145
    public function stream(): AsyncStreamingClient
146
    {
147
        if (!($this->streamingClient instanceof AsyncStreamingClient)) {
148
            $this->streamingClient = new AsyncStreamingClient($this->client);
149
        }
150
151
        return $this->streamingClient;
152
    }
153
}
154