PluginProbe
Plausible Analytics / trunk
Plausible Analytics vtrunk
2.6.1 2.6.0 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 All 52 releases
plausible-analytics / src / Client / lib / Configuration.php

Configuration.php in Plausible Analytics trunk, at src/Client/lib/Configuration.php

514 lines 11.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Configuration
4 * PHP version 7.4
5 *
6 * @category Class
7 * @package Plausible\Analytics\WP\Client
8 * @author OpenAPI Generator team
9 * @link https://openapi-generator.tech
10 */
11
12 /**
13 * Plausible Plugins API
14 *
15 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
16 *
17 * The version of the OpenAPI document: 1.0-rc
18 * Generated by: https://openapi-generator.tech
19 * OpenAPI Generator version: 7.0.1
20 */
21
22 /**
23 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
24 * https://openapi-generator.tech
25 * Do not edit the class manually.
26 */
27
28 namespace Plausible\Analytics\WP\Client;
29
30 /**
31 * Configuration Class Doc Comment
32 * PHP version 7.4
33 *
34 * @category Class
35 * @package Plausible\Analytics\WP\Client
36 * @author OpenAPI Generator team
37 * @link https://openapi-generator.tech
38 */
39 class Configuration {
40 public const BOOLEAN_FORMAT_INT = 'int';
41
42 public const BOOLEAN_FORMAT_STRING = 'string';
43
44 /**
45 * @var Configuration
46 */
47 private static $defaultConfiguration;
48
49 /**
50 * Associate array to store API key(s)
51 *
52 * @var string[]
53 */
54 protected $apiKeys = [];
55
56 /**
57 * Associate array to store API prefix (e.g. Bearer)
58 *
59 * @var string[]
60 */
61 protected $apiKeyPrefixes = [];
62
63 /**
64 * Access token for OAuth/Bearer authentication
65 *
66 * @var string
67 */
68 protected $accessToken = '';
69
70 /**
71 * Boolean format for query string
72 *
73 * @var string
74 */
75 protected $booleanFormatForQueryString = self::BOOLEAN_FORMAT_INT;
76
77 /**
78 * Username for HTTP basic authentication
79 *
80 * @var string
81 */
82 protected $username = '';
83
84 /**
85 * Password for HTTP basic authentication
86 *
87 * @var string
88 */
89 protected $password = '';
90
91 /**
92 * The host
93 *
94 * @var string
95 */
96 protected $host = 'https://plausible.io';
97
98 /**
99 * User agent of the HTTP request, set to "OpenAPI-Generator/{version}/PHP" by default
100 *
101 * @var string
102 */
103 protected $userAgent = 'OpenAPI-Generator/1.0.0/PHP';
104
105 /**
106 * Debug switch (default set to false)
107 *
108 * @var bool
109 */
110 protected $debug = false;
111
112 /**
113 * Debug file location (log to STDOUT by default)
114 *
115 * @var string
116 */
117 protected $debugFile = 'php://output';
118
119 /**
120 * Debug file location (log to STDOUT by default)
121 *
122 * @var string
123 */
124 protected $tempFolderPath;
125
126 /**
127 * Constructor
128 */
129 public function __construct() {
130 $this->tempFolderPath = sys_get_temp_dir();
131 }
132
133 /**
134 * Sets API key
135 *
136 * @param string $apiKeyIdentifier API key identifier (authentication scheme)
137 * @param string $key API key or token
138 *
139 * @return $this
140 */
141 public function setApiKey( $apiKeyIdentifier, $key ) {
142 $this->apiKeys[ $apiKeyIdentifier ] = $key;
143
144 return $this;
145 }
146
147 /**
148 * Gets API key
149 *
150 * @param string $apiKeyIdentifier API key identifier (authentication scheme)
151 *
152 * @return null|string API key or token
153 */
154 public function getApiKey( $apiKeyIdentifier ) {
155 return isset( $this->apiKeys[ $apiKeyIdentifier ] ) ? $this->apiKeys[ $apiKeyIdentifier ] : null;
156 }
157
158 /**
159 * Sets the prefix for API key (e.g. Bearer)
160 *
161 * @param string $apiKeyIdentifier API key identifier (authentication scheme)
162 * @param string $prefix API key prefix, e.g. Bearer
163 *
164 * @return $this
165 */
166 public function setApiKeyPrefix( $apiKeyIdentifier, $prefix ) {
167 $this->apiKeyPrefixes[ $apiKeyIdentifier ] = $prefix;
168
169 return $this;
170 }
171
172 /**
173 * Gets API key prefix
174 *
175 * @param string $apiKeyIdentifier API key identifier (authentication scheme)
176 *
177 * @return null|string
178 */
179 public function getApiKeyPrefix( $apiKeyIdentifier ) {
180 return isset( $this->apiKeyPrefixes[ $apiKeyIdentifier ] ) ? $this->apiKeyPrefixes[ $apiKeyIdentifier ] : null;
181 }
182
183 /**
184 * Sets the access token for OAuth
185 *
186 * @param string $accessToken Token for OAuth
187 *
188 * @return $this
189 */
190 public function setAccessToken( $accessToken ) {
191 $this->accessToken = $accessToken;
192
193 return $this;
194 }
195
196 /**
197 * Gets the access token for OAuth
198 *
199 * @return string Access token for OAuth
200 */
201 public function getAccessToken() {
202 return $this->accessToken;
203 }
204
205 /**
206 * Sets boolean format for query string.
207 *
208 * @param string $booleanFormatForQueryString Boolean format for query string
209 *
210 * @return $this
211 */
212 public function setBooleanFormatForQueryString( string $booleanFormat ) {
213 $this->booleanFormatForQueryString = $booleanFormat;
214
215 return $this;
216 }
217
218 /**
219 * Gets boolean format for query string.
220 *
221 * @return string Boolean format for query string
222 */
223 public function getBooleanFormatForQueryString(): string {
224 return $this->booleanFormatForQueryString;
225 }
226
227 /**
228 * Sets the username for HTTP basic authentication
229 *
230 * @param string $username Username for HTTP basic authentication
231 *
232 * @return $this
233 */
234 public function setUsername( $username ) {
235 $this->username = $username;
236
237 return $this;
238 }
239
240 /**
241 * Gets the username for HTTP basic authentication
242 *
243 * @return string Username for HTTP basic authentication
244 */
245 public function getUsername() {
246 return $this->username;
247 }
248
249 /**
250 * Sets the password for HTTP basic authentication
251 *
252 * @param string $password Password for HTTP basic authentication
253 *
254 * @return $this
255 */
256 public function setPassword( $password ) {
257 $this->password = $password;
258
259 return $this;
260 }
261
262 /**
263 * Gets the password for HTTP basic authentication
264 *
265 * @return string Password for HTTP basic authentication
266 */
267 public function getPassword() {
268 return $this->password;
269 }
270
271 /**
272 * Sets the host
273 *
274 * @param string $host Host
275 *
276 * @return $this
277 */
278 public function setHost( $host ) {
279 $this->host = $host;
280
281 return $this;
282 }
283
284 /**
285 * Gets the host
286 *
287 * @return string Host
288 */
289 public function getHost() {
290 return $this->host;
291 }
292
293 /**
294 * Sets the user agent of the api client
295 *
296 * @param string $userAgent the user agent of the api client
297 *
298 * @return $this
299 * @throws \InvalidArgumentException
300 */
301 public function setUserAgent( $userAgent ) {
302 if ( ! is_string( $userAgent ) ) {
303 throw new \InvalidArgumentException( 'User-agent must be a string.' );
304 }
305
306 $this->userAgent = $userAgent;
307
308 return $this;
309 }
310
311 /**
312 * Gets the user agent of the api client
313 *
314 * @return string user agent
315 */
316 public function getUserAgent() {
317 return $this->userAgent;
318 }
319
320 /**
321 * Sets debug flag
322 *
323 * @param bool $debug Debug flag
324 *
325 * @return $this
326 */
327 public function setDebug( $debug ) {
328 $this->debug = $debug;
329
330 return $this;
331 }
332
333 /**
334 * Gets the debug flag
335 *
336 * @return bool
337 */
338 public function getDebug() {
339 return $this->debug;
340 }
341
342 /**
343 * Sets the debug file
344 *
345 * @param string $debugFile Debug file
346 *
347 * @return $this
348 */
349 public function setDebugFile( $debugFile ) {
350 $this->debugFile = $debugFile;
351
352 return $this;
353 }
354
355 /**
356 * Gets the debug file
357 *
358 * @return string
359 */
360 public function getDebugFile() {
361 return $this->debugFile;
362 }
363
364 /**
365 * Sets the temp folder path
366 *
367 * @param string $tempFolderPath Temp folder path
368 *
369 * @return $this
370 */
371 public function setTempFolderPath( $tempFolderPath ) {
372 $this->tempFolderPath = $tempFolderPath;
373
374 return $this;
375 }
376
377 /**
378 * Gets the temp folder path
379 *
380 * @return string Temp folder path
381 */
382 public function getTempFolderPath() {
383 return $this->tempFolderPath;
384 }
385
386 /**
387 * Gets the default configuration instance
388 *
389 * @return Configuration
390 */
391 public static function getDefaultConfiguration() {
392 if ( self::$defaultConfiguration === null ) {
393 self::$defaultConfiguration = new Configuration();
394 }
395
396 return self::$defaultConfiguration;
397 }
398
399 /**
400 * Sets the default configuration instance
401 *
402 * @param Configuration $config An instance of the Configuration Object
403 *
404 * @return void
405 */
406 public static function setDefaultConfiguration( Configuration $config ) {
407 self::$defaultConfiguration = $config;
408 }
409
410 /**
411 * Gets the essential information for debugging
412 *
413 * @return string The report for debugging
414 */
415 public static function toDebugReport() {
416 $report = 'PHP SDK (Plausible\Analytics\WP\Client) Debug Report:' . PHP_EOL;
417 $report .= ' OS: ' . php_uname() . PHP_EOL;
418 $report .= ' PHP Version: ' . PHP_VERSION . PHP_EOL;
419 $report .= ' The version of the OpenAPI document: 1.0-rc' . PHP_EOL;
420 $report .= ' Temp Folder Path: ' . self::getDefaultConfiguration()->getTempFolderPath() . PHP_EOL;
421
422 return $report;
423 }
424
425 /**
426 * Get API key (with prefix if set)
427 *
428 * @param string $apiKeyIdentifier name of apikey
429 *
430 * @return null|string API key with the prefix
431 */
432 public function getApiKeyWithPrefix( $apiKeyIdentifier ) {
433 $prefix = $this->getApiKeyPrefix( $apiKeyIdentifier );
434 $apiKey = $this->getApiKey( $apiKeyIdentifier );
435
436 if ( $apiKey === null ) {
437 return null;
438 }
439
440 if ( $prefix === null ) {
441 $keyWithPrefix = $apiKey;
442 } else {
443 $keyWithPrefix = $prefix . ' ' . $apiKey;
444 }
445
446 return $keyWithPrefix;
447 }
448
449 /**
450 * Returns an array of host settings
451 *
452 * @return array an array of host settings
453 */
454 public function getHostSettings() {
455 return [
456 [
457 "url" => "https://plausible.io",
458 "description" => "This server",
459 ]
460 ];
461 }
462
463 /**
464 * Returns URL based on host settings, index and variables
465 *
466 * @param array $hostSettings array of host settings, generated from getHostSettings() or equivalent from the API clients
467 * @param int $hostIndex index of the host settings
468 * @param array|null $variables hash of variable and the corresponding value (optional)
469 *
470 * @return string URL based on host settings
471 */
472 public static function getHostString( array $hostsSettings, $hostIndex, ?array $variables = null ) {
473 if ( null === $variables ) {
474 $variables = [];
475 }
476
477 // check array index out of bound
478 if ( $hostIndex < 0 || $hostIndex >= count( $hostsSettings ) ) {
479 throw new \InvalidArgumentException( "Invalid index $hostIndex when selecting the host. Must be less than " . count( $hostsSettings ) );
480 }
481
482 $host = $hostsSettings[ $hostIndex ];
483 $url = $host["url"];
484
485 // go through variable and assign a value
486 foreach ( $host["variables"] ?? [] as $name => $variable ) {
487 if ( array_key_exists( $name, $variables ) ) { // check to see if it's in the variables provided by the user
488 if ( ! isset( $variable['enum_values'] ) || in_array( $variables[ $name ], $variable["enum_values"], true ) ) { // check to see if the value is in the enum
489 $url = str_replace( "{" . $name . "}", $variables[ $name ], $url );
490 } else {
491 throw new \InvalidArgumentException( "The variable `$name` in the host URL has invalid value " . $variables[ $name ] . ". Must be " . join( ',', $variable["enum_values"] ) . "." );
492 }
493 } else {
494 // use default value
495 $url = str_replace( "{" . $name . "}", $variable["default_value"], $url );
496 }
497 }
498
499 return $url;
500 }
501
502 /**
503 * Returns URL based on the index and variables
504 *
505 * @param int $index index of the host settings
506 * @param array|null $variables hash of variable and the corresponding value (optional)
507 *
508 * @return string URL based on host settings
509 */
510 public function getHostFromSettings( $index, $variables = null ) {
511 return self::getHostString( $this->getHostSettings(), $index, $variables );
512 }
513 }
514