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

PIPRES-319: Lock and unlock cart actions #838

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
2 changes: 1 addition & 1 deletion mollie.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function __construct()
{
$this->name = 'mollie';
$this->tab = 'payments_gateways';
$this->version = '6.0.4';
$this->version = '6.0.5';
$this->author = 'Mollie B.V.';
$this->need_instance = 1;
$this->bootstrap = true;
Expand Down
5 changes: 5 additions & 0 deletions src/Adapter/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,9 @@ public function formatPrice(float $price, string $isoCode): string
$isoCode
);
}

public function getShopName(): string
{
return (string) PrestashopContext::getContext()->shop->name;
}
}
49 changes: 49 additions & 0 deletions src/Core/Payment/Action/LockCartAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Mollie\Core\Payment\Action;

use Mollie\Core\Payment\Exception\CouldNotLockCart;
use Mollie\Exception\MollieException;
use Mollie\Infrastructure\Context\GlobalShopContextInterface;
use Mollie\Infrastructure\EntityManager\EntityManagerInterface;
use Mollie\Infrastructure\EntityManager\ObjectModelUnitOfWork;
use Mollie\Logger\PrestaLoggerInterface;

class LockCartAction
{
private $logger;
private $globalShopContext;
private $entityManager;

public function __construct(
PrestaLoggerInterface $logger,
GlobalShopContextInterface $globalShopContext,
EntityManagerInterface $entityManager
) {
$this->logger = $logger;
$this->globalShopContext = $globalShopContext;
$this->entityManager = $entityManager;
}

/**
* @throws MollieException
*/
public function run(int $cartId): void
{
$this->logger->debug(sprintf('%s - Function called', __METHOD__));

try {
$cart = new \MollieCart();

$cart->id_cart = $cartId;
$cart->id_shop = $this->globalShopContext->getShopId();

$this->entityManager->persist($cart, ObjectModelUnitOfWork::UNIT_OF_WORK_SAVE);
$this->entityManager->flush();
} catch (\Exception $exception) {
throw CouldNotLockCart::unknownError($exception);
}

$this->logger->debug(sprintf('%s - Function ended', __METHOD__));
}
}
59 changes: 59 additions & 0 deletions src/Core/Payment/Action/UnlockCartAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Mollie\Core\Payment\Action;

use Mollie\Core\Payment\Exception\CouldNotUnlockCart;
use Mollie\Core\Shared\Repository\MollieCartRepositoryInterface;
use Mollie\Exception\MollieException;
use Mollie\Infrastructure\Context\GlobalShopContextInterface;
use Mollie\Infrastructure\EntityManager\EntityManagerInterface;
use Mollie\Infrastructure\EntityManager\ObjectModelUnitOfWork;
use Mollie\Logger\PrestaLoggerInterface;

class UnlockCartAction
{
private $logger;
private $globalShopContext;
private $entityManager;
private $mollieCartRepository;

public function __construct(
PrestaLoggerInterface $logger,
GlobalShopContextInterface $globalShopContext,
EntityManagerInterface $entityManager,
MollieCartRepositoryInterface $mollieCartRepository
) {
$this->logger = $logger;
$this->globalShopContext = $globalShopContext;
$this->entityManager = $entityManager;
$this->mollieCartRepository = $mollieCartRepository;
}

/**
* @throws MollieException
*/
public function run(int $cartId): void
{
$this->logger->debug(sprintf('%s - Function called', __METHOD__));

try {
$mollieCart = $this->mollieCartRepository->findOneBy([
'id_cart' => $cartId,
'id_shop' => $this->globalShopContext->getShopId(),
]);

if (!$mollieCart) {
$this->logger->debug(sprintf('%s - Function ended', __METHOD__));

return;
}

$this->entityManager->persist($mollieCart, ObjectModelUnitOfWork::UNIT_OF_WORK_DELETE);
$this->entityManager->flush();
} catch (\Throwable $exception) {
throw CouldNotUnlockCart::unknownError($exception);
}

$this->logger->debug(sprintf('%s - Function ended', __METHOD__));
}
}
9 changes: 9 additions & 0 deletions src/Core/Payment/Exception/CouldNotLockCart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Mollie\Core\Payment\Exception;

use Mollie\Exception\MollieException;

class CouldNotLockCart extends MollieException
{
}
9 changes: 9 additions & 0 deletions src/Core/Payment/Exception/CouldNotUnlockCart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Mollie\Core\Payment\Exception;

use Mollie\Exception\MollieException;

class CouldNotUnlockCart extends MollieException
{
}
19 changes: 19 additions & 0 deletions src/Core/Shared/Entity/MollieCart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

class MollieCart extends ObjectModel
{
public $id_mollie_cart;

public $id_cart;

public $id_shop;

public static $definition = [
'table' => 'mollie_cart',
'primary' => 'id_mollie_cart',
'fields' => [
'id_cart' => ['type' => self::TYPE_INT, 'validate' => 'isInt'],
'id_shop' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId'],
],
];
}
13 changes: 13 additions & 0 deletions src/Core/Shared/Repository/MollieCartRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Mollie\Core\Shared\Repository;

use Mollie\Repository\AbstractRepository;

class MollieCartRepository extends AbstractRepository implements MollieCartRepositoryInterface
{
public function __construct()
{
parent::__construct(\MollieCart::class);
}
}
9 changes: 9 additions & 0 deletions src/Core/Shared/Repository/MollieCartRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Mollie\Core\Shared\Repository;

use Mollie\Repository\ReadOnlyRepositoryInterface;

interface MollieCartRepositoryInterface extends ReadOnlyRepositoryInterface
{
}
50 changes: 50 additions & 0 deletions src/Infrastructure/Context/GlobalShopContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Mollie\Infrastructure\Context;

use Mollie\Adapter\Context;

final class GlobalShopContext implements GlobalShopContextInterface
{
private $context;

public function __construct(Context $context)
{
$this->context = $context;
}

public function getShopId(): int
{
return $this->context->getShopId();
}

public function getLanguageId(): int
{
return $this->context->getLanguageId();
}

public function getLanguageIso(): string
{
return $this->context->getLanguageIso();
}

public function getCurrencyIso(): string
{
return $this->context->getCurrencyIso();
}

public function getShopDomain(): string
{
return $this->context->getShopDomain();
}

public function getShopName(): string
{
return $this->context->getShopName();
}

public function isShopSingleShopContext(): bool
{
return \Shop::getContext() === \Shop::CONTEXT_SHOP;
}
}
20 changes: 20 additions & 0 deletions src/Infrastructure/Context/GlobalShopContextInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Mollie\Infrastructure\Context;

interface GlobalShopContextInterface
{
public function getShopId(): int;

public function getLanguageId(): int;

public function getLanguageIso(): string;

public function getCurrencyIso(): string;

public function getShopDomain(): string;

public function getShopName(): string;

public function isShopSingleShopContext(): bool;
}
36 changes: 36 additions & 0 deletions src/Infrastructure/EntityManager/EntityManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Mollie https://www.mollie.nl
*
* @author Mollie B.V. <[email protected]>
* @copyright Mollie B.V.
* @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md
*
* @see https://github.com/mollie/PrestaShop
*/

namespace Mollie\Infrastructure\EntityManager;

interface EntityManagerInterface
{
/**
* @param \ObjectModel $model
* @param string $unitOfWorkType - @see ObjectModelUnitOfWork
* @param string|null $specificKey
*
* @return EntityManagerInterface
*/
public function persist(
\ObjectModel $model,
string $unitOfWorkType,
?string $specificKey = null
): EntityManagerInterface;

/**
* @return array<\ObjectModel>
*
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function flush(): array;
}
58 changes: 58 additions & 0 deletions src/Infrastructure/EntityManager/ObjectModelManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Mollie https://www.mollie.nl
*
* @author Mollie B.V. <[email protected]>
* @copyright Mollie B.V.
* @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md
*
* @see https://github.com/mollie/PrestaShop
*/

namespace Mollie\Infrastructure\EntityManager;

class ObjectModelManager implements EntityManagerInterface
{
private $unitOfWork;

public function __construct(ObjectModelUnitOfWork $unitOfWork)
{
$this->unitOfWork = $unitOfWork;
}

public function persist(
\ObjectModel $model,
string $unitOfWorkType,
?string $specificKey = null
): EntityManagerInterface {
$this->unitOfWork->setWork($model, $unitOfWorkType, $specificKey);

return $this;
}

public function flush(): array
{
$persistenceModels = $this->unitOfWork->getWork();
$persistedModels = [];

foreach ($persistenceModels as $externalId => $persistenceModel) {
if ($persistenceModel['unit_of_work_type'] === ObjectModelUnitOfWork::UNIT_OF_WORK_SAVE) {
$persistenceModel['object']->save();
}

if ($persistenceModel['unit_of_work_type'] === ObjectModelUnitOfWork::UNIT_OF_WORK_DELETE) {
$persistenceModel['object']->delete();
}

if (!empty($externalId)) {
$persistedModels[$externalId] = $persistenceModel['object'];
} else {
$persistedModels[] = $persistenceModel['object'];
}
}

$this->unitOfWork->clearWork();

return $persistedModels;
}
}
Loading
Loading