Client   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 234
Duplicated Lines 32.05 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 14
c 4
b 1
f 0
lcom 1
cbo 4
dl 75
loc 234
ccs 0
cts 108
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getAuthorizeUrl() 0 17 3
A getAccessToken() 18 18 2
A refreshAccessToken() 18 18 2
A getDonations() 0 16 2
A postDonation() 19 19 2
A postAlert() 20 20 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace TwitchAlerts;
4
5
use GuzzleHttp\Exception\BadResponseException;
6
use TwitchAlerts\Exception\ApiException;
7
use TwitchAlerts\Exception\InvalidScopeException;
8
use GuzzleHttp\Client as HttpClient;
9
10
/**
11
 * Class Client
12
 *
13
 * @package TwitchAlerts
14
 */
15
class Client
16
{
17
    /**
18
     * Twitch alerts API base URI
19
     */
20
    const BASE_URI = 'https://www.twitchalerts.com';
21
22
    /**
23
     *  Twitch alerts Authorize endpoint path
24
     */
25
    const AUTHORIZE_ENDPOINT = '/api/v1.0/authorize';
26
27
    /**
28
     *  Twitch alerts Token endpoint path
29
     */
30
    const TOKEN_ENDPOINT = '/api/v1.0/token';
31
32
    /**
33
     *  Twitch alerts Donations endpoint path
34
     */
35
    const DONATION_ENDPOINT = '/api/v1.0/donations';
36
37
    /**
38
     *  Twitch alerts Alert endpoint path
39
     */
40
    const ALERT_ENDPOINT = '/api/v1.0/alerts';
41
42
    /**
43
     * Twitch alerts API scopes
44
     */
45
    const SCOPES = [
46
        'donations.create',
47
        'donations.read',
48
        'alerts.create',
49
    ];
50
51
    /**
52
     * @var string
53
     */
54
    protected $clientId;
55
56
    /**
57
     * @var string
58
     */
59
    protected $clientSecret;
60
61
    /**
62
     * @var string
63
     */
64
    protected $redirectUrl;
65
66
    /**
67
     * @var HttpClient
68
     */
69
    protected $httpClient;
70
71
    /**
72
     * Client constructor.
73
     *
74
     * @param string $clientId
75
     * @param string $clientSecret
76
     * @param string $redirectUrl
77
     */
78
    public function __construct($clientId, $clientSecret, $redirectUrl)
79
    {
80
        $this->clientId     = $clientId;
81
        $this->clientSecret = $clientSecret;
82
        $this->redirectUrl  = $redirectUrl;
83
        $this->httpClient   = new HttpClient(['base_uri' => self::BASE_URI]);
84
    }
85
86
    /**
87
     * @param array $scopes
88
     *
89
     * @return string
90
     * @throws InvalidScopeException
91
     */
92
    public function getAuthorizeUrl(array $scopes = self::SCOPES)
93
    {
94
        foreach ($scopes as $scope) {
95
            if (!in_array($scope, self::SCOPES)) {
96
                throw new InvalidScopeException(sprintf('Invalid scope %s', $scope));
97
            }
98
        }
99
100
        $query = [
101
            'response_type' => 'code',
102
            'client_id'     => $this->clientId,
103
            'redirect_uri'  => $this->redirectUrl,
104
            'scope'         => implode(',', $scopes)
105
        ];
106
107
        return self::BASE_URI . self::AUTHORIZE_ENDPOINT . '?' . http_build_query($query);
108
    }
109
110
    /**
111
     * @param string $code
112
     *
113
     * @return array
114
     * @throws ApiException
115
     */
116 View Code Duplication
    public function getAccessToken($code)
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...
117
    {
118
        try {
119
            $response = $this->httpClient->post(self::TOKEN_ENDPOINT, [
120
                'form_params' => [
121
                    'grant_type'    => 'authorization_code',
122
                    'client_id'     => $this->clientId,
123
                    'client_secret' => $this->clientSecret,
124
                    'redirect_uri'  => $this->redirectUrl,
125
                    'code'          => $code
126
                ]
127
            ]);
128
129
            return json_decode((string) $response->getBody(), true);
130
        } catch (BadResponseException $e) {
131
            throw ApiException::fromBadResponseException($e);
132
        }
133
    }
134
135
    /**
136
     * @param string $refreshToken
137
     *
138
     * @return array
139
     * @throws ApiException
140
     */
141 View Code Duplication
    public function refreshAccessToken($refreshToken)
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...
142
    {
143
        try {
144
            $response = $this->httpClient->post(self::TOKEN_ENDPOINT, [
145
                'form_params' => [
146
                    'grant_type'    => 'refresh_token',
147
                    'client_id'     => $this->clientId,
148
                    'client_secret' => $this->clientSecret,
149
                    'redirect_uri'  => $this->redirectUrl,
150
                    'refresh_token' => $refreshToken
151
                ]
152
            ]);
153
154
            return json_decode((string) $response->getBody(), true);
155
        } catch (BadResponseException $e) {
156
            throw ApiException::fromBadResponseException($e);
157
        }
158
    }
159
160
    /**
161
     * @param string $accessToken
162
     * @param int    $limit
163
     * @param string $currency
164
     *
165
     * @return array
166
     * @throws ApiException
167
     */
168
    public function getDonations($accessToken, $limit, $currency)
169
    {
170
        try {
171
            $response = $this->httpClient->get(self::DONATION_ENDPOINT, [
172
                'query' => [
173
                    'access_token' => $accessToken,
174
                    'limit'        => $limit,
175
                    'currency'     => $currency
176
                ]
177
            ]);
178
179
            return json_decode((string) $response->getBody(), true);
180
        } catch (BadResponseException $e) {
181
            throw ApiException::fromBadResponseException($e);
182
        }
183
    }
184
185
    /**
186
     * @param string $accessToken
187
     * @param string $name
188
     * @param string $email
189
     * @param int    $amount
190
     * @param string $currency
191
     * @param string $message
192
     *
193
     * @return array
194
     * @throws ApiException
195
     */
196 View Code Duplication
    public function postDonation($accessToken, $name, $email, $amount, $currency, $message)
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...
197
    {
198
        try {
199
            $response = $this->httpClient->post(self::DONATION_ENDPOINT, [
200
                'form_params' => [
201
                    'access_token' => $accessToken,
202
                    'name'         => $name,
203
                    'identifier'   => $email,
204
                    'amount'       => $amount,
205
                    'currency'     => $currency,
206
                    'message'      => $message
207
                ]
208
            ]);
209
210
            return json_decode((string) $response->getBody(), true);
211
        } catch (BadResponseException $e) {
212
            throw ApiException::fromBadResponseException($e);
213
        }
214
    }
215
216
    /**
217
     * @param string $accessToken
218
     * @param string $type
219
     * @param string $message
220
     * @param string $imageUrl
221
     * @param string $soundUrl
222
     * @param string $textColor
223
     * @param int    $duration
224
     *
225
     * @return array
226
     * @throws ApiException
227
     */
228 View Code Duplication
    public function postAlert($accessToken, $type, $message, $imageUrl, $soundUrl, $textColor, $duration)
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...
229
    {
230
        try {
231
            $response = $this->httpClient->post(self::ALERT_ENDPOINT, [
232
                'form_params' => [
233
                    'access_token'       => $accessToken,
234
                    'type'               => $type,
235
                    'message'            => $message,
236
                    'image_href'         => $imageUrl,
237
                    'sound_href'         => $soundUrl,
238
                    'special_text_color' => $textColor,
239
                    'duration'           => $duration
240
                ]
241
            ]);
242
243
            return json_decode((string) $response->getBody(), true);
244
        } catch (BadResponseException $e) {
245
            throw ApiException::fromBadResponseException($e);
246
        }
247
    }
248
}
249