|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is the main class of the keinos/mastodon-streaming-api-config package. |
|
5
|
|
|
* |
|
6
|
|
|
* - Reference of the public methods of this class to use: |
|
7
|
|
|
* - See: ./ConfigInterface.php |
|
8
|
|
|
* |
|
9
|
|
|
* - Authors, copyright, license, usage and etc.: |
|
10
|
|
|
* - See: https://github.com/KEINOS/Mastodon_StreamingAPI_Config/ |
|
11
|
|
|
* |
|
12
|
|
|
* Debug: |
|
13
|
|
|
* - To test and analyze the scripts run: |
|
14
|
|
|
* - Debug: `$ composer test all verbose` |
|
15
|
|
|
* - Help: `$ composer test help` |
|
16
|
|
|
* - Note: Composer is required |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
declare(strict_types=1); |
|
20
|
|
|
|
|
21
|
|
|
namespace KEINOS\MSTDN_TOOLS\Config; |
|
22
|
|
|
|
|
23
|
|
|
use Symfony\Component\HttpClient\HttpClient; |
|
|
|
|
|
|
24
|
|
|
use KEINOS\MSTDN_TOOLS\Cache\Cache; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* This class holds and provides Mastodon server, a.k.a instance, information to |
|
28
|
|
|
* receive server-sent messages from Mastodon Streaming API. |
|
29
|
|
|
*/ |
|
30
|
|
|
class Config implements ConfigInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/* Traits */ |
|
33
|
|
|
use PropertiesTrait; // Properties and their getter/setter methods as well |
|
34
|
|
|
use StaticMethodsTrait; |
|
35
|
|
|
|
|
36
|
|
|
/* Constants */ |
|
37
|
|
|
public const ACCESS_TOKEN_DEFAULT = ''; |
|
38
|
|
|
public const CACHE_PREFIX_DEFAULT = 'mstdn_tools_cache_'; |
|
39
|
|
|
public const CACHE_TTL_DEFAULT = 60 * 60 * 1; // 1 hour |
|
40
|
|
|
public const ENDPOINT_API_INSTANCE_DEFAULT = '/api/v1/instance'; |
|
41
|
|
|
public const FLAG_USE_CACHE_DEFAULT = true; |
|
42
|
|
|
|
|
43
|
|
|
/* Methods */ |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
* |
|
48
|
|
|
* @param array<string,mixed> $settings |
|
49
|
|
|
* @return void |
|
50
|
|
|
* @throws \Exception |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct(array $settings) |
|
53
|
|
|
{ |
|
54
|
|
|
/* Set must settings */ |
|
55
|
|
|
if (isset($settings['url_host'])) { |
|
56
|
|
|
$this->setUrlHost($settings['url_host']); |
|
57
|
|
|
} else { |
|
58
|
|
|
$msg = '"url_host" key missing. URL of the Mastodon server/instance is required.'; |
|
59
|
|
|
throw new \Exception($msg); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/* Set default settings */ |
|
63
|
|
|
$this->setDefaultSettings(); |
|
64
|
|
|
|
|
65
|
|
|
/* Set optional settings */ |
|
66
|
|
|
$this->setOptionalSettings($settings); |
|
67
|
|
|
|
|
68
|
|
|
$url_api_instance = $this->generateUrlApiInstance(); |
|
69
|
|
|
$this->setUrlApiInstance($url_api_instance); |
|
70
|
|
|
|
|
71
|
|
|
/* Set server info from API Instance method */ |
|
72
|
|
|
$info_instance = $this->preloadCacheInstanceInfo(); |
|
73
|
|
|
if (null === $info_instance) { |
|
74
|
|
|
$info_instance = $this->requestApiInstance(); |
|
75
|
|
|
} |
|
76
|
|
|
$this->saveCacheInstanceInfo($info_instance); |
|
77
|
|
|
$this->setInfoInstance($info_instance); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return string |
|
82
|
|
|
* @throws \Exception |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function generateUrlApiInstance(): string |
|
85
|
|
|
{ |
|
86
|
|
|
$url_api_instance = rtrim($this->getUrlHost(), '/') . $this->getEndpointApiInstance(); |
|
87
|
|
|
|
|
88
|
|
|
if (! self::isUrlAlive($url_api_instance)) { |
|
89
|
|
|
$msg = 'Invalid settings given. Can NOT access to the endpoint URL. URL: ' |
|
90
|
|
|
. $url_api_instance . PHP_EOL; |
|
91
|
|
|
throw new \Exception($msg); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $url_api_instance; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return null|mixed |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function preloadCacheInstanceInfo() |
|
101
|
|
|
{ |
|
102
|
|
|
$key_cache = 'info_instance'; |
|
|
|
|
|
|
103
|
|
|
$info_instance = null; |
|
104
|
|
|
|
|
105
|
|
|
$cache = new Cache([ |
|
106
|
|
|
'ttl' => $this->getTtlCache(), |
|
107
|
|
|
'prefix' => $this->getPrefixCache(), |
|
108
|
|
|
]); |
|
109
|
|
|
|
|
110
|
|
|
if ($this->getFlagUseCache()) { |
|
111
|
|
|
$info_instance = $cache->get('info_instance'); |
|
112
|
|
|
} else { |
|
113
|
|
|
$cache->clear(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $info_instance; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param mixed $info_instance |
|
121
|
|
|
* @return bool |
|
122
|
|
|
* @throws \Exception |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function saveCacheInstanceInfo($info_instance): bool |
|
125
|
|
|
{ |
|
126
|
|
|
try { |
|
127
|
|
|
$cache = new Cache([ |
|
128
|
|
|
'ttl' => $this->getTtlCache(), |
|
129
|
|
|
'prefix' => $this->getPrefixCache(), |
|
130
|
|
|
]); |
|
131
|
|
|
$cache->set('info_instance', $info_instance); |
|
132
|
|
|
return true; |
|
133
|
|
|
} catch (\Exception $e) { |
|
134
|
|
|
$msg = 'Error while getting the server info from the API.' . PHP_EOL |
|
135
|
|
|
. '- Error details: ' . $e->getMessage() . PHP_EOL; |
|
136
|
|
|
throw new \Exception($msg); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return array<mixed,mixed> |
|
142
|
|
|
* @throws \Exception |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function requestApiInstance(): array |
|
145
|
|
|
{ |
|
146
|
|
|
try { |
|
147
|
|
|
$client_http = HttpClient::create(); |
|
148
|
|
|
$url_api_instance = $this->getUrlApiInstance(); |
|
149
|
|
|
|
|
150
|
|
|
// Get contents and return as array |
|
151
|
|
|
$response = $client_http->request('GET', $url_api_instance); |
|
152
|
|
|
return $response->toArray(); |
|
153
|
|
|
} catch (\Exception $e) { |
|
154
|
|
|
$msg = 'Error while getting the server info from the API.' . PHP_EOL |
|
155
|
|
|
. '- Error details: ' . $e->getMessage() . PHP_EOL; |
|
156
|
|
|
throw new \Exception($msg); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
protected function setDefaultSettings(): void |
|
161
|
|
|
{ |
|
162
|
|
|
$this->setAccessToken(self::ACCESS_TOKEN_DEFAULT); |
|
163
|
|
|
$this->setEndpointApiInstance(self::ENDPOINT_API_INSTANCE_DEFAULT); |
|
164
|
|
|
$this->setFlagUseCache(self::FLAG_USE_CACHE_DEFAULT); |
|
165
|
|
|
$this->setIdHashSelf(strval(hash_file('md5', __FILE__))); |
|
166
|
|
|
$this->setPrefixCache(self::CACHE_PREFIX_DEFAULT); |
|
167
|
|
|
$this->setTtlCache(self::CACHE_TTL_DEFAULT); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param array<string,mixed> $settings |
|
172
|
|
|
* @return void |
|
173
|
|
|
* @throws \Exception |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function setOptionalSettings(array $settings): void |
|
176
|
|
|
{ |
|
177
|
|
|
if (isset($settings['access_token'])) { |
|
178
|
|
|
$this->setAccessToken($settings['access_token']); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
if (isset($settings['endpoint_api_instance'])) { |
|
182
|
|
|
$this->setEndpointApiInstance($settings['endpoint_api_instance']); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if (isset($settings['flag_use_cache'])) { |
|
186
|
|
|
$this->setFlagUseCache($settings['flag_use_cache']); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
if (isset($settings['prefix_cache'])) { |
|
190
|
|
|
$this->setPrefixCache($settings['prefix_cache']); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
if (isset($settings['id_hash_self'])) { |
|
194
|
|
|
$this->setIdHashSelf($settings['id_hash_self']); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
if (isset($settings['ttl_cache'])) { |
|
198
|
|
|
$this->setTtlCache($settings['ttl_cache']); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths