-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MOL-1276: add store api config route for frontend components
- Loading branch information
1 parent
9dc38d8
commit ded64e6
Showing
17 changed files
with
547 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
src/Controller/StoreApi/Config/Response/ConfigResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.