diff --git a/config.local.php b/config.local.php index 498dc7f82..c986fa2de 100644 --- a/config.local.php +++ b/config.local.php @@ -44,5 +44,5 @@ 'ps_accounts.oauth2_url' => 'https://oauth.prestashop.local', 'ps_accounts.testimonials_url' => 'https://assets.prestashop3.com/dst/accounts/assets/testimonials.json', - 'ps_accounts.log_level' => 'DEBUG', + 'ps_accounts.log_level' => 'ERROR', ]; diff --git a/src/Account/CommandHandler/DeleteUserShopHandler.php b/src/Account/CommandHandler/DeleteUserShopHandler.php index c4ffcefe4..d13c6c6eb 100644 --- a/src/Account/CommandHandler/DeleteUserShopHandler.php +++ b/src/Account/CommandHandler/DeleteUserShopHandler.php @@ -26,10 +26,8 @@ use PrestaShop\Module\PsAccounts\Api\Client\AccountsClient; use PrestaShop\Module\PsAccounts\Context\ShopContext; use PrestaShop\Module\PsAccounts\Exception\RefreshTokenException; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; -class DeleteUserShopHandler implements IServiceContainerService +class DeleteUserShopHandler { /** * @var AccountsClient @@ -89,19 +87,4 @@ public function handle(DeleteUserShopCommand $command) ); }); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(AccountsClient::class), - $serviceContainer->get(ShopContext::class), - $serviceContainer->get(ShopSession::class), - $serviceContainer->get(OwnerSession::class) - ); - } } diff --git a/src/Account/CommandHandler/LinkShopHandler.php b/src/Account/CommandHandler/LinkShopHandler.php index 0d93036b5..2b8b429a9 100644 --- a/src/Account/CommandHandler/LinkShopHandler.php +++ b/src/Account/CommandHandler/LinkShopHandler.php @@ -24,11 +24,9 @@ use PrestaShop\Module\PsAccounts\Account\Command\LinkShopCommand; use PrestaShop\Module\PsAccounts\Account\LinkShop; use PrestaShop\Module\PsAccounts\Hook\ActionShopAccountLinkAfter; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; use PrestaShopException; -class LinkShopHandler implements IServiceContainerService +class LinkShopHandler { /** * @var LinkShop @@ -56,16 +54,4 @@ public function handle(LinkShopCommand $command) 'shopId' => $command->payload->shopId, ]); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(LinkShop::class) - ); - } } diff --git a/src/Account/CommandHandler/MigrateAndLinkV4ShopHandler.php b/src/Account/CommandHandler/MigrateAndLinkV4ShopHandler.php deleted file mode 100644 index 806bd59c7..000000000 --- a/src/Account/CommandHandler/MigrateAndLinkV4ShopHandler.php +++ /dev/null @@ -1,75 +0,0 @@ - - * @copyright Since 2007 PrestaShop SA and Contributors - * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 - */ - -namespace PrestaShop\Module\PsAccounts\Account\CommandHandler; - -use PrestaShop\Module\PsAccounts\Account\Command\MigrateAndLinkV4ShopCommand; -use PrestaShop\Module\PsAccounts\Account\Session\Firebase\ShopSession; -use PrestaShop\Module\PsAccounts\Api\Client\AccountsClient; -use PrestaShop\Module\PsAccounts\Context\ShopContext; -use PrestaShop\Module\PsAccounts\Exception\RefreshTokenException; - -class MigrateAndLinkV4ShopHandler -{ - /** - * @var AccountsClient - */ - private $accountClient; - - /** - * @var ShopContext - */ - private $shopContext; - - /** - * @var ShopSession - */ - private $shopSession; - - public function __construct( - AccountsClient $accountClient, - ShopContext $shopContext, - ShopSession $shopSession - ) { - $this->accountClient = $accountClient; - $this->shopContext = $shopContext; - $this->shopSession = $shopSession; - } - - /** - * @param MigrateAndLinkV4ShopCommand $command - * - * @return array - * - * @throws RefreshTokenException - */ - public function handle(MigrateAndLinkV4ShopCommand $command) - { - return $this->shopContext->execInShopContext((int) $command->shopId, function () use ($command) { - $shopToken = $this->shopSession->getValidToken(); - - return $this->accountClient->reonboardShop( - $shopToken->getUuid(), - $shopToken->getJwt(), - $command->payload - ); - }); - } -} diff --git a/src/Account/CommandHandler/UnlinkShopHandler.php b/src/Account/CommandHandler/UnlinkShopHandler.php index d035a3312..a70a9d23e 100644 --- a/src/Account/CommandHandler/UnlinkShopHandler.php +++ b/src/Account/CommandHandler/UnlinkShopHandler.php @@ -26,10 +26,8 @@ use PrestaShop\Module\PsAccounts\Hook\ActionShopAccountUnlinkAfter; use PrestaShop\Module\PsAccounts\Provider\ShopProvider; use PrestaShop\Module\PsAccounts\Service\AnalyticsService; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; -class UnlinkShopHandler implements IServiceContainerService +class UnlinkShopHandler { /** * @var LinkShop @@ -101,18 +99,4 @@ public function handle(UnlinkShopCommand $command) Hook::exec(ActionShopAccountUnlinkAfter::getName(), $hookData); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(LinkShop::class), - $serviceContainer->get(AnalyticsService::class), - $serviceContainer->get(ShopProvider::class) - ); - } } diff --git a/src/Account/CommandHandler/UpdateUserShopHandler.php b/src/Account/CommandHandler/UpdateUserShopHandler.php index 920952d1c..01051ecf3 100644 --- a/src/Account/CommandHandler/UpdateUserShopHandler.php +++ b/src/Account/CommandHandler/UpdateUserShopHandler.php @@ -26,10 +26,8 @@ use PrestaShop\Module\PsAccounts\Api\Client\AccountsClient; use PrestaShop\Module\PsAccounts\Context\ShopContext; use PrestaShop\Module\PsAccounts\Exception\RefreshTokenException; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; -class UpdateUserShopHandler implements IServiceContainerService +class UpdateUserShopHandler { /** * @var AccountsClient @@ -90,19 +88,4 @@ public function handle(UpdateUserShopCommand $command) ); }); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(AccountsClient::class), - $serviceContainer->get(ShopContext::class), - $serviceContainer->get(ShopSession::class), - $serviceContainer->get(OwnerSession::class) - ); - } } diff --git a/src/Account/CommandHandler/UpgradeModuleHandler.php b/src/Account/CommandHandler/UpgradeModuleHandler.php index 374069b02..e41d6539a 100644 --- a/src/Account/CommandHandler/UpgradeModuleHandler.php +++ b/src/Account/CommandHandler/UpgradeModuleHandler.php @@ -31,10 +31,8 @@ use PrestaShop\Module\PsAccounts\Exception\RefreshTokenException; use PrestaShop\Module\PsAccounts\Log\Logger; use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; -class UpgradeModuleHandler implements IServiceContainerService +class UpgradeModuleHandler { /** * @var LinkShop @@ -166,21 +164,4 @@ private function lastChanceToRefreshShopToken() $tokens['refresh_token'] ); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(AccountsClient::class), - $serviceContainer->get(LinkShop::class), - $serviceContainer->get(ShopSession::class), - $serviceContainer->get(ShopContext::class), - $serviceContainer->get(ConfigurationRepository::class), - $serviceContainer->get(CommandBus::class) - ); - } } diff --git a/src/Account/CommandHandler/UpgradeModuleMultiHandler.php b/src/Account/CommandHandler/UpgradeModuleMultiHandler.php index a028bf1b5..aa59f8025 100644 --- a/src/Account/CommandHandler/UpgradeModuleMultiHandler.php +++ b/src/Account/CommandHandler/UpgradeModuleMultiHandler.php @@ -27,11 +27,9 @@ use PrestaShop\Module\PsAccounts\Exception\DtoException; use PrestaShop\Module\PsAccounts\Exception\RefreshTokenException; use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; use PrestaShopDatabaseException; -class UpgradeModuleMultiHandler implements IServiceContainerService +class UpgradeModuleMultiHandler { /** * @var ConfigurationRepository @@ -95,17 +93,4 @@ private function getShops($multishop) return $shops; } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(CommandBus::class), - $serviceContainer->get(ConfigurationRepository::class) - ); - } } diff --git a/src/Account/LinkShop.php b/src/Account/LinkShop.php index ee1c92aea..962e351b8 100644 --- a/src/Account/LinkShop.php +++ b/src/Account/LinkShop.php @@ -21,10 +21,8 @@ namespace PrestaShop\Module\PsAccounts\Account; use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; -class LinkShop implements IServiceContainerService +class LinkShop { /** * @var ConfigurationRepository @@ -191,16 +189,4 @@ public function setUnlinkedOnError($errorMsg) { $this->configuration->updateUnlinkedOnError($errorMsg); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(ConfigurationRepository::class) - ); - } } diff --git a/src/Account/Session/Firebase/OwnerSession.php b/src/Account/Session/Firebase/OwnerSession.php index 790ac9d94..603257ec3 100644 --- a/src/Account/Session/Firebase/OwnerSession.php +++ b/src/Account/Session/Firebase/OwnerSession.php @@ -23,10 +23,8 @@ use PrestaShop\Module\PsAccounts\Account\Session\SessionInterface; use PrestaShop\Module\PsAccounts\Account\Token\Token; use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; -class OwnerSession extends FirebaseSession implements SessionInterface, IServiceContainerService +class OwnerSession extends FirebaseSession implements SessionInterface { /** * @var ConfigurationRepository @@ -75,17 +73,4 @@ public function setToken($token, $refreshToken = null) { $this->configurationRepository->updateUserFirebaseIdAndRefreshToken($token, $refreshToken); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(ConfigurationRepository::class), - $serviceContainer->get(\PrestaShop\Module\PsAccounts\Account\Session\ShopSession::class) - ); - } } diff --git a/src/Account/Session/Firebase/ShopSession.php b/src/Account/Session/Firebase/ShopSession.php index d52e879a3..ad0d50515 100644 --- a/src/Account/Session/Firebase/ShopSession.php +++ b/src/Account/Session/Firebase/ShopSession.php @@ -23,10 +23,8 @@ use PrestaShop\Module\PsAccounts\Account\Session\SessionInterface; use PrestaShop\Module\PsAccounts\Account\Token\Token; use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; -class ShopSession extends FirebaseSession implements SessionInterface, IServiceContainerService +class ShopSession extends FirebaseSession implements SessionInterface { /** * @var ConfigurationRepository @@ -75,17 +73,4 @@ public function setToken($token, $refreshToken = null) { $this->configurationRepository->updateFirebaseIdAndRefreshTokens($token, $refreshToken); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(ConfigurationRepository::class), - $serviceContainer->get(\PrestaShop\Module\PsAccounts\Account\Session\ShopSession::class) - ); - } } diff --git a/src/Account/Session/ShopSession.php b/src/Account/Session/ShopSession.php index eff61dad0..fb94ad396 100644 --- a/src/Account/Session/ShopSession.php +++ b/src/Account/Session/ShopSession.php @@ -30,14 +30,12 @@ use PrestaShop\Module\PsAccounts\Log\Logger; use PrestaShop\Module\PsAccounts\Provider\OAuth2\ShopProvider; use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; use PrestaShop\Module\PsAccounts\Vendor\League\OAuth2\Client\Grant\ClientCredentials; use PrestaShop\Module\PsAccounts\Vendor\League\OAuth2\Client\Provider\Exception\IdentityProviderException; use PrestaShop\Module\PsAccounts\Vendor\League\OAuth2\Client\Token\AccessToken; use PrestaShop\Module\PsAccounts\Vendor\League\OAuth2\Client\Token\AccessTokenInterface; -class ShopSession extends Session implements SessionInterface, IServiceContainerService +class ShopSession extends Session implements SessionInterface { /** * @var CommandBus @@ -205,19 +203,4 @@ private function getShopUuid() { return $this->linkShop->getShopUuid(); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(ConfigurationRepository::class), - $serviceContainer->get(ShopProvider::class), - $serviceContainer->get(LinkShop::class), - $serviceContainer->get(CommandBus::class) - ); - } } diff --git a/src/Adapter/Configuration.php b/src/Adapter/Configuration.php index 5ce973a01..6bd455add 100644 --- a/src/Adapter/Configuration.php +++ b/src/Adapter/Configuration.php @@ -20,10 +20,7 @@ namespace PrestaShop\Module\PsAccounts\Adapter; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; - -class Configuration implements IServiceContainerService +class Configuration { /** * @var int @@ -232,16 +229,4 @@ public function isMultishopActive() return \Db::getInstance()->getValue('SELECT value FROM `' . _DB_PREFIX_ . 'configuration` WHERE `name` = "PS_MULTISHOP_FEATURE_ACTIVE"') && (\Db::getInstance()->getValue('SELECT COUNT(*) FROM ' . _DB_PREFIX_ . 'shop') > 1); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get('ps_accounts.context') - ); - } } diff --git a/src/Adapter/Link.php b/src/Adapter/Link.php index eacf2641e..89f516f10 100644 --- a/src/Adapter/Link.php +++ b/src/Adapter/Link.php @@ -21,13 +21,11 @@ namespace PrestaShop\Module\PsAccounts\Adapter; use PrestaShop\Module\PsAccounts\Context\ShopContext; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; /** * Link adapter */ -class Link implements IServiceContainerService +class Link { /** * @var ShopContext @@ -121,16 +119,4 @@ public function getAdminLinkWithCustomDomain($sslDomain, $domain, $controller, $ return $boBaseUrl; } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(ShopContext::class) - ); - } } diff --git a/src/Api/Client/AccountsClient.php b/src/Api/Client/AccountsClient.php index 8a6e5a830..3dd35116b 100644 --- a/src/Api/Client/AccountsClient.php +++ b/src/Api/Client/AccountsClient.php @@ -24,11 +24,9 @@ use PrestaShop\Module\PsAccounts\Account\Dto\UpgradeModule; use PrestaShop\Module\PsAccounts\Http\Client\Guzzle\GuzzleClient; use PrestaShop\Module\PsAccounts\Http\Client\Guzzle\GuzzleClientFactory; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; use PrestaShop\Module\PsAccounts\Vendor\Ramsey\Uuid\Uuid; -class AccountsClient implements IServiceContainerService +class AccountsClient { /** * @var string @@ -243,18 +241,4 @@ public function healthCheck() return $this->getClient()->get(); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->getParameter('ps_accounts.accounts_api_url'), - null, - 10 - ); - } } diff --git a/src/Api/Client/ServicesBillingClient.php b/src/Api/Client/ServicesBillingClient.php index ebd96b168..28da29e3c 100644 --- a/src/Api/Client/ServicesBillingClient.php +++ b/src/Api/Client/ServicesBillingClient.php @@ -24,13 +24,11 @@ use PrestaShop\Module\PsAccounts\Http\Client\Guzzle\GuzzleClientFactory; use PrestaShop\Module\PsAccounts\Provider\ShopProvider; use PrestaShop\Module\PsAccounts\Service\PsAccountsService; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; /** * Handle call api Services */ -class ServicesBillingClient implements IServiceContainerService +class ServicesBillingClient { /** * @var GuzzleClient @@ -132,18 +130,4 @@ public function createBillingSubscriptions($shopUuidV4, $module, $bodyHttp) 'body' => $bodyHttp, ]); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->getParameter('ps_accounts.billing_api_url'), - $serviceContainer->get(PsAccountsService::class), - $serviceContainer->get(ShopProvider::class) - ); - } } diff --git a/src/Context/ShopContext.php b/src/Context/ShopContext.php index 943a42672..6b1e8c382 100644 --- a/src/Context/ShopContext.php +++ b/src/Context/ShopContext.php @@ -22,13 +22,11 @@ use Context; use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; /** * Get the shop context */ -class ShopContext implements IServiceContainerService +class ShopContext { /** * @var ConfigurationRepository @@ -189,17 +187,4 @@ public function execInShopContext($shopId, $closure) } throw $e; } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(ConfigurationRepository::class), - $serviceContainer->get('ps_accounts.context') - ); - } } diff --git a/src/Cqrs/CommandBus.php b/src/Cqrs/CommandBus.php index f10acbde0..ef392d3e8 100755 --- a/src/Cqrs/CommandBus.php +++ b/src/Cqrs/CommandBus.php @@ -20,10 +20,7 @@ namespace PrestaShop\Module\PsAccounts\Cqrs; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; - -class CommandBus extends Bus implements IServiceContainerService +class CommandBus extends Bus { /** * @param string $className @@ -37,16 +34,4 @@ public function resolveHandlerClass($className) '${2}Handler\\\\${4}Handler', $className, 1); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get('ps_accounts.module') - ); - } } diff --git a/src/Factory/CircuitBreakerFactory.php b/src/Factory/CircuitBreakerFactory.php index ffb2634ba..42924ffba 100644 --- a/src/Factory/CircuitBreakerFactory.php +++ b/src/Factory/CircuitBreakerFactory.php @@ -24,10 +24,8 @@ use PrestaShop\Module\PsAccounts\Api\Client\AccountsClient; use PrestaShop\Module\PsAccounts\Http\Client\CircuitBreaker\CircuitBreaker; use PrestaShop\Module\PsAccounts\Http\Client\CircuitBreaker\PersistentCircuitBreaker; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; -class CircuitBreakerFactory implements IServiceContainerService +class CircuitBreakerFactory { /** * @var array @@ -134,16 +132,4 @@ protected function className($className) { return strtoupper(preg_replace(['/^.*\\\\/', '/([^A-Z])([A-Z])/'], ['', '$1_$2'], $className)); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(Configuration::class) - ); - } } diff --git a/src/Installer/Installer.php b/src/Installer/Installer.php index 43492352c..ff754926b 100644 --- a/src/Installer/Installer.php +++ b/src/Installer/Installer.php @@ -24,8 +24,6 @@ use PrestaShop\Module\PsAccounts\Adapter\Link; use PrestaShop\Module\PsAccounts\Context\ShopContext; use PrestaShop\Module\PsAccounts\Service\SentryService; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; use PrestaShop\PrestaShop\Adapter\SymfonyContainer; use PrestaShop\PrestaShop\Core\Addon\Module\ModuleManagerBuilder; use Tools; @@ -35,7 +33,7 @@ * * Install ps_accounts module */ -class Installer implements IServiceContainerService +class Installer { /** * @var ShopContext @@ -174,17 +172,4 @@ public function isEnabled($module) return $moduleManager->isEnabled($module); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(ShopContext::class), - $serviceContainer->get(Link::class) - ); - } } diff --git a/src/Middleware/Oauth2Middleware.php b/src/Middleware/Oauth2Middleware.php index 08be1cf97..d6294e052 100644 --- a/src/Middleware/Oauth2Middleware.php +++ b/src/Middleware/Oauth2Middleware.php @@ -25,12 +25,10 @@ use PrestaShop\Module\PsAccounts\Provider\OAuth2\PrestaShopSession; use PrestaShop\Module\PsAccounts\Provider\OAuth2\ShopProvider; use PrestaShop\Module\PsAccounts\Service\PsAccountsService; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; use PrestaShop\Module\PsAccounts\Vendor\League\OAuth2\Client\Provider\Exception\IdentityProviderException; use Ps_accounts; -class Oauth2Middleware implements IServiceContainerService +class Oauth2Middleware { use PrestaShopLogoutTrait; @@ -123,16 +121,4 @@ protected function onLogoutCallback() \Tools::redirectLink($this->getProvider()->getRedirectUri()); } } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get('ps_accounts.module') - ); - } } diff --git a/src/Presenter/PsAccountsPresenter.php b/src/Presenter/PsAccountsPresenter.php index 27bbf4c0f..d2b54ccbe 100644 --- a/src/Presenter/PsAccountsPresenter.php +++ b/src/Presenter/PsAccountsPresenter.php @@ -26,14 +26,12 @@ use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; use PrestaShop\Module\PsAccounts\Service\PsAccountsService; use PrestaShop\Module\PsAccounts\Service\SentryService; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; use PrestaShopException; /** * Construct the psaccounts module. */ -class PsAccountsPresenter implements PresenterInterface, IServiceContainerService +class PsAccountsPresenter implements PresenterInterface { /** * @var ShopProvider @@ -170,16 +168,4 @@ public function present($psxName = 'ps_accounts') return []; } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get('ps_accounts.module') - ); - } } diff --git a/src/Provider/OAuth2/Oauth2Client.php b/src/Provider/OAuth2/Oauth2Client.php index 294edae2c..8b0f07623 100644 --- a/src/Provider/OAuth2/Oauth2Client.php +++ b/src/Provider/OAuth2/Oauth2Client.php @@ -21,10 +21,8 @@ namespace PrestaShop\Module\PsAccounts\Provider\OAuth2; use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; -class Oauth2Client implements IServiceContainerService +class Oauth2Client { /** * @var ConfigurationRepository @@ -81,16 +79,4 @@ public function getClientSecret() { return $this->cfRepos->getOauth2ClientSecret(); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(ConfigurationRepository::class) - ); - } } diff --git a/src/Provider/OAuth2/PrestaShopSession.php b/src/Provider/OAuth2/PrestaShopSession.php index 083692f88..3b5c88476 100644 --- a/src/Provider/OAuth2/PrestaShopSession.php +++ b/src/Provider/OAuth2/PrestaShopSession.php @@ -20,14 +20,12 @@ namespace PrestaShop\Module\PsAccounts\Provider\OAuth2; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; use PrestaShop\Module\PsAccounts\Vendor\League\OAuth2\Client\Provider\Exception\IdentityProviderException; use PrestaShop\Module\PsAccounts\Vendor\League\OAuth2\Client\Token\AccessToken; use PrestaShop\OAuth2\Client\Provider\PrestaShopUser; use Symfony\Component\HttpFoundation\Session\SessionInterface; -class PrestaShopSession implements IServiceContainerService +class PrestaShopSession { const TOKEN_NAME = 'accessToken'; @@ -135,14 +133,4 @@ private function getTokenProvider() return $this->session->get(self::TOKEN_NAME); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return \PrestaShop\Module\PsAccounts\Factory\PrestaShopSessionFactory::create(); - } } diff --git a/src/Provider/OAuth2/ShopProvider.php b/src/Provider/OAuth2/ShopProvider.php index 01529f77b..a05e3c194 100644 --- a/src/Provider/OAuth2/ShopProvider.php +++ b/src/Provider/OAuth2/ShopProvider.php @@ -21,12 +21,10 @@ namespace PrestaShop\Module\PsAccounts\Provider\OAuth2; use PrestaShop\Module\PsAccounts\Adapter\Link; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; use PrestaShop\Module\PsAccounts\Vendor\League\OAuth2\Client\Provider\AbstractProvider; use PrestaShop\OAuth2\Client\Provider\PrestaShop; -class ShopProvider extends PrestaShop implements IServiceContainerService +class ShopProvider extends PrestaShop { const QUERY_LOGOUT_CALLBACK_PARAM = 'oauth2Callback'; @@ -170,14 +168,4 @@ private function syncOauth2ClientProps() $this->clientId = $this->getOauth2Client()->getClientId(); $this->clientSecret = $this->getOauth2Client()->getClientSecret(); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return PrestaShop - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return \PrestaShop\Module\PsAccounts\Provider\OAuth2\ShopProvider::create(); - } } diff --git a/src/Provider/RsaKeysProvider.php b/src/Provider/RsaKeysProvider.php index e8aa32367..d2ef123f8 100644 --- a/src/Provider/RsaKeysProvider.php +++ b/src/Provider/RsaKeysProvider.php @@ -22,14 +22,12 @@ use PrestaShop\Module\PsAccounts\Exception\SshKeysNotFoundException; use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; use PrestaShop\Module\PsAccounts\Vendor\phpseclib\Crypt\RSA; /** * Manage RSA */ -class RsaKeysProvider implements IServiceContainerService +class RsaKeysProvider { /** * @var RSA @@ -193,16 +191,4 @@ public function cleanupKeys() $this->configuration->updateAccountsRsaPrivateKey(''); $this->configuration->updateAccountsRsaPublicKey(''); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(ConfigurationRepository::class) - ); - } } diff --git a/src/Provider/ShopProvider.php b/src/Provider/ShopProvider.php index 282f64852..5baf221f6 100644 --- a/src/Provider/ShopProvider.php +++ b/src/Provider/ShopProvider.php @@ -25,10 +25,8 @@ use PrestaShop\Module\PsAccounts\Account\Session\Firebase\OwnerSession; use PrestaShop\Module\PsAccounts\Adapter\Link; use PrestaShop\Module\PsAccounts\Context\ShopContext; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; -class ShopProvider implements IServiceContainerService +class ShopProvider { /** * @var ShopContext @@ -266,17 +264,4 @@ private function getShopUrl($shopData) ($shopData['domain_ssl'] ?: $shopData['domain']) . $shopData['uri']; } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(ShopContext::class), - $serviceContainer->get(Link::class) - ); - } } diff --git a/src/Repository/ConfigurationRepository.php b/src/Repository/ConfigurationRepository.php index 051543f73..d808e5169 100644 --- a/src/Repository/ConfigurationRepository.php +++ b/src/Repository/ConfigurationRepository.php @@ -23,10 +23,8 @@ use PrestaShop\Module\PsAccounts\Adapter\Configuration; use PrestaShop\Module\PsAccounts\Adapter\ConfigurationKeys; use PrestaShop\Module\PsAccounts\Log\Logger; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; -class ConfigurationRepository implements IServiceContainerService +class ConfigurationRepository { /** * @var Configuration @@ -464,16 +462,4 @@ protected function migrateToSingleShop() ' AND id_shop = ' . (int) $shop->id . ';' ); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(Configuration::class) - ); - } } diff --git a/src/Repository/ShopTokenRepository.php b/src/Repository/ShopTokenRepository.php index 5bbf36897..653f6a195 100644 --- a/src/Repository/ShopTokenRepository.php +++ b/src/Repository/ShopTokenRepository.php @@ -21,30 +21,16 @@ namespace PrestaShop\Module\PsAccounts\Repository; use PrestaShop\Module\PsAccounts\Account\Session\Firebase\ShopSession; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; /** * Class ShopTokenRepository * * @deprecated */ -class ShopTokenRepository extends TokenRepository implements IServiceContainerService +class ShopTokenRepository extends TokenRepository { /** * @var ShopSession */ protected $session; - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(ShopSession::class) - ); - } } diff --git a/src/Repository/UserTokenRepository.php b/src/Repository/UserTokenRepository.php index 570771033..5905d7c55 100644 --- a/src/Repository/UserTokenRepository.php +++ b/src/Repository/UserTokenRepository.php @@ -21,15 +21,13 @@ namespace PrestaShop\Module\PsAccounts\Repository; use PrestaShop\Module\PsAccounts\Account\Session\Firebase\OwnerSession; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; /** * Class UserTokenRepository * * @deprecated */ -class UserTokenRepository extends TokenRepository implements IServiceContainerService +class UserTokenRepository extends TokenRepository { /** * @var OwnerSession @@ -53,16 +51,4 @@ public function getTokenEmailVerified() { return $this->session->isEmailVerified(); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(OwnerSession::class) - ); - } } diff --git a/src/Service/AnalyticsService.php b/src/Service/AnalyticsService.php index 69b1c58cb..9006852a9 100644 --- a/src/Service/AnalyticsService.php +++ b/src/Service/AnalyticsService.php @@ -20,13 +20,11 @@ namespace PrestaShop\Module\PsAccounts\Service; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; use PrestaShop\Module\PsAccounts\Vendor\Monolog\Logger; use PrestaShop\Module\PsAccounts\Vendor\Ramsey\Uuid\Uuid; use Segment; -class AnalyticsService implements IServiceContainerService +class AnalyticsService { const COOKIE_ANONYMOUS_ID = 'ajs_anonymous_id'; @@ -317,17 +315,4 @@ private function initAnonymousId() } } } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->getParameter('ps_accounts.segment_write_key'), - $serviceContainer->get('ps_accounts.logger') - ); - } } diff --git a/src/Service/PsAccountsService.php b/src/Service/PsAccountsService.php index 461c36e98..99ce5cc85 100644 --- a/src/Service/PsAccountsService.php +++ b/src/Service/PsAccountsService.php @@ -32,13 +32,11 @@ use PrestaShop\Module\PsAccounts\Provider\ShopProvider; use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; use PrestaShop\Module\PsAccounts\Repository\EmployeeAccountRepository; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; /** * Class PsAccountsService */ -class PsAccountsService implements IServiceContainerService +class PsAccountsService { /** * @var Link @@ -340,16 +338,4 @@ public function getEmployeeAccount() return null; } } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get('ps_accounts.module') - ); - } } diff --git a/src/Service/PsBillingService.php b/src/Service/PsBillingService.php index 798739de9..1d238088d 100644 --- a/src/Service/PsBillingService.php +++ b/src/Service/PsBillingService.php @@ -25,15 +25,13 @@ use PrestaShop\Module\PsAccounts\Exception\BillingException; use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; use PrestaShop\Module\PsAccounts\Repository\ShopTokenRepository; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; /** * @deprecated * * Construct the psbilling service */ -class PsBillingService implements IServiceContainerService +class PsBillingService { /** * @var ConfigurationRepository @@ -149,18 +147,4 @@ public function subscribeToFreePlan($module, $planName, $shopId = false, $custom throw new \Exception('Shop account unknown.', 10); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->get(ServicesBillingClient::class), - $serviceContainer->get(ShopTokenRepository::class), - $serviceContainer->get(ConfigurationRepository::class) - ); - } } diff --git a/src/Service/SentryService.php b/src/Service/SentryService.php index 539733b22..ee979d640 100644 --- a/src/Service/SentryService.php +++ b/src/Service/SentryService.php @@ -24,15 +24,13 @@ use Module; use PrestaShop\Module\PsAccounts\Account\LinkShop; use PrestaShop\Module\PsAccounts\Service\Sentry\ModuleFilteredRavenClient; -use PrestaShop\Module\PsAccounts\ServiceContainer\IServiceContainerService; -use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; use Ps_accounts; use Raven_Client; /** * FIXME: outdated sentry client due to PHP 5.6 support */ -class SentryService implements IServiceContainerService +class SentryService { /** * @var Raven_Client @@ -181,19 +179,4 @@ private function isContextInFrontOffice(Context $context = null) return in_array($controller->controller_type, ['front', 'modulefront']); } - - /** - * @param ServiceContainer $serviceContainer - * - * @return self - */ - public static function getInstance(ServiceContainer $serviceContainer) - { - return new self( - $serviceContainer->getParameter('ps_accounts.sentry_credentials'), - $serviceContainer->getParameter('ps_accounts.environment'), - $serviceContainer->get(LinkShop::class), - $serviceContainer->get('ps_accounts.context') - ); - } } diff --git a/src/ServiceContainer/IServiceContainerService.php b/src/ServiceContainer/Contract/IServiceContainerService.php similarity index 88% rename from src/ServiceContainer/IServiceContainerService.php rename to src/ServiceContainer/Contract/IServiceContainerService.php index ec2b1fc35..8884b4dde 100644 --- a/src/ServiceContainer/IServiceContainerService.php +++ b/src/ServiceContainer/Contract/IServiceContainerService.php @@ -18,7 +18,9 @@ * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 */ -namespace PrestaShop\Module\PsAccounts\ServiceContainer; +namespace PrestaShop\Module\PsAccounts\ServiceContainer\Contract; + +use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; interface IServiceContainerService { diff --git a/src/ServiceContainer/Contract/IServiceProvider.php b/src/ServiceContainer/Contract/IServiceProvider.php new file mode 100644 index 000000000..cbe4e8af2 --- /dev/null +++ b/src/ServiceContainer/Contract/IServiceProvider.php @@ -0,0 +1,33 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsAccounts\ServiceContainer\Contract; + +use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; + +interface IServiceProvider +{ + /** + * @param ServiceContainer $container + * + * @return void + */ + public function provide(ServiceContainer $container); +} diff --git a/src/ServiceContainer/ParameterNotFoundException.php b/src/ServiceContainer/Exception/ParameterNotFoundException.php similarity index 92% rename from src/ServiceContainer/ParameterNotFoundException.php rename to src/ServiceContainer/Exception/ParameterNotFoundException.php index 8503d4fde..72d6ff7b5 100644 --- a/src/ServiceContainer/ParameterNotFoundException.php +++ b/src/ServiceContainer/Exception/ParameterNotFoundException.php @@ -18,7 +18,7 @@ * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 */ -namespace PrestaShop\Module\PsAccounts\ServiceContainer; +namespace PrestaShop\Module\PsAccounts\ServiceContainer\Exception; class ParameterNotFoundException extends \Exception { diff --git a/src/ServiceContainer/Exception/ProviderNotFoundException.php b/src/ServiceContainer/Exception/ProviderNotFoundException.php new file mode 100644 index 000000000..b3e653e9e --- /dev/null +++ b/src/ServiceContainer/Exception/ProviderNotFoundException.php @@ -0,0 +1,25 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsAccounts\ServiceContainer\Exception; + +class ProviderNotFoundException extends \Exception +{ +} diff --git a/src/ServiceContainer/ServiceNotFoundException.php b/src/ServiceContainer/Exception/ServiceNotFoundException.php similarity index 92% rename from src/ServiceContainer/ServiceNotFoundException.php rename to src/ServiceContainer/Exception/ServiceNotFoundException.php index 00bf8e61a..12c3c145d 100644 --- a/src/ServiceContainer/ServiceNotFoundException.php +++ b/src/ServiceContainer/Exception/ServiceNotFoundException.php @@ -18,7 +18,7 @@ * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 */ -namespace PrestaShop\Module\PsAccounts\ServiceContainer; +namespace PrestaShop\Module\PsAccounts\ServiceContainer\Exception; class ServiceNotFoundException extends \Exception { diff --git a/src/ServiceContainer/Provider/ApiClientProvider.php b/src/ServiceContainer/Provider/ApiClientProvider.php new file mode 100644 index 000000000..68baf352f --- /dev/null +++ b/src/ServiceContainer/Provider/ApiClientProvider.php @@ -0,0 +1,54 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsAccounts\ServiceContainer\Provider; + +use PrestaShop\Module\PsAccounts\Api\Client\AccountsClient; +use PrestaShop\Module\PsAccounts\Api\Client\ServicesBillingClient; +use PrestaShop\Module\PsAccounts\Provider\ShopProvider; +use PrestaShop\Module\PsAccounts\Service\PsAccountsService; +use PrestaShop\Module\PsAccounts\ServiceContainer\Contract\IServiceProvider; +use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; + +class ApiClientProvider implements IServiceProvider +{ + /** + * @param ServiceContainer $container + * + * @return void + */ + public function provide(ServiceContainer $container) + { + $container->registerProvider(AccountsClient::class, function () use ($container) { + return new AccountsClient( + $container->getParameter('ps_accounts.accounts_api_url'), + null, + 10 + ); + }); + $container->registerProvider(ServicesBillingClient::class, function () use ($container) { + return new ServicesBillingClient( + $container->getParameter('ps_accounts.billing_api_url'), + $container->get(PsAccountsService::class), + $container->get(ShopProvider::class) + ); + }); + } +} diff --git a/src/ServiceContainer/Provider/CommandProvider.php b/src/ServiceContainer/Provider/CommandProvider.php new file mode 100644 index 000000000..424a9a0bc --- /dev/null +++ b/src/ServiceContainer/Provider/CommandProvider.php @@ -0,0 +1,90 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsAccounts\ServiceContainer\Provider; + +use PrestaShop\Module\PsAccounts\Account\CommandHandler\DeleteUserShopHandler; +use PrestaShop\Module\PsAccounts\Account\CommandHandler\LinkShopHandler; +use PrestaShop\Module\PsAccounts\Account\CommandHandler\UnlinkShopHandler; +use PrestaShop\Module\PsAccounts\Account\CommandHandler\UpdateUserShopHandler; +use PrestaShop\Module\PsAccounts\Account\CommandHandler\UpgradeModuleHandler; +use PrestaShop\Module\PsAccounts\Account\CommandHandler\UpgradeModuleMultiHandler; +use PrestaShop\Module\PsAccounts\Account\LinkShop; +use PrestaShop\Module\PsAccounts\Account\Session\Firebase\OwnerSession; +use PrestaShop\Module\PsAccounts\Account\Session\Firebase\ShopSession; +use PrestaShop\Module\PsAccounts\Api\Client\AccountsClient; +use PrestaShop\Module\PsAccounts\Context\ShopContext; +use PrestaShop\Module\PsAccounts\Cqrs\CommandBus; +use PrestaShop\Module\PsAccounts\Provider\ShopProvider; +use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; +use PrestaShop\Module\PsAccounts\Service\AnalyticsService; +use PrestaShop\Module\PsAccounts\ServiceContainer\Contract\IServiceProvider; +use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; + +class CommandProvider implements IServiceProvider +{ + public function provide(ServiceContainer $container) + { + $container->registerProvider(DeleteUserShopHandler::class, function () use ($container) { + return new DeleteUserShopHandler( + $container->get(AccountsClient::class), + $container->get(ShopContext::class), + $container->get(ShopSession::class), + $container->get(OwnerSession::class) + ); + }); + $container->registerProvider(LinkShopHandler::class, function () use ($container) { + return new LinkShopHandler( + $container->get(LinkShop::class) + ); + }); + $container->registerProvider(UnlinkShopHandler::class, function () use ($container) { + return new UnlinkShopHandler( + $container->get(LinkShop::class), + $container->get(AnalyticsService::class), + $container->get(ShopProvider::class) + ); + }); + $container->registerProvider(UpdateUserShopHandler::class, function () use ($container) { + return new UpdateUserShopHandler( + $container->get(AccountsClient::class), + $container->get(ShopContext::class), + $container->get(ShopSession::class), + $container->get(OwnerSession::class) + ); + }); + $container->registerProvider(UpgradeModuleHandler::class, function () use ($container) { + return new UpgradeModuleHandler( + $container->get(AccountsClient::class), + $container->get(LinkShop::class), + $container->get(ShopSession::class), + $container->get(ShopContext::class), + $container->get(ConfigurationRepository::class), + $container->get(CommandBus::class) + ); + }); + $container->registerProvider(UpgradeModuleMultiHandler::class, function () use ($container) { + return new UpgradeModuleMultiHandler( + $container->get(CommandBus::class), + $container->get(ConfigurationRepository::class) + ); + }); + } +} diff --git a/src/ServiceContainer/Provider/DefaultProvider.php b/src/ServiceContainer/Provider/DefaultProvider.php new file mode 100644 index 000000000..59d36d981 --- /dev/null +++ b/src/ServiceContainer/Provider/DefaultProvider.php @@ -0,0 +1,150 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsAccounts\ServiceContainer\Provider; + +use PrestaShop\Module\PsAccounts\Account\LinkShop; +use PrestaShop\Module\PsAccounts\Adapter; +use PrestaShop\Module\PsAccounts\Adapter\Configuration; +use PrestaShop\Module\PsAccounts\Adapter\Link; +use PrestaShop\Module\PsAccounts\Api\Client\ServicesBillingClient; +use PrestaShop\Module\PsAccounts\Context\ShopContext; +use PrestaShop\Module\PsAccounts\Cqrs\CommandBus; +use PrestaShop\Module\PsAccounts\Factory\CircuitBreakerFactory; +use PrestaShop\Module\PsAccounts\Installer\Installer; +use PrestaShop\Module\PsAccounts\Presenter\PsAccountsPresenter; +use PrestaShop\Module\PsAccounts\Provider; +use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; +use PrestaShop\Module\PsAccounts\Repository\ShopTokenRepository; +use PrestaShop\Module\PsAccounts\Service\AnalyticsService; +use PrestaShop\Module\PsAccounts\Service\PsAccountsService; +use PrestaShop\Module\PsAccounts\Service\PsBillingService; +use PrestaShop\Module\PsAccounts\Service\SentryService; +use PrestaShop\Module\PsAccounts\ServiceContainer\Contract\IServiceProvider; +use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; + +class DefaultProvider implements IServiceProvider +{ + /** + * @param ServiceContainer $container + * + * @return void + */ + public function provide(ServiceContainer $container) + { + $container->registerProvider('ps_accounts.context', function () { + return \Context::getContext(); + }); + $container->registerProvider('ps_accounts.logger', function () { + return \PrestaShop\Module\PsAccounts\Log\Logger::create(); + }); + $container->registerProvider('ps_accounts.module', function () { + return \Module::getInstanceByName('ps_accounts'); + }); + // Entities ? + $container->registerProvider(LinkShop::class, function () use ($container) { + return new LinkShop( + $container->get(ConfigurationRepository::class) + ); + }); + // Adapter + $container->registerProvider(Adapter\Configuration::class, function () use ($container) { + return new Adapter\Configuration( + $container->get('ps_accounts.context') + ); + }); + $container->registerProvider(Adapter\Link::class, function () use ($container) { + return new Adapter\Link( + $container->get(ShopContext::class) + ); + }); + // Services + $container->registerProvider(AnalyticsService::class, function () use ($container) { + return new AnalyticsService( + $container->getParameter('ps_accounts.segment_write_key'), + $container->get('ps_accounts.logger') + ); + }); + $container->registerProvider(PsAccountsService::class, function () use ($container) { + return new PsAccountsService( + $container->get('ps_accounts.module') + ); + }); + $container->registerProvider(PsBillingService::class, function () use ($container) { + return new PsBillingService( + $container->get(ServicesBillingClient::class), + $container->get(ShopTokenRepository::class), + $container->get(ConfigurationRepository::class) + ); + }); + $container->registerProvider(SentryService::class, function () use ($container) { + return new SentryService( + $container->getParameter('ps_accounts.sentry_credentials'), + $container->getParameter('ps_accounts.environment'), + $container->get(LinkShop::class), + $container->get('ps_accounts.context') + ); + }); + // "Providers" + $container->registerProvider(Provider\RsaKeysProvider::class, function () use ($container) { + return new Provider\RsaKeysProvider( + $container->get(ConfigurationRepository::class) + ); + }); + $container->registerProvider(Provider\ShopProvider::class, function () use ($container) { + return new Provider\ShopProvider( + $container->get(ShopContext::class), + $container->get(Link::class) + ); + }); + // Context + $container->registerProvider(ShopContext::class, function () use ($container) { + return new ShopContext( + $container->get(ConfigurationRepository::class), + $container->get('ps_accounts.context') + ); + }); + // CQRS + $container->registerProvider(CommandBus::class, function () use ($container) { + return new CommandBus( + $container->get('ps_accounts.module') + ); + }); + // Factories + $container->registerProvider(CircuitBreakerFactory::class, function () use ($container) { + return new CircuitBreakerFactory( + $container->get(Configuration::class) + ); + }); + // Installer + $container->registerProvider(Installer::class, function () use ($container) { + return new Installer( + $container->get(ShopContext::class), + $container->get(Link::class) + ); + }); + // Presenter + $container->registerProvider(PsAccountsPresenter::class, function () use ($container) { + return new PsAccountsPresenter( + $container->get('ps_accounts.module') + ); + }); + } +} diff --git a/src/ServiceContainer/Provider/OAuth2Provider.php b/src/ServiceContainer/Provider/OAuth2Provider.php new file mode 100644 index 000000000..7dd9c3d22 --- /dev/null +++ b/src/ServiceContainer/Provider/OAuth2Provider.php @@ -0,0 +1,58 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsAccounts\ServiceContainer\Provider; + +use PrestaShop\Module\PsAccounts\Factory\PrestaShopSessionFactory; +use PrestaShop\Module\PsAccounts\Middleware\Oauth2Middleware; +use PrestaShop\Module\PsAccounts\Provider; +use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; +use PrestaShop\Module\PsAccounts\ServiceContainer\Contract\IServiceProvider; +use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; + +class OAuth2Provider implements IServiceProvider +{ + /** + * @param ServiceContainer $container + * + * @return void + */ + public function provide(ServiceContainer $container) + { + // OAuth2 + $container->registerProvider(Provider\OAuth2\Oauth2Client::class, function () use ($container) { + return new Provider\OAuth2\Oauth2Client( + $container->get(ConfigurationRepository::class) + ); + }); + $container->registerProvider(Provider\OAuth2\PrestaShopSession::class, function () { + return PrestaShopSessionFactory::create(); + }); + $container->registerProvider(Provider\OAuth2\ShopProvider::class, function () { + return Provider\OAuth2\ShopProvider::create(); + }); + // Middleware + $container->registerProvider(Oauth2Middleware::class, function () use ($container) { + return new Oauth2Middleware( + $container->get('ps_accounts.module') + ); + }); + } +} diff --git a/src/ServiceContainer/Provider/RepositoryProvider.php b/src/ServiceContainer/Provider/RepositoryProvider.php new file mode 100644 index 000000000..4eb9deb65 --- /dev/null +++ b/src/ServiceContainer/Provider/RepositoryProvider.php @@ -0,0 +1,58 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsAccounts\ServiceContainer\Provider; + +use PrestaShop\Module\PsAccounts\Account\Session\Firebase; +use PrestaShop\Module\PsAccounts\Account\Session\Firebase\OwnerSession; +use PrestaShop\Module\PsAccounts\Adapter\Configuration; +use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; +use PrestaShop\Module\PsAccounts\Repository\ShopTokenRepository; +use PrestaShop\Module\PsAccounts\Repository\UserTokenRepository; +use PrestaShop\Module\PsAccounts\ServiceContainer\Contract\IServiceProvider; +use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; + +class RepositoryProvider implements IServiceProvider +{ + /** + * @param ServiceContainer $container + * + * @return void + */ + public function provide(ServiceContainer $container) + { + // Repositories + $container->registerProvider(ConfigurationRepository::class, function () use ($container) { + return new ConfigurationRepository( + $container->get(Configuration::class) + ); + }); + $container->registerProvider(ShopTokenRepository::class, function () use ($container) { + return new ShopTokenRepository( + $container->get(Firebase\ShopSession::class) + ); + }); + $container->registerProvider(UserTokenRepository::class, function () use ($container) { + return new UserTokenRepository( + $container->get(OwnerSession::class) + ); + }); + } +} diff --git a/src/ServiceContainer/Provider/SessionProvider.php b/src/ServiceContainer/Provider/SessionProvider.php new file mode 100644 index 000000000..6870fd97d --- /dev/null +++ b/src/ServiceContainer/Provider/SessionProvider.php @@ -0,0 +1,63 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsAccounts\ServiceContainer\Provider; + +use PrestaShop\Module\PsAccounts\Account\LinkShop; +use PrestaShop\Module\PsAccounts\Account\Session\Firebase; +use PrestaShop\Module\PsAccounts\Account\Session\ShopSession; +use PrestaShop\Module\PsAccounts\Cqrs\CommandBus; +use PrestaShop\Module\PsAccounts\Provider\OAuth2\ShopProvider; +use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; +use PrestaShop\Module\PsAccounts\ServiceContainer\Contract\IServiceProvider; +use PrestaShop\Module\PsAccounts\ServiceContainer\ServiceContainer; + +class SessionProvider implements IServiceProvider +{ + /** + * @param ServiceContainer $container + * + * @return void + */ + public function provide(ServiceContainer $container) + { + // Sessions + $container->registerProvider(ShopSession::class, function () use ($container) { + return new ShopSession( + $container->get(ConfigurationRepository::class), + $container->get(ShopProvider::class), + $container->get(LinkShop::class), + $container->get(CommandBus::class) + ); + }); + $container->registerProvider(Firebase\OwnerSession::class, function () use ($container) { + return new Firebase\OwnerSession( + $container->get(ConfigurationRepository::class), + $container->get(ShopSession::class) + ); + }); + $container->registerProvider(Firebase\ShopSession::class, function () use ($container) { + return new Firebase\ShopSession( + $container->get(ConfigurationRepository::class), + $container->get(ShopSession::class) + ); + }); + } +} diff --git a/src/ServiceContainer/ServiceContainer.php b/src/ServiceContainer/ServiceContainer.php index 17ac0cea6..70547d894 100644 --- a/src/ServiceContainer/ServiceContainer.php +++ b/src/ServiceContainer/ServiceContainer.php @@ -20,6 +20,18 @@ namespace PrestaShop\Module\PsAccounts\ServiceContainer; +use PrestaShop\Module\PsAccounts\ServiceContainer\Contract\IServiceContainerService; +use PrestaShop\Module\PsAccounts\ServiceContainer\Contract\IServiceProvider; +use PrestaShop\Module\PsAccounts\ServiceContainer\Exception\ParameterNotFoundException; +use PrestaShop\Module\PsAccounts\ServiceContainer\Exception\ProviderNotFoundException; +use PrestaShop\Module\PsAccounts\ServiceContainer\Exception\ServiceNotFoundException; +use PrestaShop\Module\PsAccounts\ServiceContainer\Provider\ApiClientProvider; +use PrestaShop\Module\PsAccounts\ServiceContainer\Provider\CommandProvider; +use PrestaShop\Module\PsAccounts\ServiceContainer\Provider\DefaultProvider; +use PrestaShop\Module\PsAccounts\ServiceContainer\Provider\OAuth2Provider; +use PrestaShop\Module\PsAccounts\ServiceContainer\Provider\RepositoryProvider; +use PrestaShop\Module\PsAccounts\ServiceContainer\Provider\SessionProvider; + class ServiceContainer { /** @@ -37,6 +49,18 @@ class ServiceContainer */ protected $providers = []; + /** + * @var string[] + */ + protected $provides = [ + ApiClientProvider::class, + CommandProvider::class, + DefaultProvider::class, + OAuth2Provider::class, + RepositoryProvider::class, + SessionProvider::class, + ]; + /** * @var string */ @@ -49,19 +73,9 @@ class ServiceContainer public function __construct() { - $this->config = require_once __DIR__ . '/../../' . $this->configName . '.php'; - - $this->providers = [ - 'ps_accounts.context' => function () { - return \Context::getContext(); - }, - 'ps_accounts.logger' => function () { - return \PrestaShop\Module\PsAccounts\Log\Logger::create(); - }, - 'ps_accounts.module' => function () { - return \Module::getInstanceByName('ps_accounts'); - }, - ]; + $this->config = $this->loadConfig(); + + $this->init(); } /** @@ -76,6 +90,26 @@ public static function getInstance() return self::$instance; } + /** + * @return mixed + */ + public function loadConfig() + { + return require_once __DIR__ . '/../../' . $this->configName . '.php'; + } + + /** + * @return void + */ + public function init() + { + foreach ($this->provides as $provider) { + if (is_a($provider, IServiceProvider::class, true)) { + (new $provider())->provide($this); + } + } + } + /** * @param string $name * @@ -88,11 +122,20 @@ public function get($name) if ($this->has($name)) { return $this->services[$name]; } - if (array_key_exists($name, $this->providers)) { - return $this->providers[$name](); + + if ($this->hasProvider($name)) { + $service = $this->getProvider($name)(); + } else { + $service = $this->provideInstanceFromClassname($name); } - return $this->provideInstanceFromClassname($name); + if (null === $service) { + throw new ServiceNotFoundException('Service Not Found: ' . $name); + } + + $this->set($name, $service); + + return $service; } /** @@ -153,20 +196,54 @@ public function hasParameter($name) return array_key_exists($name, $this->config); } + /** + * @param string $name + * + * @return \Closure + * + * @throws ProviderNotFoundException + */ + public function getProvider($name) + { + if (array_key_exists($name, $this->providers)) { + return $this->providers[$name]; + } + throw new ProviderNotFoundException('Provider "' . $name . '" not found.'); + } + + /** + * @param string $name + * + * @return bool + */ + public function hasProvider($name) + { + return array_key_exists($name, $this->providers); + } + + /** + * @param string $name + * @param \Closure $provider + * + * @return void + */ + public function registerProvider($name, \Closure $provider) + { + //echo(sprintf('Initializing "%s"', $name) . PHP_EOL); + $this->providers[$name] = $provider; + } + /** * @param string $className * * @return mixed - * - * @throws ServiceNotFoundException */ protected function provideInstanceFromClassname($className) { - if (class_exists($className) && method_exists($className, 'getInstance')) { - $this->set($className, $className::getInstance($this)); - - return $this->services[$className]; + if (is_a($className, IServiceContainerService::class, true)) { + return $className::getInstance($this); } - throw new ServiceNotFoundException('Service Not Found: ' . $className); + + return null; } }