PluginProbe
Metricool – Social media and site statistics / 2.0.2
Metricool – Social media and site statistics v2.0.2
2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
metricool / app / Http / Endpoints / ConnectedNetworksEndpoint.php

ConnectedNetworksEndpoint.php in Metricool – Social media and site statistics 2.0.2, at app/Http/Endpoints/ConnectedNetworksEndpoint.php

84 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Metricool\Http\Endpoints;
6
7 use GuzzleHttp\Exception\GuzzleException;
8 use Metricool\Http\Endpoints\Responses\ConnectedNetworksResponse;
9 use Metricool\Http\Metricool\MetricoolApi;
10 use Metricool\Interfaces\SingleEndpointInterface;
11 use Metricool\Traits\HasAllowlistControl;
12 use Metricool\Traits\HasRestAccess;
13
14 class ConnectedNetworksEndpoint implements SingleEndpointInterface
15 {
16 use HasRestAccess;
17 use HasAllowlistControl;
18
19 public const ROUTE = 'connected_networks';
20
21 public MetricoolApi $metricoolApi;
22
23 public function __construct(MetricoolApi $metricoolApi)
24 {
25 $this->metricoolApi = $metricoolApi;
26 }
27
28 /**
29 * @inheritDoc
30 */
31 public function registerRoute(): string
32 {
33 return self::ROUTE;
34 }
35
36 /**
37 * @inheritDoc
38 */
39 public function enabled(): bool
40 {
41 return $this->adminAccessAllowed();
42 }
43
44 /**
45 * @inheritDoc
46 */
47 public function registerArguments(): array
48 {
49 return [
50 'methods' => \WP_REST_Server::READABLE,
51 'callback' => [$this, 'callback'],
52 'middleware' => ['metricool:auth', 'metricool:blog_id'],
53 ];
54 }
55
56 /**
57 * Return the brands related to the user
58 */
59 public function callback(\WP_REST_Request $request): \WP_REST_Response
60 {
61 try {
62 $response = $this->buildResponse($request);
63 } catch (\Exception $e) {
64 return $this->sendHttpErrorResponse(__('Failed to load brands data', 'metricool'), $e->getMessage(), $e->getCode());
65 }
66
67 return $this->sendHttpResponse($response);
68 }
69
70 /**
71 * Build the specific ConnectedNetworksResponse response for the endpoint.
72 * This response returns just the brand names that are connected to the user.
73 * Filtering it server side prevents client-side complexity.
74 * @throws GuzzleException
75 */
76 public function buildResponse(\WP_REST_Request $request): array
77 {
78 $connectedBrand = $this->metricoolApi->connectedBrands()->get();
79 $response = new ConnectedNetworksResponse($connectedBrand);
80
81 return $response->body();
82 }
83 }
84