PluginProbe
Parse.ly / 3.19.2
Parse.ly v3.19.2
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / src / services / class-base-api-service.php

class-base-api-service.php in Parse.ly 3.19.2, at src/services/class-base-api-service.php

133 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * External API Service base class.
4 *
5 * @package Parsely
6 * @since 3.17.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\Services;
12
13 use Parsely\Parsely;
14
15 /**
16 * The base class for the external API services.
17 *
18 * An external API service is a service that interacts with an external API,
19 * such as the Parse.ly Content API or the Parse.ly Suggestions API.
20 *
21 * @since 3.17.0
22 */
23 abstract class Base_API_Service {
24 /**
25 * The registered endpoints for this service.
26 *
27 * @since 3.17.0
28 *
29 * @var array<string, Base_Service_Endpoint>
30 */
31 protected $endpoints;
32
33 /**
34 * The Parsely instance.
35 *
36 * @since 3.17.0
37 *
38 * @var Parsely
39 */
40 private $parsely;
41
42 /**
43 * Initializes the class.
44 *
45 * @since 3.17.0
46 *
47 * @param Parsely $parsely The Parsely instance.
48 */
49 public function __construct( Parsely $parsely ) {
50 $this->parsely = $parsely;
51 $this->register_endpoints();
52 }
53
54 /**
55 * Registers an endpoint with the service.
56 *
57 * @since 3.17.0
58 *
59 * @param Base_Service_Endpoint $endpoint The endpoint to register.
60 */
61 protected function register_endpoint( Base_Service_Endpoint $endpoint ): void {
62 $this->endpoints[ $endpoint->get_endpoint() ] = $endpoint;
63 }
64
65 /**
66 * Registers a cached endpoint with the service.
67 *
68 * @since 3.17.0
69 *
70 * @param Base_Service_Endpoint $endpoint The endpoint to register.
71 * @param int $ttl The time-to-live for the cache, in seconds.
72 */
73 protected function register_cached_endpoint( Base_Service_Endpoint $endpoint, int $ttl ): void {
74 $this->endpoints[ $endpoint->get_endpoint() ] = new Cached_Service_Endpoint( $endpoint, $ttl );
75 }
76
77 /**
78 * Gets an endpoint by name.
79 *
80 * @since 3.17.0
81 *
82 * @param string $endpoint The name of the endpoint.
83 * @return Base_Service_Endpoint The endpoint.
84 */
85 public function get_endpoint( string $endpoint ): Base_Service_Endpoint {
86 return $this->endpoints[ $endpoint ];
87 }
88
89 /**
90 * Returns the base URL for the API service.
91 *
92 * This method should be overridden in the child class to return the base URL
93 * for the API service.
94 *
95 * @since 3.17.0
96 *
97 * @return string
98 */
99 abstract public static function get_base_url(): string;
100
101 /**
102 * Registers the endpoints for the service.
103 *
104 * This method should be overridden in the child class to register the
105 * endpoints for the service.
106 *
107 * @since 3.17.0
108 */
109 abstract protected function register_endpoints(): void;
110
111 /**
112 * Returns the API URL for the service.
113 *
114 * @since 3.17.0
115 *
116 * @return string
117 */
118 public function get_api_url(): string {
119 return static::get_base_url();
120 }
121
122 /**
123 * Returns the Parsely instance.
124 *
125 * @since 3.17.0
126 *
127 * @return Parsely
128 */
129 public function get_parsely(): Parsely {
130 return $this->parsely;
131 }
132 }
133