Skip to content

Commit

Permalink
feat: add languages endpoint (#193)
Browse files Browse the repository at this point in the history
* feat: add languages endpoint

* fix phpcs

* fix
  • Loading branch information
jshah4517 authored May 31, 2024
1 parent 5296b0f commit ce2f274
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 15 deletions.
38 changes: 38 additions & 0 deletions src/Api/Core/Languages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace SupportPal\ApiClient\Api\Core;

use SupportPal\ApiClient\Exception\HttpResponseException;
use SupportPal\ApiClient\Exception\InvalidArgumentException;
use SupportPal\ApiClient\Http\CoreClient;
use SupportPal\ApiClient\Model\Collection;
use SupportPal\ApiClient\Model\Core\Language;

use function array_map;

trait Languages
{
/**
* @param array<mixed> $queryParameters
* @throws HttpResponseException
* @throws InvalidArgumentException
*/
public function getLanguages(array $queryParameters = []): Collection
{
$response = $this->getApiClient()->getLanguages($queryParameters);
$body = $this->decodeBody($response);
$models = array_map([$this, 'createLanguageModel'], $body['data']);

return $this->createCollection($body['count'], $models);
}

/**
* @param array<mixed> $data
*/
private function createLanguageModel(array $data): Language
{
return new Language($data);
}

abstract protected function getApiClient(): CoreClient;
}
2 changes: 2 additions & 0 deletions src/Api/CoreApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use SupportPal\ApiClient\Api\Core\Brands;
use SupportPal\ApiClient\Api\Core\IpBans;
use SupportPal\ApiClient\Api\Core\IpWhitelist;
use SupportPal\ApiClient\Api\Core\Languages;
use SupportPal\ApiClient\Api\Core\Settings;
use SupportPal\ApiClient\Api\Core\SpamRules;
use SupportPal\ApiClient\Http\CoreClient;
Expand All @@ -14,6 +15,7 @@ class CoreApi extends Api
use Brands;
use IpBans;
use IpWhitelist;
use Languages;
use Settings;
use SpamRules;

Expand Down
4 changes: 3 additions & 1 deletion src/Dictionary/ApiDictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ final class ApiDictionary

public const CORE_IP_WHITELIST = 'core/ipwhitelist';

public const CORE_LANGUAGE = 'core/language';

public const CORE_SETTINGS = 'core/settings';

public const CORE_SPAM_RULES = 'core/spamrule';
public const CORE_SPAM_RULE = 'core/spamrule';

/**
* Self Service APIs
Expand Down
22 changes: 22 additions & 0 deletions src/Http/Core/LanguageApis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

namespace SupportPal\ApiClient\Http\Core;

use Psr\Http\Message\ResponseInterface;
use SupportPal\ApiClient\Dictionary\ApiDictionary;
use SupportPal\ApiClient\Exception\HttpResponseException;
use SupportPal\ApiClient\Http\ApiClientAware;

trait LanguageApis
{
use ApiClientAware;

/**
* @param array<mixed> $queryParameters
* @throws HttpResponseException
*/
public function getLanguages(array $queryParameters): ResponseInterface
{
return $this->prepareAndSendGetRequest(ApiDictionary::CORE_LANGUAGE, $queryParameters);
}
}
10 changes: 5 additions & 5 deletions src/Http/Core/SpamRuleApis.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ trait SpamRuleApis
*/
public function getSpamRules(array $queryParameters): ResponseInterface
{
return $this->prepareAndSendGetRequest(ApiDictionary::CORE_SPAM_RULES, $queryParameters);
return $this->prepareAndSendGetRequest(ApiDictionary::CORE_SPAM_RULE, $queryParameters);
}

/**
* @throws HttpResponseException
*/
public function getSpamRule(int $id): ResponseInterface
{
return $this->prepareAndSendGetRequest(ApiDictionary::CORE_SPAM_RULES . '/' . $id, []);
return $this->prepareAndSendGetRequest(ApiDictionary::CORE_SPAM_RULE . '/' . $id, []);
}

/**
Expand All @@ -34,7 +34,7 @@ public function getSpamRule(int $id): ResponseInterface
*/
public function postSpamRule(array $body): ResponseInterface
{
$request = $this->getRequest()->create('POST', ApiDictionary::CORE_SPAM_RULES, [], $body);
$request = $this->getRequest()->create('POST', ApiDictionary::CORE_SPAM_RULE, [], $body);

return $this->sendRequest($request);
}
Expand All @@ -45,7 +45,7 @@ public function postSpamRule(array $body): ResponseInterface
*/
public function putSpamRule(int $id, array $body): ResponseInterface
{
$request = $this->getRequest()->create('PUT', ApiDictionary::CORE_SPAM_RULES . '/' . $id, [], $body);
$request = $this->getRequest()->create('PUT', ApiDictionary::CORE_SPAM_RULE . '/' . $id, [], $body);

return $this->sendRequest($request);
}
Expand All @@ -55,7 +55,7 @@ public function putSpamRule(int $id, array $body): ResponseInterface
*/
public function deleteSpamRule(int $id): ResponseInterface
{
$request = $this->getRequest()->create('DELETE', ApiDictionary::CORE_SPAM_RULES . '/' . $id);
$request = $this->getRequest()->create('DELETE', ApiDictionary::CORE_SPAM_RULE . '/' . $id);

return $this->sendRequest($request);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Http/CoreClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use SupportPal\ApiClient\Http\Core\BrandApis;
use SupportPal\ApiClient\Http\Core\IpBanApis;
use SupportPal\ApiClient\Http\Core\IpWhitelistApis;
use SupportPal\ApiClient\Http\Core\LanguageApis;
use SupportPal\ApiClient\Http\Core\SettingsApis;
use SupportPal\ApiClient\Http\Core\SpamRuleApis;

Expand All @@ -13,6 +14,7 @@ class CoreClient extends Client
use BrandApis;
use IpBanApis;
use IpWhitelistApis;
use LanguageApis;
use SettingsApis;
use SpamRuleApis;
}
21 changes: 21 additions & 0 deletions src/Model/Core/Language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

namespace SupportPal\ApiClient\Model\Core;

use SupportPal\ApiClient\Model\Model;

class Language extends Model
{
/** @var array<string, string> */
protected array $casts = [
'id' => 'int',
'name' => 'string',
'code' => 'string',
'enabled' => 'bool',
'upgrade_available' => 'bool',
'version' => 'string',
'created_at' => 'int',
'updated_at' => 'int',
'formatted_name' => 'string',
];
}
3 changes: 3 additions & 0 deletions test/DataFixtures/ApiCalls/CoreApisData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use SupportPal\ApiClient\Tests\DataFixtures\Core\BrandData;
use SupportPal\ApiClient\Tests\DataFixtures\Core\CoreSettingsData;
use SupportPal\ApiClient\Tests\DataFixtures\Core\IpBanData;
use SupportPal\ApiClient\Tests\DataFixtures\Core\LanguageData;
use SupportPal\ApiClient\Tests\DataFixtures\Core\Request\CreateIpBanData;
use SupportPal\ApiClient\Tests\DataFixtures\Core\Request\CreateSpamRuleData;
use SupportPal\ApiClient\Tests\DataFixtures\Core\Request\CreateWhitelistedIpData;
Expand Down Expand Up @@ -36,6 +37,8 @@ public function getApiCalls(): array
// IP Whitelist
'getWhitelistedIps' => [$whitelistedIpData->getAllResponse(), []],
'getWhitelistedIp' => [$whitelistedIpData->getResponse(), [1]],
// Languages
'getLanguages' => [(new LanguageData)->getAllResponse(), []],
// General Settings
'getSettings' => [(new CoreSettingsData)->getResponse(), []],
// Spam Rules
Expand Down
29 changes: 29 additions & 0 deletions test/DataFixtures/Core/LanguageData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

namespace SupportPal\ApiClient\Tests\DataFixtures\Core;

use SupportPal\ApiClient\Model\Core\Language;
use SupportPal\ApiClient\Tests\DataFixtures\BaseModelData;

class LanguageData extends BaseModelData
{
public const DATA = [
'id' => 1,
'name' => 'English',
'code' => 'en',
'enabled' => 1,
'upgrade_available' => 0,
'version' => '1.0',
'created_at' => 1600753870,
'updated_at' => 1600753870,
'formatted_name' => 'English',
];

/**
* @inheritDoc
*/
public function getModel(): string
{
return Language::class;
}
}
17 changes: 9 additions & 8 deletions test/E2E/CoreApisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ class CoreApisTest extends BaseTestCase
protected function getGetAllEndpoints(): array
{
return [
ApiDictionary::CORE_BRAND => 'getBrands',
ApiDictionary::CORE_IP_BAN => 'getIpBans',
ApiDictionary::CORE_BRAND => 'getBrands',
ApiDictionary::CORE_IP_BAN => 'getIpBans',
ApiDictionary::CORE_IP_WHITELIST => 'getWhitelistedIps',
ApiDictionary::CORE_SPAM_RULES => 'getSpamRules',
ApiDictionary::CORE_LANGUAGE => 'getLanguages',
ApiDictionary::CORE_SPAM_RULE => 'getSpamRules',
];
}

Expand All @@ -29,10 +30,10 @@ protected function getGetAllEndpoints(): array
protected function getGetOneEndpoints(): array
{
return [
ApiDictionary::CORE_BRAND => 'getBrand',
ApiDictionary::CORE_IP_BAN => 'getIpBan',
ApiDictionary::CORE_BRAND => 'getBrand',
ApiDictionary::CORE_IP_BAN => 'getIpBan',
ApiDictionary::CORE_IP_WHITELIST => 'getWhitelistedIp',
ApiDictionary::CORE_SPAM_RULES => 'getSpamRule',
ApiDictionary::CORE_SPAM_RULE => 'getSpamRule',
];
}

Expand All @@ -42,9 +43,9 @@ protected function getGetOneEndpoints(): array
protected function getPostEndpoints(): array
{
return [
ApiDictionary::CORE_IP_BAN => CreateIpBanData::DATA,
ApiDictionary::CORE_IP_BAN => CreateIpBanData::DATA,
ApiDictionary::CORE_IP_WHITELIST => CreateWhitelistedIpData::DATA,
ApiDictionary::CORE_SPAM_RULES => CreateSpamRuleData::DATA,
ApiDictionary::CORE_SPAM_RULE => CreateSpamRuleData::DATA,
];
}

Expand Down
2 changes: 1 addition & 1 deletion test/Unit/Api/Core/BrandApisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testGetBrands(): void
->shouldBeCalled()
->willReturn($response->reveal());

$returnedBrands = $this->api->getBrands([]);
$returnedBrands = $this->api->getBrands();
self::assertEquals($output, $returnedBrands);
}

Expand Down
23 changes: 23 additions & 0 deletions test/Unit/Api/Core/LanguageApisTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace Api\Core;

use SupportPal\ApiClient\Model\Core\Language;
use SupportPal\ApiClient\Tests\DataFixtures\Core\LanguageData;
use SupportPal\ApiClient\Tests\Unit\Api\Core\BaseCoreApiTest;

class LanguageApisTest extends BaseCoreApiTest
{
public function testGetLanguages(): void
{
[$output, $response] = $this->makeCommonExpectations((new LanguageData)->getAllResponse(), Language::class);

$this->apiClient
->getLanguages([])
->shouldBeCalled()
->willReturn($response->reveal());

$returnedModels = $this->api->getLanguages();
self::assertEquals($output, $returnedModels);
}
}

0 comments on commit ce2f274

Please sign in to comment.