-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use tweaked DoctrineStorage for Payum that accepts new Doctrine Persi…
…stence API
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/Sylius/Bundle/PayumBundle/DependencyInjection/Compiler/UseTweakedDoctrineStoragePass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters