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 ( ed9f29...e1b520 )
by Cees-Jan
10:51
created

Client::withOutAccessToken()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 6
Ratio 25 %

Code Coverage

Tests 12
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 6
loc 24
c 0
b 0
f 0
ccs 12
cts 15
cp 0.8
rs 8.9713
cc 3
eloc 14
nc 4
nop 0
crap 3.072
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
            $consumerKey,
68
            $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 1
95
        return $clone;
96
    }
97 1
98
    public function withOutAccessToken(): Client
99 1
    {
100
        $options = $this->options;
101 1
        // @codingStandardsIgnoreStart
102 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 1
        }
105 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 1
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
            $this->client
118 1
        ));
119
120
        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),
0 ignored issues
show
Bug introduced by
The method tweet() does not seem to exist on object<ApiClients\Client\Twitter\AsyncClient>.

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...
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