| @@ -1,0 +1,71 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace Metricool\Http\Endpoints; | |
| 6 | + | |
| 7 | +use Metricool\Http\Metricool\MetricoolApi; | |
| 8 | +use Metricool\Interfaces\SingleEndpointInterface; | |
| 9 | +use Metricool\Services\DashboardService; | |
| 10 | +use Metricool\Traits\HasAllowlistControl; | |
| 11 | +use Metricool\Traits\HasRestAccess; | |
| 12 | + | |
| 13 | +class LogoutEndpoint implements SingleEndpointInterface | |
| 14 | +{ | |
| 15 | + use HasRestAccess; | |
| 16 | + use HasAllowlistControl; | |
| 17 | + | |
| 18 | + public const ROUTE = 'logout'; | |
| 19 | + | |
| 20 | + private MetricoolApi $metricoolApi; | |
| 21 | + private DashboardService $dashboard; | |
| 22 | + | |
| 23 | + public function __construct(MetricoolApi $metricoolApi, DashboardService $dashboard) | |
| 24 | + { | |
| 25 | + $this->metricoolApi = $metricoolApi; | |
| 26 | + $this->dashboard = $dashboard; | |
| 27 | + } | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * @inheritDoc | |
| 31 | + */ | |
| 32 | + public function registerRoute(): string | |
| 33 | + { | |
| 34 | + return self::ROUTE; | |
| 35 | + } | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * @inheritDoc | |
| 39 | + */ | |
| 40 | + public function enabled(): bool | |
| 41 | + { | |
| 42 | + return true; | |
| 43 | + } | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * @inheritDoc | |
| 47 | + */ | |
| 48 | + public function registerArguments(): array | |
| 49 | + { | |
| 50 | + return [ | |
| 51 | + 'methods' => \WP_REST_Server::EDITABLE, | |
| 52 | + 'callback' => [$this, 'callback'], | |
| 53 | + 'middleware' => ['metricool:auth'], | |
| 54 | + ]; | |
| 55 | + } | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * Log the user out and redirect to the dashboard | |
| 59 | + * | |
| 60 | + * POST /wp-json/metricool/v1/logout | |
| 61 | + */ | |
| 62 | + public function callback(\WP_REST_Request $request): \WP_REST_Response | |
| 63 | + { | |
| 64 | + $this->metricoolApi->logout(); | |
| 65 | + | |
| 66 | + return $this->sendHttpResponse([ | |
| 67 | + 'state' => $this->dashboard->state(), | |
| 68 | + 'mode' => $this->dashboard->mode(), | |
| 69 | + ]); | |
| 70 | + } | |
| 71 | +} | |