Skip to content

Commit

Permalink
Use tweaked DoctrineStorage for Payum that accepts new Doctrine Persi…
Browse files Browse the repository at this point in the history
…stence API
  • Loading branch information
pamil committed Jan 21, 2021
1 parent bcf9be8 commit 11303b6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
65 changes: 65 additions & 0 deletions src/Sylius/Bundle/PayumBundle/Storage/DoctrineStorage.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions src/Sylius/Bundle/PayumBundle/SyliusPayumBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;
Expand All @@ -32,5 +33,6 @@ public function build(ContainerBuilder $container): void
parent::build($container);

$container->addCompilerPass(new RegisterGatewayConfigTypePass());
$container->addCompilerPass(new UseTweakedDoctrineStoragePass());
}
}

0 comments on commit 11303b6

Please sign in to comment.