Use tweaked DoctrineStorage for Payum that accepts new Doctrine Persistence API

This commit is contained in:
Kamil Kokot 2021-01-12 16:27:24 +01:00
parent bcf9be87dd
commit 11303b6489
3 changed files with 84 additions and 0 deletions

View file

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Sylius\Bundle\PayumBundle\DependencyInjection\Compiler;
use Sylius\Bundle\PayumBundle\Storage\DoctrineStorage;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
final class UseTweakedDoctrineStoragePass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
$container->setParameter('payum.storage.doctrine.orm.class', DoctrineStorage::class);
}
}

View file

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace Sylius\Bundle\PayumBundle\Storage;
use Doctrine\Persistence\ObjectManager;
use Payum\Core\Model\Identity;
use Payum\Core\Storage\AbstractStorage;
/**
* It's a drop-in replacement for DoctrineStorage that accepts
* Doctrine\Persistence\ObjectManager instead of Doctrine\Common\Persistence\ObjectManager.
*
* @internal
*
* @see \Payum\Core\Bridge\Doctrine\Storage\DoctrineStorage
*/
class DoctrineStorage extends AbstractStorage
{
/**
* @var ObjectManager
*/
protected $objectManager;
public function __construct(ObjectManager $objectManager, $modelClass)
{
parent::__construct($modelClass);
$this->objectManager = $objectManager;
}
public function findBy(array $criteria): array
{
return $this->objectManager->getRepository($this->modelClass)->findBy($criteria);
}
protected function doFind($id): ?object
{
return $this->objectManager->find($this->modelClass, $id);
}
protected function doUpdateModel($model): void
{
$this->objectManager->persist($model);
$this->objectManager->flush();
}
protected function doDeleteModel($model): void
{
$this->objectManager->remove($model);
$this->objectManager->flush();
}
protected function doGetIdentity($model): Identity
{
$modelMetadata = $this->objectManager->getClassMetadata(get_class($model));
$id = $modelMetadata->getIdentifierValues($model);
if (count($id) > 1) {
throw new \LogicException('Storage not support composite primary ids');
}
return new Identity(array_shift($id), $model);
}
}

View file

@ -14,6 +14,7 @@ declare(strict_types=1);
namespace Sylius\Bundle\PayumBundle;
use Sylius\Bundle\PayumBundle\DependencyInjection\Compiler\RegisterGatewayConfigTypePass;
use Sylius\Bundle\PayumBundle\DependencyInjection\Compiler\UseTweakedDoctrineStoragePass;
use Sylius\Bundle\ResourceBundle\AbstractResourceBundle;
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
@ -32,5 +33,6 @@ final class SyliusPayumBundle extends AbstractResourceBundle
parent::build($container);
$container->addCompilerPass(new RegisterGatewayConfigTypePass());
$container->addCompilerPass(new UseTweakedDoctrineStoragePass());
}
}