Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MOL-1276: add store api config route for frontend components #660

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/Controller/StoreApi/Config/ConfigControllerBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Kiener\MolliePayments\Controller\StoreApi\Config;

use Kiener\MolliePayments\Controller\StoreApi\Config\Response\ConfigResponse;
use Kiener\MolliePayments\Service\ConfigService;
use Kiener\MolliePayments\Service\SalesChannel\SalesChannelLocale;
use Kiener\MolliePayments\Service\SettingsService;
use Psr\Log\LoggerInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SalesChannel\StoreApiResponse;
use Symfony\Component\Routing\Annotation\Route;

class ConfigControllerBase
{

/**
* @var SettingsService
*/
private $settingsService;

/**
* @var ConfigService
*/
private $configService;

/**
* @var SalesChannelLocale
*/
private $salesChannelLocale;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param SettingsService $settingsService
* @param ConfigService $configService
* @param SalesChannelLocale $salesChannelLocale
* @param LoggerInterface $logger
*/
public function __construct(SettingsService $settingsService, ConfigService $configService, SalesChannelLocale $salesChannelLocale, LoggerInterface $logger)
{
$this->settingsService = $settingsService;
$this->configService = $configService;
$this->salesChannelLocale = $salesChannelLocale;
$this->logger = $logger;
}


/**
* @Route("/store-api/mollie/config", name="store-api.mollie.config", methods={"GET"})
*
* @param SalesChannelContext $context
* @throws \Exception
* @return StoreApiResponse
*/
public function getConfig(SalesChannelContext $context): StoreApiResponse
{
try {
$scId = $context->getSalesChannelId();

$settings = $this->settingsService->getSettings($scId);

$profileId = (string)$settings->getProfileId();
$locale = $this->salesChannelLocale->getLocale($context);

if (empty($profileId)) {
# if its somehow not yet loaded (plugin config in admin when clicking save)
# then load it right now
$this->configService->fetchProfileId($scId);

$settings = $this->settingsService->getSettings($scId);
$profileId = (string)$settings->getProfileId();
}

return new ConfigResponse(
$profileId,
$settings->isTestMode(),
$locale
);
} catch (\Exception $e) {
$this->logger->error(
'Error when fetching config in Store API: ' . $e->getMessage(),
[
'error' => $e,
]
);

throw $e;
}
}
}
34 changes: 34 additions & 0 deletions src/Controller/StoreApi/Config/Response/ConfigResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Kiener\MolliePayments\Controller\StoreApi\Config\Response;

use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\SalesChannel\StoreApiResponse;

class ConfigResponse extends StoreApiResponse
{
/**
* @var ArrayStruct<mixed, mixed>
*/
protected $object;


/**
* @param string $profileId
* @param bool $isTestMode
* @param string $defaultLocale
*/
public function __construct(string $profileId, bool $isTestMode, string $defaultLocale)
{
$this->object = new ArrayStruct(
[
'profileId' => $profileId,
'testMode' => $isTestMode,
'locale' => $defaultLocale,
],
'mollie_payments_config'
);

parent::__construct($this->object);
}
}
14 changes: 14 additions & 0 deletions src/Controller/StoreApi/Config/Sw6/ConfigController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Kiener\MolliePayments\Controller\StoreApi\Config\Sw6;

use Kiener\MolliePayments\Controller\StoreApi\Config\ConfigControllerBase;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Symfony\Component\Routing\Annotation\Route;

/**
* @RouteScope(scopes={"store-api"})
*/
class ConfigController extends ConfigControllerBase
{
}
13 changes: 13 additions & 0 deletions src/Controller/StoreApi/Config/Sw65/ConfigController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Kiener\MolliePayments\Controller\StoreApi\Config\Sw65;

use Kiener\MolliePayments\Controller\StoreApi\Config\ConfigControllerBase;
use Symfony\Component\Routing\Annotation\Route;

/**
* @Route(defaults={"_routeScope"={"store-api"}})
*/
class ConfigController extends ConfigControllerBase
{
}
18 changes: 18 additions & 0 deletions src/Repository/Language/LanguageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\Language\LanguageEntity;

class LanguageRepository implements LanguageRepositoryInterface
{
Expand All @@ -32,4 +34,20 @@ public function search(Criteria $criteria, Context $context): EntitySearchResult
{
return $this->languageRepository->search($criteria, $context);
}

/**
* @param string $languageId
* @param Context $context
* @return null|LanguageEntity
*/
public function findById(string $languageId, Context $context): ?LanguageEntity
{
$languageCriteria = new Criteria();
$languageCriteria->addAssociation('locale');
$languageCriteria->addFilter(new EqualsFilter('id', $languageId));

$languagesResult = $this->search($languageCriteria, $context);

return $languagesResult->first();
}
}
8 changes: 8 additions & 0 deletions src/Repository/Language/LanguageRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\System\Language\LanguageEntity;

interface LanguageRepositoryInterface
{
Expand All @@ -15,4 +16,11 @@ interface LanguageRepositoryInterface
* @return EntitySearchResult
*/
public function search(Criteria $criteria, Context $context): EntitySearchResult;

/**
* @param string $languageId
* @param Context $context
* @return null|LanguageEntity
*/
public function findById(string $languageId, Context $context): ?LanguageEntity;
}
7 changes: 7 additions & 0 deletions src/Resources/config/compatibility/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@
<!-- ////////////////////////////////////////////////////////////////////////////////////////////////////////////// -->
<!-- Store API Controller -->

<service id="Kiener\MolliePayments\Controller\StoreApi\Config\Sw6\ConfigController" public="true">
<argument type="service" id="Kiener\MolliePayments\Service\SettingsService"/>
<argument type="service" id="Kiener\MolliePayments\Service\ConfigService"/>
<argument type="service" id="Kiener\MolliePayments\Service\SalesChannel\SalesChannelLocale"/>
<argument type="service" id="mollie_payments.logger"/>
</service>

<service id="Kiener\MolliePayments\Controller\StoreApi\ApplePayDirect\Sw6\ApplePayDirectController"
public="true">
<argument type="service" id="Kiener\MolliePayments\Components\ApplePayDirect\ApplePayDirect"/>
Expand Down
22 changes: 17 additions & 5 deletions src/Resources/config/compatibility/controller_6.5.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,16 @@
</call>
</service>

<service id="Kiener\MolliePayments\Controller\Storefront\ApplePayDirect\Sw65\ApplePayDirectController" public="true">
<service id="Kiener\MolliePayments\Controller\Storefront\ApplePayDirect\Sw65\ApplePayDirectController"
public="true">
<argument type="service" id="Kiener\MolliePayments\Components\ApplePayDirect\ApplePayDirect"/>
<argument type="service" id="router"/>
<argument type="service" id="mollie_payments.logger"/>
<argument type="service" id="Kiener\MolliePayments\Service\Cart\CartBackupService"/>
<argument type="service" id="session.flash_bag" on-invalid="null"/>
<argument type="service" id="Kiener\MolliePayments\Compatibility\Bundles\FlowBuilder\FlowBuilderFactory"/>
<argument type="service" id="Kiener\MolliePayments\Compatibility\Bundles\FlowBuilder\FlowBuilderEventFactory"/>
<argument type="service"
id="Kiener\MolliePayments\Compatibility\Bundles\FlowBuilder\FlowBuilderEventFactory"/>
<argument type="service" id="Kiener\MolliePayments\Repository\Customer\CustomerRepository"/>
<argument type="service" id="Kiener\MolliePayments\Service\OrderService"/>
<call method="setContainer">
Expand All @@ -176,7 +178,8 @@
</service>

<service id="Kiener\MolliePayments\Controller\Storefront\Account\Sw65\AccountController" public="true">
<argument type="service" id="Kiener\MolliePayments\Components\Subscription\Page\Account\SubscriptionPageLoader"/>
<argument type="service"
id="Kiener\MolliePayments\Components\Subscription\Page\Account\SubscriptionPageLoader"/>
<argument type="service" id="Kiener\MolliePayments\Components\Subscription\SubscriptionManager"/>
<argument type="service" id="mollie_payments.logger"/>
<call method="setContainer">
Expand All @@ -190,7 +193,15 @@
<!-- ////////////////////////////////////////////////////////////////////////////////////////////////////////////// -->
<!-- Store API Controller -->

<service id="Kiener\MolliePayments\Controller\StoreApi\ApplePayDirect\Sw65\ApplePayDirectController" public="true">
<service id="Kiener\MolliePayments\Controller\StoreApi\Config\Sw65\ConfigController" public="true">
<argument type="service" id="Kiener\MolliePayments\Service\SettingsService"/>
<argument type="service" id="Kiener\MolliePayments\Service\ConfigService"/>
<argument type="service" id="Kiener\MolliePayments\Service\SalesChannel\SalesChannelLocale"/>
<argument type="service" id="mollie_payments.logger"/>
</service>

<service id="Kiener\MolliePayments\Controller\StoreApi\ApplePayDirect\Sw65\ApplePayDirectController"
public="true">
<argument type="service" id="Kiener\MolliePayments\Components\ApplePayDirect\ApplePayDirect"/>
<argument type="service" id="mollie_payments.logger"/>
</service>
Expand All @@ -207,7 +218,8 @@

<service id="Kiener\MolliePayments\Controller\StoreApi\Subscription\Sw65\SubscriptionController" public="true">
<argument type="service" id="Kiener\MolliePayments\Components\Subscription\SubscriptionManager"/>
<argument type="service" id="Kiener\MolliePayments\Components\Subscription\DAL\Repository\SubscriptionRepository"/>
<argument type="service"
id="Kiener\MolliePayments\Components\Subscription\DAL\Repository\SubscriptionRepository"/>
<argument type="service" id="mollie_payments.logger"/>
</service>

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@
<service id="Kiener\MolliePayments\Subscriber\CheckoutConfirmPageSubscriber" class="Kiener\MolliePayments\Subscriber\CheckoutConfirmPageSubscriber">
<argument type="service" id="Kiener\MolliePayments\Factory\MollieApiFactory"/>
<argument type="service" id="Kiener\MolliePayments\Service\SettingsService"/>
<argument type="service" id="Kiener\MolliePayments\Repository\Language\LanguageRepository"/>
<argument type="service" id="Kiener\MolliePayments\Service\MandateService"/>
<argument type="service" id="Kiener\MolliePayments\Gateway\Mollie\MollieGateway"/>
<argument type="service" id="Kiener\MolliePayments\Service\SalesChannel\SalesChannelLocale"/>
<tag name="kernel.event_subscriber"/>
</service>

Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/services/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@
<service id="Kiener\MolliePayments\Service\SalesChannel\SalesChannelDataExtractor">
</service>

<service id="Kiener\MolliePayments\Service\SalesChannel\SalesChannelLocale">
<argument type="service" id="Kiener\MolliePayments\Repository\Language\LanguageRepository"/>
</service>

<!-- Mail -->


Expand Down
1 change: 1 addition & 0 deletions src/Service/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kiener\MolliePayments\Service;

use Kiener\MolliePayments\Gateway\MollieGatewayInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;

class ConfigService
Expand Down
79 changes: 79 additions & 0 deletions src/Service/SalesChannel/SalesChannelLocale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Kiener\MolliePayments\Service\SalesChannel;

use Kiener\MolliePayments\Repository\Language\LanguageRepositoryInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;

class SalesChannelLocale
{
public const AVAILABLE_LOCALES = [
'en_US',
'en_GB',
'nl_NL',
'fr_FR',
'it_IT',
'de_DE',
'de_AT',
'de_CH',
'es_ES',
'ca_ES',
'nb_NO',
'pt_PT',
'sv_SE',
'fi_FI',
'da_DK',
'is_IS',
'hu_HU',
'pl_PL',
'lv_LV',
'lt_LT'
];


/**
* @var LanguageRepositoryInterface
*/
private $repoLanguages;


/**
* @param LanguageRepositoryInterface $repoLanguages
*/
public function __construct(LanguageRepositoryInterface $repoLanguages)
{
$this->repoLanguages = $repoLanguages;
}


/**
* @param SalesChannelContext $salesChannelContext
* @return string
*/
public function getLocale(SalesChannelContext $salesChannelContext): string
{
# Get the language object from the sales channel context.
$locale = '';

$salesChannel = $salesChannelContext->getSalesChannel();
$languageId = $salesChannel->getLanguageId();

$language = $this->repoLanguages->findById($languageId, $salesChannelContext->getContext());

if ($language !== null && $language->getLocale() !== null) {
$locale = $language->getLocale()->getCode();
}

# Set the locale based on the current storefront.
if ($locale !== null && $locale !== '') {
$locale = str_replace('-', '_', $locale);
}

# Check if the shop locale is available.
if ($locale === '' || !in_array($locale, self::AVAILABLE_LOCALES, true)) {
$locale = 'en_GB';
}

return $locale;
}
}
Loading
Loading