Skip to content

Commit

Permalink
Merge pull request #142 from alma/feature/ecom-2116-php-client-create…
Browse files Browse the repository at this point in the history
…-endpoint-that-returns-data

Create endpoint and formatter for gather cms data
  • Loading branch information
joyet-simon authored Oct 22, 2024
2 parents ea76db2 + 8ed5363 commit 6c6ff13
Show file tree
Hide file tree
Showing 8 changed files with 675 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Endpoints/Merchants.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@

use Alma\API\Entities\FeePlan;
use Alma\API\Entities\Merchant;
use Alma\API\Entities\MerchantData\MerchantData;
use Alma\API\Exceptions\RequestException;
use Alma\API\RequestError;

class Merchants extends Base
{
const MERCHANTS_PATH = '/v1/merchants';
const ME_PATH = '/v1/me';

/**
Expand Down Expand Up @@ -81,4 +82,20 @@ public function feePlans($kind = FeePlan::KIND_GENERAL, $installmentsCounts = "a
return new FeePlan($val);
}, $res->json);
}

/**
* @param string $url The URL to send to Alma for integrations configuration
* @throws RequestException
* @throws RequestError
*/
public function sendIntegrationsConfigurationsUrl($url)
{
$res = $this->request(self::ME_PATH . "/configuration")->setRequestBody(array(
"endpoint_url" => $url
))->post();

if ($res->isError()) {
throw new RequestException($res->errorMessage, null, $res);
}
}
}
121 changes: 121 additions & 0 deletions src/Entities/MerchantData/CmsFeatures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace Alma\API\Entities\MerchantData;

class CmsFeatures
{
/**
* @var bool | null
*/
private $almaEnabled;

/**
* @var bool | null
*/
private $widgetCartActivated;

/**
* @var bool | null
*/
private $widgetProductActivated;

/**
* @var array
*/
private $usedFeePlans;

/**
* @var int | null
*/
private $paymentMethodPosition;

/**
* @var bool | null
*/
private $inPageActivated;

/**
* @var bool | null
*/
private $logActivated;

/**
* @var string[]
*/
private $excludedCategories;

/**
* @var bool | null
*/
private $excludedCategoriesActivated;

/**
* @var array<array{name: string}>
*/
private $specificFeatures;

/**
* @var string[]
*/
private $countryRestriction;

/**
* @var bool | null
*/
private $isMultisite;

/**
* @var bool | null
*/
private $customWidgetCss;

/**
* CmsFeatures constructor.
* @param array $cmsFeaturesDataArray
*/
public function __construct($cmsFeaturesDataArray)
{
// Ensure values are properly initialized
$this->almaEnabled = isset($cmsFeaturesDataArray['alma_enabled']) ? $cmsFeaturesDataArray['alma_enabled'] : null;
$this->widgetCartActivated = isset($cmsFeaturesDataArray['widget_cart_activated']) ? $cmsFeaturesDataArray['widget_cart_activated'] : null;
$this->widgetProductActivated = isset($cmsFeaturesDataArray['widget_product_activated']) ? $cmsFeaturesDataArray['widget_product_activated'] : null;
$this->usedFeePlans = isset($cmsFeaturesDataArray['used_fee_plans']) ? $cmsFeaturesDataArray['used_fee_plans'] : [];
$this->inPageActivated = isset($cmsFeaturesDataArray['in_page_activated']) ? $cmsFeaturesDataArray['in_page_activated'] : null;
$this->logActivated = isset($cmsFeaturesDataArray['log_activated']) ? $cmsFeaturesDataArray['log_activated'] : null;
$this->excludedCategories = isset($cmsFeaturesDataArray['excluded_categories']) ? $cmsFeaturesDataArray['excluded_categories'] : [];
$this->excludedCategoriesActivated = isset($cmsFeaturesDataArray['excluded_categories_activated']) ?
$cmsFeaturesDataArray['excluded_categories_activated'] : null;
$this->paymentMethodPosition = isset($cmsFeaturesDataArray['payment_method_position']) ? $cmsFeaturesDataArray['payment_method_position'] : null;
$this->specificFeatures = isset($cmsFeaturesDataArray['specific_features']) ? $cmsFeaturesDataArray['specific_features'] : [];
$this->countryRestriction = isset($cmsFeaturesDataArray['country_restriction']) ? $cmsFeaturesDataArray['country_restriction'] : [];
$this->isMultisite = isset($cmsFeaturesDataArray['is_multisite']) ? $cmsFeaturesDataArray['is_multisite'] : null;
$this->customWidgetCss = isset($cmsFeaturesDataArray['custom_widget_css']) ? $cmsFeaturesDataArray['custom_widget_css'] : null;
}

/**
* @return array
*/
public function getProperties()
{
// Use array_filter with ARRAY_FILTER_USE_BOTH to remove null or empty values
return array_filter([
'alma_enabled' => $this->almaEnabled,
'widget_cart_activated' => $this->widgetCartActivated,
'widget_product_activated' => $this->widgetProductActivated,
'used_fee_plans' => $this->usedFeePlans,
'in_page_activated' => $this->inPageActivated,
'log_activated' => $this->logActivated,
'excluded_categories' => $this->excludedCategories,
'excluded_categories_activated' => $this->excludedCategoriesActivated,
'payment_method_position' => $this->paymentMethodPosition,
'specific_features' => $this->specificFeatures,
'country_restriction' => $this->countryRestriction,
'is_multisite' => $this->isMultisite,
'custom_widget_css' => $this->customWidgetCss,
], function ($value) {
// Keep only values that are not null and not empty
// But keep false or 0 values
return !is_null($value) && $value !== '';
});
}
}
92 changes: 92 additions & 0 deletions src/Entities/MerchantData/CmsInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Alma\API\Entities\MerchantData;

class CmsInfo
{
/**
* @var string
*/
private $cmsName;

/**
* @var string
*/
private $cmsVersion;

/**
* @var array<array{name: string, version: string}>
*/
private $thirdPartiesPlugins;

/**
* @var array<array{name: string, version: string}>
*/
private $themes;

/**
* @var string
*/
private $languageName;

/**
* @var string
*/
private $languageVersion;

/**
* @var string
*/
private $almaPluginVersion;

/**
* @var string
*/
private $almaSdkVersion;

/**
* @var string
*/
private $almaSdkName;

/**
* CmsInfo constructor.
* @param array $cmsInfoDataArray
*/
public function __construct($cmsInfoDataArray)
{
// Initialize values or set them to null if not available
$this->cmsName = isset($cmsInfoDataArray['cms_name']) ? $cmsInfoDataArray['cms_name'] : '';
$this->cmsVersion = isset($cmsInfoDataArray['cms_version']) ? $cmsInfoDataArray['cms_version'] : '';
$this->thirdPartiesPlugins = isset($cmsInfoDataArray['third_parties_plugins']) ? $cmsInfoDataArray['third_parties_plugins'] : [];
$this->themes = isset($cmsInfoDataArray['themes']) ? $cmsInfoDataArray['themes'] : [];
$this->languageName = isset($cmsInfoDataArray['language_name']) ? $cmsInfoDataArray['language_name'] : '';
$this->languageVersion = isset($cmsInfoDataArray['language_version']) ? $cmsInfoDataArray['language_version'] : '';
$this->almaPluginVersion = isset($cmsInfoDataArray['alma_plugin_version']) ? $cmsInfoDataArray['alma_plugin_version'] : '';
$this->almaSdkVersion = isset($cmsInfoDataArray['alma_sdk_version']) ? $cmsInfoDataArray['alma_sdk_version'] : '';
$this->almaSdkName = isset($cmsInfoDataArray['alma_sdk_name']) ? $cmsInfoDataArray['alma_sdk_name'] : '';
}

/**
* @return array
*/
public function getProperties()
{
// Use array_filter with ARRAY_FILTER_USE_BOTH to remove null or empty values
return array_filter([
'cms_name' => $this->cmsName,
'cms_version' => $this->cmsVersion,
'third_parties_plugins' => $this->thirdPartiesPlugins,
'themes' => $this->themes,
'language_name' => $this->languageName,
'language_version' => $this->languageVersion,
'alma_plugin_version' => $this->almaPluginVersion,
'alma_sdk_version' => $this->almaSdkVersion,
'alma_sdk_name' => $this->almaSdkName,
], function ($value) {
// Keep only values that are not null and not empty
// But keep false or 0 values
return !is_null($value) && $value !== '';
});
}
}
23 changes: 23 additions & 0 deletions src/Lib/PayloadFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Alma\API\Lib;

use Alma\API\Entities\MerchantData\CmsFeatures;
use Alma\API\Entities\MerchantData\CmsInfo;

class PayloadFormatter
{
/**
* @param CmsInfo $cmsInfo
* @param CmsFeatures $cmsFeatures
* @return array
*/
public function formatConfigurationPayload(CmsInfo $cmsInfo, CmsFeatures $cmsFeatures)
{
return [
"cms_info" => $cmsInfo->getProperties(),
"cms_features" => $cmsFeatures->getProperties(),
];
}

}
64 changes: 64 additions & 0 deletions tests/Unit/Endpoints/MerchantsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Unit\Endpoints;

use Alma\API\Endpoints\Merchants;
use Alma\API\Exceptions\RequestException;
use Alma\API\Request;
use Alma\API\Response;
use Mockery;
use PHPUnit\Framework\TestCase;

class MerchantsTest extends TestCase
{
const URL = "https://www.example.com/integrations/configurations";

public function setUp(): void
{
$this->merchantEndpoint = Mockery::mock(Merchants::class)->makePartial();
$this->responseMock = Mockery::mock(Response::class);
$this->requestObject = Mockery::mock(Request::class);
}

public function tearDown(): void
{
$this->merchantEndpoint = null;
$this->responseMock = null;
$this->requestObject = null;
Mockery::close();
}

public function testSendIntegrationsConfigurationsUrlIsOk(){
$this->responseMock->shouldReceive('isError')->once()->andReturn(false);
$this->requestObject->shouldReceive('setRequestBody')
->with(['endpoint_url' => self::URL])
->andReturn($this->requestObject);

$this->merchantEndpoint->shouldReceive('request')
->with(Merchants::ME_PATH . "/configuration")
->once()
->andReturn($this->requestObject);
$this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock);

$url = self::URL;
$this->assertNull($this->merchantEndpoint->sendIntegrationsConfigurationsUrl($url));
}

public function testSendIntegrationsConfigurationsUrlThrowRequestException(){
$this->responseMock->shouldReceive('isError')->once()->andReturn(true);
$this->requestObject->shouldReceive('setRequestBody')
->with(['endpoint_url' => self::URL])
->andReturn($this->requestObject);

$this->merchantEndpoint->shouldReceive('request')
->with(Merchants::ME_PATH . "/configuration")
->once()
->andReturn($this->requestObject);
$this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock);

$url = self::URL;
$this->expectException(RequestException::class);
$this->merchantEndpoint->sendIntegrationsConfigurationsUrl($url);

}
}
Loading

0 comments on commit 6c6ff13

Please sign in to comment.