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.

Client::withOutAccessToken()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 24

Duplication

Lines 6
Ratio 25 %

Code Coverage

Tests 13
CRAP Score 3.0213

Importance

Changes 0
Metric Value
dl 6
loc 24
ccs 13
cts 15
cp 0.8667
rs 9.536
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 3.0213
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Client\Twitter;
4
5
use ApiClients\Client\Twitter\Resource\Async\Profile;
6
use ApiClients\Client\Twitter\Resource\ProfileInterface;
7
use ApiClients\Client\Twitter\Resource\TweetInterface;
8
use ApiClients\Client\Twitter\Resource\UserInterface;
9
use ApiClients\Foundation\Client as FoundationClient;
10
use ApiClients\Foundation\Factory;
11
use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildSyncFromAsyncCommand;
12
use ApiClients\Foundation\Options;
13
use ApiClients\Foundation\Transport\Options as TransportOptions;
14
use ApiClients\Middleware\Oauth1\Oauth1Middleware;
15
use ApiClients\Middleware\Oauth1\Options as Oauth1Options;
16
use ApiClients\Tools\Psr7\Oauth1\Definition;
17
use React\EventLoop\Factory as LoopFactory;
18
use React\EventLoop\LoopInterface;
19
use function Clue\React\Block\await;
20
21
final class Client implements ClientInterface
22
{
23
24
    /**
25
     * @var LoopInterface
26
     */
27
    protected $loop;
28
29
    /**
30
     * @var AsyncClient
31
     */
32
    protected $asyncClient;
33
34
    /**
35
     * @var FoundationClient
36
     */
37
    protected $client;
38
39
    /**
40
     * @var StreamingClient
41
     */
42
    protected $streamingClient;
43
44
    /**
45
     * @var array
46
     */
47
    protected $options;
48
    /**
49
     * @var string
50
     */
51
    private $consumerKey;
52
53
    /**
54
     * @var string
55
     */
56
    private $consumerSecret;
57
58 1
    public function __construct(
59
        string $consumerKey,
60
        string $consumerSecret
61
    ) {
62 1
        $this->consumerKey = $consumerKey;
63 1
        $this->consumerSecret = $consumerSecret;
64 1
        $this->loop = LoopFactory::create();
65
66 1
        $this->options = ApiSettings::getOptions(
67 1
            $consumerKey,
68 1
            $consumerSecret,
69 1
            'Sync'
70
        );
71
72 1
        $this->client = Factory::create($this->loop, $this->options);
73
74 1
        $this->asyncClient = new AsyncClient($consumerKey, $consumerSecret, $this->loop, [], $this->client);
75 1
    }
76
77 1
    public function withAccessToken(string $accessToken, string $accessTokenSecret): Client
78
    {
79 1
        $options = $this->options;
80
        // @codingStandardsIgnoreStart
81 1
        $options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN] = new Definition\AccessToken($accessToken);
82 1
        $options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET] = new Definition\TokenSecret($accessTokenSecret);
83
        // @codingStandardsIgnoreEnd
84
85 1
        $clone = clone $this;
86 1
        $clone->client = Factory::create($this->loop, $options);
87 1
        $clone->asyncClient = (new AsyncClient(
88 1
            $this->consumerKey,
89 1
            $this->consumerSecret,
90 1
            $this->loop,
91 1
            [],
92 1
            $this->client
93 1
        ))->withAccessToken($accessToken, $accessTokenSecret);
94
95 1
        return $clone;
96
    }
97
98 1
    public function withOutAccessToken(): Client
99
    {
100 1
        $options = $this->options;
101
        // @codingStandardsIgnoreStart
102 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...
103
            unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN]);
104
        }
105 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...
106
            unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET]);
107
        }
108
        // @codingStandardsIgnoreEnd
109
110 1
        $clone = clone $this;
111 1
        $clone->client = Factory::create($this->loop, $options);
112 1
        $clone->asyncClient = (new AsyncClient(
113 1
            $this->consumerKey,
114 1
            $this->consumerSecret,
115 1
            $this->loop,
116 1
            [],
117 1
            $this->client
118
        ));
119
120 1
        return $clone;
121
    }
122
123
    public function stream(): StreamingClient
124
    {
125
        if (!($this->streamingClient instanceof StreamingClient)) {
126
            $this->streamingClient = new StreamingClient(
127
                $this->loop,
128
                $this->asyncClient->getCommandBus(),
129
                $this->asyncClient->stream()
130
            );
131
        }
132
133
        return $this->streamingClient;
134
    }
135
136
    public function tweet(string $tweet): TweetInterface
137
    {
138
        return await(
139
            $this->asyncClient->tweet($tweet),
140
            $this->loop
141
        );
142
    }
143
144
    public function profile(): ProfileInterface
145
    {
146
        return await(
147
            $this->asyncClient->profile()->then(function (Profile $profile) {
148
                return $this->client->handle(new BuildSyncFromAsyncCommand(ProfileInterface::HYDRATE_CLASS, $profile));
149
            }),
150
            $this->loop
151
        );
152
    }
153
154
    public function user(string $tweet): UserInterface
155
    {
156
        return await(
157
            $this->asyncClient->user($tweet),
158
            $this->loop
159
        );
160
    }
161
}
162