1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of GitterApi package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Gitter\Resources; |
11
|
|
|
|
12
|
|
|
use Gitter\Adapters\AdapterInterface; |
13
|
|
|
use Gitter\Adapters\HttpAdapter; |
14
|
|
|
use Gitter\Adapters\StreamAdapter; |
15
|
|
|
use Gitter\Route; |
16
|
|
|
use Gitter\Client; |
17
|
|
|
use Gitter\Support\Observer; |
18
|
|
|
use GuzzleHttp\Exception\ClientException; |
19
|
|
|
use Serafim\Evacuator\Evacuator; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class AbstractResource |
23
|
|
|
* @package Gitter\Resources |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractResource implements ResourceInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var null |
29
|
|
|
*/ |
30
|
|
|
private $client; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* AbstractResource constructor. |
34
|
|
|
* @param Client $client |
35
|
|
|
*/ |
36
|
|
|
public function __construct(Client $client) |
37
|
|
|
{ |
38
|
|
|
$this->client = $client; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return Client |
43
|
|
|
*/ |
44
|
|
|
protected function client(): Client |
45
|
|
|
{ |
46
|
|
|
return $this->client; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Route $route |
51
|
|
|
* @return array|mixed |
52
|
|
|
* @throws \Exception |
53
|
|
|
* @throws \Throwable |
54
|
|
|
* @throws \InvalidArgumentException |
55
|
|
|
*/ |
56
|
|
|
protected function fetch(Route $route): array |
57
|
|
|
{ |
58
|
|
|
$rescue = (new Evacuator(function () use ($route) { |
59
|
|
|
return (array)$this->viaHttp()->request($route); |
60
|
|
|
})) |
61
|
|
|
// If response has status code 4xx |
62
|
|
|
->onError(function (ClientException $e) { |
63
|
|
|
$this->client->logger->error(\get_class($e) . ' ' . $e->getMessage()); |
64
|
|
|
|
65
|
|
|
switch ($e->getResponse()->getStatusCode()) { |
66
|
|
|
case 429: // 429 Too Many Requests |
67
|
|
|
sleep(2); |
68
|
|
|
return null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
throw $e; |
72
|
|
|
}) |
73
|
|
|
// Other |
74
|
|
|
->onError(function (\Exception $e) { |
75
|
|
|
$this->client->logger->error($e->getMessage()); |
76
|
|
|
throw $e; |
77
|
|
|
}) |
78
|
|
|
->retries($this->client->getRetriesCount()) |
|
|
|
|
79
|
|
|
->invoke(); |
80
|
|
|
|
81
|
|
|
return $rescue; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Route $route |
86
|
|
|
* @return Observer |
87
|
|
|
* @throws \Throwable |
88
|
|
|
* @throws \InvalidArgumentException |
89
|
|
|
*/ |
90
|
|
|
protected function stream(Route $route): Observer |
91
|
|
|
{ |
92
|
|
|
return (new Evacuator(function() use ($route) { |
93
|
|
|
return $this->viaStream()->request($route); |
|
|
|
|
94
|
|
|
})) |
95
|
|
|
->onError(function (\Exception $e) { |
96
|
|
|
$this->client->logger->error($e->getMessage()); |
97
|
|
|
}) |
98
|
|
|
->retries($this->client->getRetriesCount()) |
|
|
|
|
99
|
|
|
->invoke(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return AdapterInterface|HttpAdapter |
104
|
|
|
* @throws \InvalidArgumentException |
105
|
|
|
*/ |
106
|
|
|
protected function viaHttp(): HttpAdapter |
107
|
|
|
{ |
108
|
|
|
return $this->client->viaHttp(); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return AdapterInterface|StreamAdapter |
113
|
|
|
*/ |
114
|
|
|
protected function viaStream(): StreamAdapter |
115
|
|
|
{ |
116
|
|
|
return $this->client->viaStream(); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..