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 ( 61a149...5b9c21 )
by Cees-Jan
9s
created

Client::profile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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