Skip to content

Commit

Permalink
Refactored some services to use dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
L3RAZ committed Apr 18, 2024
1 parent 1f1febe commit a0e87cc
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 49 deletions.
22 changes: 19 additions & 3 deletions config/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,21 @@ services:
PrestaShop\Module\PrestashopCheckout\Environment\Env:
class: 'PrestaShop\Module\PrestashopCheckout\Environment\Env'
public: true
arguments:
- '@PrestaShop\Module\PrestashopCheckout\PayPal\PayPalConfiguration'

PrestaShop\Module\PrestashopCheckout\Environment\PaymentEnv:
class: 'PrestaShop\Module\PrestashopCheckout\Environment\PaymentEnv'
public: true
arguments:
- '@PrestaShop\Module\PrestashopCheckout\PayPal\PayPalConfiguration'


PrestaShop\Module\PrestashopCheckout\Environment\PaypalEnv:
class: 'PrestaShop\Module\PrestashopCheckout\Environment\PaypalEnv'
public: true
arguments:
- '@PrestaShop\Module\PrestashopCheckout\PayPal\PayPalConfiguration'

PrestaShop\Module\PrestashopCheckout\Event\SymfonyEventDispatcherFactory:
class: 'PrestaShop\Module\PrestashopCheckout\Event\SymfonyEventDispatcherFactory'
Expand All @@ -61,15 +68,15 @@ services:
public: true
arguments:
- '@PrestaShop\Module\PrestashopCheckout\Checkout\CheckoutChecker'
- '@ps_checkout.bus.command'
- '@ps_checkout.module'
- '@PrestaShop\Module\PrestashopCheckout\Repository\PsCheckoutCartRepository'

PrestaShop\Module\PrestashopCheckout\Order\EventSubscriber\OrderEventSubscriber:
class: 'PrestaShop\Module\PrestashopCheckout\Order\EventSubscriber\OrderEventSubscriber'
public: true
arguments:
- '@PrestaShop\Module\PrestashopCheckout\Repository\PsCheckoutCartRepository'
- '@ps_checkout.bus.command'
- '@ps_checkout.module'

PrestaShop\Module\PrestashopCheckout\PayPal\Order\EventSubscriber\PayPalOrderEventSubscriber:
class: 'PrestaShop\Module\PrestashopCheckout\PayPal\Order\EventSubscriber\PayPalOrderEventSubscriber'
Expand Down Expand Up @@ -195,6 +202,7 @@ services:
- '@PrestaShop\Module\PrestashopCheckout\ShopContext'
- '@PrestaShop\Module\PrestashopCheckout\Context\PrestaShopContext'
- '@ps_checkout.logger'
- '@PrestaShop\Module\PrestashopCheckout\Environment\PaypalEnv'

PrestaShop\Module\PrestashopCheckout\Presenter\Store\Modules\PaypalModule:
class: 'PrestaShop\Module\PrestashopCheckout\Presenter\Store\Modules\PaypalModule'
Expand Down Expand Up @@ -425,7 +433,7 @@ services:
class: 'PrestaShop\Module\PrestashopCheckout\Provider\PaymentMethodLogoProvider'
public: true
arguments:
- '@ps_checkout.module'
- '@=service("ps_checkout.module").getPathUri()'

PrestaShop\Module\PrestashopCheckout\Dispatcher\OrderDispatcher:
class: 'PrestaShop\Module\PrestashopCheckout\Dispatcher\OrderDispatcher'
Expand All @@ -434,3 +442,11 @@ services:
- '@PrestaShop\Module\PrestashopCheckout\Event\SymfonyEventDispatcherAdapter'
- '@ps_checkout.logger'
- '@PrestaShop\Module\PrestashopCheckout\PayPal\PayPalConfiguration'

PrestaShop\Module\PrestashopCheckout\Presenter\Order\OrderPresenter:
class: 'PrestaShop\Module\PrestashopCheckout\Presenter\Order\OrderPresenter'
public: true
arguments:
- '@ps_checkout.module'
- '@PrestaShop\Module\PrestashopCheckout\FundingSource\FundingSourceTranslationProvider'
- '@PrestaShop\Module\PrestashopCheckout\Provider\PaymentMethodLogoProvider'
5 changes: 3 additions & 2 deletions controllers/admin/AdminAjaxPrestashopCheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,12 +443,13 @@ public function ajaxProcessFetchOrder()

/** @var FundingSourceTranslationProvider $fundingSourceTranslationProvider */
$fundingSourceTranslationProvider = $this->module->getService(FundingSourceTranslationProvider::class);
$presenter = new OrderPresenter($this->module, $paypalOrder);
/** @var OrderPresenter $presenter */
$presenter = $this->module->getService(OrderPresenter::class);

$this->context->smarty->assign([
'moduleName' => $this->module->displayName,
'moduleUrl' => $this->context->link->getAdminLink('AdminModules', true, [], ['configure' => 'ps_checkout']),
'orderPayPal' => $presenter->present(),
'orderPayPal' => $presenter->present($paypalOrder),
'orderPayPalBaseUrl' => $this->context->link->getAdminLink('AdminAjaxPrestashopCheckout'),
'moduleLogoUri' => $this->module->getPathUri() . 'logo.png',
'orderPaymentDisplayName' => $fundingSourceTranslationProvider->getPaymentMethodName($psCheckoutCart->paypal_funding),
Expand Down
2 changes: 1 addition & 1 deletion ps_checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ public function getContent()
'contextPsAccounts' => $contextPsAccounts,
]);

$env = new \PrestaShop\Module\PrestashopCheckout\Environment\Env();
$env = $this->getService(\PrestaShop\Module\PrestashopCheckout\Environment\Env::class);
$boSdkUrl = $env->getEnv('CHECKOUT_BO_SDK_URL');
if (substr($boSdkUrl, -3) !== '.js') {
$boSdkVersion = $env->getEnv('CHECKOUT_BO_SDK_VERSION');
Expand Down
8 changes: 2 additions & 6 deletions src/Checkout/EventSubscriber/CheckoutEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@

class CheckoutEventSubscriber implements EventSubscriberInterface
{
/**
* @var Ps_checkout
*/
private $module;

/**
* @var CommandBusInterface
Expand All @@ -62,11 +58,11 @@ class CheckoutEventSubscriber implements EventSubscriberInterface
*/
private $psCheckoutCartRepository;

public function __construct(CheckoutChecker $checkoutChecker, CommandBusInterface $commandBus, PsCheckoutCartRepository $psCheckoutCartRepository)
public function __construct(CheckoutChecker $checkoutChecker, Ps_checkout $module, PsCheckoutCartRepository $psCheckoutCartRepository)
{
$this->checkoutChecker = $checkoutChecker;
$this->commandBus = $commandBus;
$this->psCheckoutCartRepository = $psCheckoutCartRepository;
$this->commandBus = $module->getService('ps_checkout.bus.command');
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Environment/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

namespace PrestaShop\Module\PrestashopCheckout\Environment;

use PrestaShop\Module\PrestashopCheckout\PayPal\PayPalConfiguration;
use Ps_checkout;

/**
* Get the current environment used: prod or test // sandbox or live
*/
Expand Down Expand Up @@ -52,7 +55,7 @@ class Env
*/
protected $mode;

public function __construct()
public function __construct(PayPalConfiguration $payPalConfiguration)
{
foreach (self::FILE_ENV_LIST as $env => $fileName) {
if (!file_exists(_PS_MODULE_DIR_ . 'ps_checkout/' . $fileName)) {
Expand All @@ -67,10 +70,7 @@ public function __construct()
break;
}

/** @var \Ps_checkout $module */
$module = \Module::getInstanceByName('ps_checkout');

$this->setMode($module->getService(\PrestaShop\Module\PrestashopCheckout\PayPal\PayPalConfiguration::class)->getPaymentMode());
$this->setMode($payPalConfiguration->getPaymentMode());
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Environment/PaymentEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace PrestaShop\Module\PrestashopCheckout\Environment;

use PrestaShop\Module\PrestashopCheckout\PayPal\Mode;
use PrestaShop\Module\PrestashopCheckout\PayPal\PayPalConfiguration;

/**
* Allow to set the differents api key / api link depending on
Expand All @@ -36,9 +37,9 @@ class PaymentEnv extends Env

private $checkoutApiUrl;

public function __construct()
public function __construct(PayPalConfiguration $payPalConfiguration)
{
parent::__construct();
parent::__construct($payPalConfiguration);

$this->setEnvDependingOnMode();
}
Expand Down
5 changes: 3 additions & 2 deletions src/Environment/PaypalEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace PrestaShop\Module\PrestashopCheckout\Environment;

use PrestaShop\Module\PrestashopCheckout\PayPal\Mode;
use PrestaShop\Module\PrestashopCheckout\PayPal\PayPalConfiguration;

/**
* Allow to set the differents api key / api link depending on
Expand All @@ -34,9 +35,9 @@ class PaypalEnv extends Env
*/
private $paypalClientId;

public function __construct()
public function __construct(PayPalConfiguration $payPalConfiguration)
{
parent::__construct();
parent::__construct($payPalConfiguration);

$this->setEnvDependingOnMode();
}
Expand Down
5 changes: 3 additions & 2 deletions src/Order/EventSubscriber/OrderEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Exception\PayPalOrderException;
use PrestaShop\Module\PrestashopCheckout\Repository\PsCheckoutCartRepository;
use PrestaShopException;
use Ps_checkout;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class OrderEventSubscriber implements EventSubscriberInterface
Expand All @@ -43,10 +44,10 @@ class OrderEventSubscriber implements EventSubscriberInterface

public function __construct(
PsCheckoutCartRepository $psCheckoutCartRepository,
CommandBusInterface $commandBus
Ps_checkout $module
) {
$this->psCheckoutCartRepository = $psCheckoutCartRepository;
$this->commandBus = $commandBus;
$this->commandBus = $module->getService('ps_checkout.bus.command');
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/PayPal/Sdk/PayPalSdkConfigurationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class PayPalSdkConfigurationBuilder
* @var LoggerInterface
*/
private $logger;
/**
* @var PaypalEnv
*/
private $paypalEnv;

/**
* @param \Ps_checkout $module
Expand All @@ -84,6 +88,7 @@ class PayPalSdkConfigurationBuilder
* @param ShopContext $shopContext
* @param PrestaShopContext $prestaShopContext
* @param LoggerInterface $logger
* @param PaypalEnv $paypalEnv
*/
public function __construct(
\Ps_checkout $module,
Expand All @@ -93,7 +98,8 @@ public function __construct(
ExpressCheckoutConfiguration $expressCheckoutConfiguration,
ShopContext $shopContext,
PrestaShopContext $prestaShopContext,
LoggerInterface $logger
LoggerInterface $logger,
PaypalEnv $paypalEnv
) {
$this->configuration = $configuration;
$this->payLaterConfiguration = $payLaterConfiguration;
Expand All @@ -103,6 +109,7 @@ public function __construct(
$this->commandBus = $module->getService('ps_checkout.bus.command');
$this->prestaShopContext = $prestaShopContext;
$this->logger = $logger;
$this->paypalEnv = $paypalEnv;
}

/**
Expand All @@ -128,7 +135,7 @@ public function buildConfiguration()
}

$params = [
'clientId' => (new PaypalEnv())->getPaypalClientId(),
'clientId' => $this->paypalEnv->getPaypalClientId(),
'merchantId' => $this->configuration->getMerchantId(),
'currency' => $this->prestaShopContext->getCurrencyIsoCode(),
'intent' => strtolower($this->configuration->getIntent()),
Expand Down
26 changes: 17 additions & 9 deletions src/Presenter/Order/OrderPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,33 @@ class OrderPresenter
* @var FundingSourceTranslationProvider
*/
private $fundingSourceTranslationProvider;
/**
* @var PaymentMethodLogoProvider
*/
private $paymentMethodLogoProvider;

/**
* @param Ps_checkout $module
* @param array $orderPayPal
* @param FundingSourceTranslationProvider $fundingSourceTranslationProvider
* @param PaymentMethodLogoProvider $paymentMethodLogoProvider
*/
public function __construct(Ps_checkout $module, array $orderPayPal)
{
public function __construct(
Ps_checkout $module,
FundingSourceTranslationProvider $fundingSourceTranslationProvider,
PaymentMethodLogoProvider $paymentMethodLogoProvider
) {
$this->module = $module;
$this->orderPayPal = $orderPayPal;
/** @var FundingSourceTranslationProvider $fundingSourceTranslationProvider */
$fundingSourceTranslationProvider = $this->module->getService(FundingSourceTranslationProvider::class);
$this->fundingSourceTranslationProvider = $fundingSourceTranslationProvider;
$this->paymentMethodLogoProvider = $paymentMethodLogoProvider;
}

/**
* @return array
*/
public function present()
public function present(array $orderPayPal)
{
$this->orderPayPal = $orderPayPal;

if (empty($this->orderPayPal)) {
return [];
}
Expand Down Expand Up @@ -272,7 +280,7 @@ private function getTotal()
continue;
}

$total += (float) $purchase['amount']['value'];
$total += (float)$purchase['amount']['value'];
$currency = $purchase['amount']['currency_code'];
}

Expand Down Expand Up @@ -382,7 +390,7 @@ private function getPaymentSourceName(array $orderPayPal)
private function getPaymentSourceLogo(array $orderPayPal)
{
if (isset($orderPayPal['payment_source'])) {
return (new PaymentMethodLogoProvider($this->module))->getLogoByPaymentSource($orderPayPal['payment_source']);
return $this->paymentMethodLogoProvider->getLogoByPaymentSource($orderPayPal['payment_source']);
}

return '';
Expand Down
1 change: 1 addition & 0 deletions src/Presenter/Store/Modules/ConfigurationModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class ConfigurationModule implements PresenterInterface
* @param ExpressCheckoutConfiguration $ecConfiguration
* @param PayPalConfiguration $paypalConfiguration
* @param FundingSourceProvider $fundingSourceProvider
* @param Ps_checkout $module
*/
public function __construct(
PayPalPayLaterConfiguration $payLaterConfiguration,
Expand Down
28 changes: 14 additions & 14 deletions src/Provider/PaymentMethodLogoProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
class PaymentMethodLogoProvider
{
/**
* @var Ps_checkout
* @var string
*/
private $module;
private $modulePathUri;

public function __construct(Ps_checkout $module)
public function __construct($modulePathUri)
{
$this->module = $module;
$this->modulePathUri = $modulePathUri;
}

/**
Expand All @@ -46,26 +46,26 @@ public function getLogoByPaymentSource($paymentSource)
if ($paymentSourceName === 'card' && isset($paymentSource['card']['brand'])) {
switch ($paymentSource['card']['brand']) {
case 'CB_NATIONALE':
return $this->module->getPathUri() . 'views/img/cb.svg';
return $this->modulePathUri . 'views/img/cb.svg';
case 'VISA':
return $this->module->getPathUri() . 'views/img/visa.svg';
return $this->modulePathUri . 'views/img/visa.svg';
case 'MASTERCARD':
return $this->module->getPathUri() . 'views/img/mastercard.svg';
return $this->modulePathUri . 'views/img/mastercard.svg';
case 'AMEX':
return $this->module->getPathUri() . 'views/img/amex.svg';
return $this->modulePathUri . 'views/img/amex.svg';
case 'DISCOVER':
return $this->module->getPathUri() . 'views/img/discover.svg';
return $this->modulePathUri . 'views/img/discover.svg';
case 'JCB':
return $this->module->getPathUri() . 'views/img/jcb.svg';
return $this->modulePathUri . 'views/img/jcb.svg';
case 'DINERS':
return $this->module->getPathUri() . 'views/img/diners.svg';
return $this->modulePathUri . 'views/img/diners.svg';
case 'UNIONPAY':
return $this->module->getPathUri() . 'views/img/unionpay.svg';
return $this->modulePathUri . 'views/img/unionpay.svg';
case 'MAESTRO':
return $this->module->getPathUri() . 'views/img/maestro.svg';
return $this->modulePathUri . 'views/img/maestro.svg';
}
}

return $this->module->getPathUri() . 'views/img/' . $paymentSourceName . '.svg';
return $this->modulePathUri . 'views/img/' . $paymentSourceName . '.svg';
}
}
1 change: 0 additions & 1 deletion src/Repository/PsAccountRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class PsAccountRepository
private $psAccountsService;

/**
* @param PrestaShopConfiguration $configuration
* @param PsAccounts $psAccountsFacade
*/
public function __construct(PsAccounts $psAccountsFacade)
Expand Down

0 comments on commit a0e87cc

Please sign in to comment.