vendor/api-platform/core/src/Bridge/Symfony/Bundle/DataPersister/TraceableChainDataPersister.php line 56

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Core\Bridge\Symfony\Bundle\DataPersister;
  12. use ApiPlatform\Core\DataPersister\ChainDataPersister;
  13. use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
  14. use ApiPlatform\Core\DataPersister\DataPersisterInterface;
  15. /**
  16.  * @author Anthony GRASSIOT <antograssiot@free.fr>
  17.  */
  18. final class TraceableChainDataPersister implements ContextAwareDataPersisterInterface
  19. {
  20.     private $persisters = [];
  21.     private $persistersResponse = [];
  22.     private $decorated;
  23.     public function __construct(DataPersisterInterface $dataPersister)
  24.     {
  25.         if ($dataPersister instanceof ChainDataPersister) {
  26.             $this->decorated $dataPersister;
  27.             $this->persisters $dataPersister->persisters;
  28.         }
  29.     }
  30.     public function getPersistersResponse(): array
  31.     {
  32.         return $this->persistersResponse;
  33.     }
  34.     /**
  35.      * {@inheritdoc}
  36.      */
  37.     public function supports($data, array $context = []): bool
  38.     {
  39.         return $this->decorated->supports($data$context);
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function persist($data, array $context = [])
  45.     {
  46.         if ($match $this->tracePersisters($data$context)) {
  47.             return $match->persist($data$context) ?? $data;
  48.         }
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public function remove($data, array $context = [])
  54.     {
  55.         if ($match $this->tracePersisters($data$context)) {
  56.             return $match->remove($data$context);
  57.         }
  58.     }
  59.     private function tracePersisters($data, array $context = [])
  60.     {
  61.         $match null;
  62.         foreach ($this->persisters as $persister) {
  63.             $this->persistersResponse[\get_class($persister)] = $match null false;
  64.             if (!$match && $persister->supports($data$context)) {
  65.                 $match $persister;
  66.                 $this->persistersResponse[\get_class($persister)] = true;
  67.             }
  68.         }
  69.         return $match;
  70.     }
  71. }