ApiConnectionsController.php
3 months ago
EmailsController.php
3 months ago
MembershipsController.php
3 months ago
PagesController.php
3 months ago
SectionsController.php
3 months ago
StatisticsController.php
3 months ago
UsersController.php
3 months ago
ApiConnectionsController.php
135 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FapiMember\Api\V2\Endpoints; |
| 4 | |
| 5 | use FapiMember\Api\V2\ApiController; |
| 6 | use FapiMember\Container\Container; |
| 7 | use FapiMember\FapiMemberPlugin; |
| 8 | use FapiMember\Library\SmartEmailing\Types\StringType; |
| 9 | use FapiMember\Model\Enums\Alert; |
| 10 | use FapiMember\Model\Enums\Keys\OptionKey; |
| 11 | use FapiMember\Model\Enums\Types\RequestMethodType; |
| 12 | use FapiMember\Service\ApiService; |
| 13 | use Throwable; |
| 14 | use WP_REST_Request; |
| 15 | |
| 16 | class ApiConnectionsController |
| 17 | { |
| 18 | private ApiService $apiService; |
| 19 | private ApiController $apiController; |
| 20 | |
| 21 | public function __construct() |
| 22 | { |
| 23 | $this->apiService = Container::get(ApiService::class); |
| 24 | $this->apiController = Container::get(ApiController::class); |
| 25 | } |
| 26 | |
| 27 | public function list(): array |
| 28 | { |
| 29 | $clients = $this->apiService->getApiClients(); |
| 30 | $connectionsData = []; |
| 31 | |
| 32 | foreach ($clients as $client) { |
| 33 | $connection = $client->getConnection(); |
| 34 | |
| 35 | if ($connection->getApiKey() === null || $connection->getApiKey() === null) { |
| 36 | continue; |
| 37 | } |
| 38 | |
| 39 | $connectionData = array_merge($connection->toArray(), $client->getLicenceData()); |
| 40 | $connectionData['billing'] = $client->getBillingData(); |
| 41 | |
| 42 | $connectionsData[] = $connectionData; |
| 43 | } |
| 44 | |
| 45 | return $connectionsData; |
| 46 | } |
| 47 | |
| 48 | public function getApiToken(): array |
| 49 | { |
| 50 | return ['apiToken' => $this->apiService->getApiToken()]; |
| 51 | } |
| 52 | |
| 53 | public function getStatusForAll(): array |
| 54 | { |
| 55 | return $this->apiService->getCredentialsStatuses(); |
| 56 | } |
| 57 | |
| 58 | public function create(array $body): void |
| 59 | { |
| 60 | $apiUser = $this->apiController->extractParamOrNull($body, 'api_user', StringType::class); |
| 61 | $apiKey = $this->apiController->extractParamOrNull($body, 'api_key', StringType::class); |
| 62 | |
| 63 | if ( $apiKey === null || $apiUser === null ) { |
| 64 | $this->apiController->callbackError([], Alert::API_FORM_EMPTY); |
| 65 | } |
| 66 | |
| 67 | update_option(OptionKey::API_USER, $apiUser); |
| 68 | update_option(OptionKey::API_KEY, $apiKey); |
| 69 | |
| 70 | $credentials = json_decode(get_option(OptionKey::API_CREDENTIALS)); |
| 71 | |
| 72 | if (wp_list_filter( $credentials, ['username' => $apiUser]) |
| 73 | && wp_list_filter($credentials, ['token' => $apiKey]) |
| 74 | ) { |
| 75 | $this->apiController->callbackError([], Alert::API_FORM_CREDENTIALS_EXIST); |
| 76 | } |
| 77 | |
| 78 | if (empty($credentials)) { |
| 79 | $credentials = [['username' => $apiUser, 'token' => $apiKey]]; |
| 80 | } elseif (count($credentials) < FapiMemberPlugin::CONNECTED_API_KEYS_LIMIT) { |
| 81 | $credentials[] = ['username' => $apiUser, 'token' => $apiKey]; |
| 82 | } else { |
| 83 | $this->apiController->callbackError([], Alert::API_FORM_TOO_MANY_CREDENTIALS); |
| 84 | } |
| 85 | |
| 86 | update_option(OptionKey::API_CREDENTIALS, json_encode($credentials)); |
| 87 | $credentialsValid = $this->apiService->checkCredentials(); |
| 88 | update_option(OptionKey::API_CHECKED, $credentialsValid); |
| 89 | $webUrl = rtrim(get_site_url(), '/' ) . '/'; |
| 90 | |
| 91 | foreach ($this->apiService->getApiClients() as $apiClient) { |
| 92 | $connection = $apiClient->getConnection(); |
| 93 | |
| 94 | if ($connection === null) { |
| 95 | $connection = $this->apiService->createConnection($webUrl, $apiClient); |
| 96 | $apiClient->setConnection($connection); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if ($credentialsValid) { |
| 101 | $this->apiController->callbackResponse([], Alert::API_FORM_SUCCESS); |
| 102 | } else { |
| 103 | array_pop($credentials); |
| 104 | update_option(OptionKey::API_CREDENTIALS, json_encode($credentials)); |
| 105 | update_option( |
| 106 | OptionKey::API_CHECKED, |
| 107 | $this->apiService->checkCredentials(), |
| 108 | ); |
| 109 | |
| 110 | $this->apiController->callbackError([], Alert::API_FORM_ERROR); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | |
| 115 | public function remove(WP_REST_Request $request): void |
| 116 | { |
| 117 | $this->apiController->checkRequestMethod($request, RequestMethodType::POST); |
| 118 | $body = json_decode($request->get_body(), true); |
| 119 | |
| 120 | $apiKey = $this->apiController->extractParam($body, 'api_key', StringType::class); |
| 121 | |
| 122 | try { |
| 123 | $this->apiService->removeCredentials($apiKey); |
| 124 | } catch (Throwable) { |
| 125 | $this->apiController->callbackError([ |
| 126 | 'class'=> self::class, |
| 127 | 'description' => "Failed to remove connection.", |
| 128 | ]); |
| 129 | } |
| 130 | |
| 131 | $this->apiController->callbackResponse([], Alert::API_FORM_CREDENTIALS_REMOVED); |
| 132 | } |
| 133 | |
| 134 | } |
| 135 |