custom/plugins/DreiscSeoPro/src/Core/Foundation/Dal/EntityRepository.php line 174

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DreiscSeoPro\Core\Foundation\Dal;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregatorResult;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  13. abstract class EntityRepository
  14. {
  15.     /**
  16.      * @var EntityRepositoryInterface
  17.      */
  18.     private $repository;
  19.     /**
  20.      * @var Context
  21.      */
  22.     private $defaultContext;
  23.     /**
  24.      * EntityRepository constructor.
  25.      * @param EntityRepositoryInterface $repository
  26.      */
  27.     public function __construct(EntityRepositoryInterface $repository)
  28.     {
  29.         $this->repository $repository;
  30.         $this->defaultContext Context::createDefaultContext();
  31.     }
  32.     /**
  33.      * @return EntityRepositoryInterface
  34.      */
  35.     public function getRepository(): EntityRepositoryInterface
  36.     {
  37.         return $this->repository;
  38.     }
  39.     /**
  40.      * @return EntityDefinition
  41.      */
  42.     public function getDefinition(): EntityDefinition
  43.     {
  44.         return $this->repository->getDefinition();
  45.     }
  46.     /**
  47.      * @param array $data
  48.      * @param Context|null $context
  49.      * @return EntityWrittenContainerEvent
  50.      */
  51.     public function create(array $data, ?Context $context null): EntityWrittenContainerEvent
  52.     {
  53.         $context $this->processContext($context);
  54.         $data $this->processData($data);
  55.         return $this->repository->create($data$context);
  56.     }
  57.     /**
  58.      * @param array $data
  59.      * @param Context|null $context
  60.      * @return EntityWrittenContainerEvent
  61.      */
  62.     public function update(array $data, ?Context $context null): EntityWrittenContainerEvent
  63.     {
  64.         $context $this->processContext($context);
  65.         $data $this->processData($data);
  66.         return $this->repository->update($data$context);
  67.     }
  68.     /**
  69.      * @param array $updateData
  70.      * @param Criteria $criteria
  71.      * @param Context|null $context
  72.      * @return EntityWrittenContainerEvent
  73.      */
  74.     public function updateByCriteria(array $updateDataCriteria $criteria, ?Context $context null): EntityWrittenContainerEvent
  75.     {
  76.         $context $this->processContext($context);
  77.         $result $this->searchIds($criteria$context);
  78.         $updates = [];
  79.         foreach($result->getIds() as $id) {
  80.             /** Add the id we want to update */
  81.             $itemUpdate array_merge($updateData, [ 'id' => $id ]);
  82.             /** Add to queue */
  83.             $updates[] = $itemUpdate;
  84.         }
  85.         return $this->update($updates$context);
  86.     }
  87.     /**
  88.      * @param array $data
  89.      * @param Context|null $context
  90.      * @return EntityWrittenContainerEvent
  91.      */
  92.     public function upsert(array $data, ?Context $context null): EntityWrittenContainerEvent
  93.     {
  94.         $context $this->processContext($context);
  95.         $data $this->processData($data);
  96.         return $this->repository->upsert($data$context);
  97.     }
  98.     /**
  99.      * @param array $data
  100.      * @param Context|null $context
  101.      * @return EntityWrittenContainerEvent
  102.      */
  103.     public function delete(array $data, ?Context $context null): EntityWrittenContainerEvent
  104.     {
  105.         $context $this->processContext($context);
  106.         $data $this->processData($data);
  107.         return $this->repository->delete($data$context);
  108.     }
  109.     /**
  110.      * @param string $entityId
  111.      * @param Context|null $context
  112.      * @return EntityWrittenContainerEvent
  113.      */
  114.     public function deleteById(string $entityId, ?Context $context null): EntityWrittenContainerEvent
  115.     {
  116.         $context $this->processContext($context);
  117.         return $this->repository->delete([
  118.             [ 'id' => $entityId ]
  119.         ], $context);
  120.     }
  121.     /**
  122.      * @param Criteria $criteria
  123.      * @param Context|null $context
  124.      * @return EntityWrittenContainerEvent|null
  125.      */
  126.     public function deleteByCriteria(Criteria $criteria, ?Context $context null): ?EntityWrittenContainerEvent
  127.     {
  128.         $context $this->processContext($context);
  129.         $result $this->searchIds($criteria$context);
  130.         $ids = [];
  131.         foreach($result->getIds() as $id) {
  132.             $ids[] = [ 'id' => $id ];
  133.         }
  134.         if(empty($ids)) {
  135.             return null;
  136.         }
  137.         return $this->repository->delete($ids$context);
  138.     }
  139.     /**
  140.      * @param string $id
  141.      * @param array|null $associations
  142.      * @param Context|null $context
  143.      * @param bool $disableCache
  144.      * @return Entity|null
  145.      * @throws InconsistentCriteriaIdsException
  146.      */
  147.     public function get(string $id, array $associations null, ?Context $context null$disableCache false): ?Entity
  148.     {
  149.         $context $this->processContext($context);
  150.         $criteria = new Criteria([ $id ]);
  151.         if (is_array($associations)) {
  152.             foreach($associations as $association) {
  153.                 $criteria->addAssociation($association);
  154.             }
  155.         }
  156.         /** Ignore the cache if necessary  */
  157.         if (true === $disableCache) {
  158.             $repository $this->repository;
  159.             return $repository->search(
  160.                 $criteria,
  161.                 $context
  162.             )->first();
  163.         }
  164.         return $this->repository->search(
  165.             $criteria,
  166.             $context
  167.         )->first();
  168.     }
  169.     /**
  170.      * @param Criteria $criteria
  171.      * @param Context|null $context
  172.      * @param bool $disableCache
  173.      * @return EntitySearchResult
  174.      */
  175.     public function search(Criteria $criteria, ?Context $context null$disableCache false): EntitySearchResult
  176.     {
  177.         $context $this->processContext($context);
  178.         /** Ignore the cache if necessary  */
  179.         if (true === $disableCache) {
  180.             $repository $this->repository;
  181.             return $repository->search($criteria$context);
  182.         }
  183.         return $this->repository->search($criteria$context);
  184.     }
  185.     /**
  186.      * @param Criteria $criteria
  187.      * @param Context|null $context
  188.      * @param bool $disableCache
  189.      * @return IdSearchResult
  190.      */
  191.     public function searchIds(Criteria $criteria, ?Context $context null$disableCache false): IdSearchResult
  192.     {
  193.         $context $this->processContext($context);
  194.         /** Ignore the cache if necessary  */
  195.         if (true === $disableCache) {
  196.             $repository $this->repository;
  197.             return $repository->searchIds($criteria$context);
  198.         }
  199.         return $this->repository->searchIds($criteria$context);
  200.     }
  201.     /**
  202.      * @param string $id
  203.      * @param Context|null $context
  204.      * @param string|null $newId
  205.      * @return EntityWrittenContainerEvent
  206.      */
  207.     public function clone(string $id, ?Context $context null, ?string $newId null): EntityWrittenContainerEvent
  208.     {
  209.         $context $this->processContext($context);
  210.         return $this->repository->clone($id$context$newId);
  211.     }
  212.     /**
  213.      * @param Criteria $criteria
  214.      * @param Context|null $context
  215.      * @return AggregatorResult
  216.      */
  217.     public function aggregate(Criteria $criteria, ?Context $context null): AggregatorResult
  218.     {
  219.         $context $this->processContext($context);
  220.         return $this->repository->aggregate($criteria$context);
  221.     }
  222.     /**
  223.      * @param string $id
  224.      * @param Context|null $context
  225.      * @param string|null $name
  226.      * @param string|null $versionId
  227.      * @return string
  228.      */
  229.     public function createVersion(string $id, ?Context $context null, ?string $name null, ?string $versionId null): string
  230.     {
  231.         $context $this->processContext($context);
  232.         return $this->repository->createVersion($id$context$name$versionId);
  233.     }
  234.     /**
  235.      * @param string $versionId
  236.      * @param Context|null $context
  237.      */
  238.     public function merge(string $versionId, ?Context $context null): void
  239.     {
  240.         $context $this->processContext($context);
  241.         $this->repository->merge($versionId$context);
  242.     }
  243.     /**
  244.      * @param Context|null $context
  245.      * @return Context
  246.      */
  247.     private function processContext(?Context $context): Context
  248.     {
  249.         if(null == $context) {
  250.             $context $this->defaultContext;
  251.         }
  252.         return $context;
  253.     }
  254.     /**
  255.      * Converts the data, so that we can save it
  256.      *
  257.      * @param array $data
  258.      * @return array
  259.      */
  260.     private function processData($data): array
  261.     {
  262.         foreach($data as $key => $item) {
  263.             /** Convert to array, if its an entity */
  264.             if($item instanceof Entity) {
  265.                 $data[$key] = $item->toArray();
  266.             }
  267.             /** Throw an exception, if it's not a array in a array */
  268.             if (!is_array($data[$key])) {
  269.                 throw new \RuntimeException('The array $data must contain an other array!');
  270.             }
  271.             /**
  272.              * Convert the entity to an array and clean up all null-value fields,
  273.              * so that the create method of the DAL works correctly
  274.              */
  275.             $data[$key] = $this->unsetNullFields($data[$key]);
  276.             /**
  277.              * Special case:
  278.              * Remove empty translated and extensions keys
  279.              */
  280.             if(isset($data[$key]['translated']) && empty($data[$key]['translated'])) {
  281.                 unset($data[$key]['translated']);
  282.             }
  283.             if(isset($data[$key]['extensions']) && empty($data[$key]['extensions'])) {
  284.                 unset($data[$key]['extensions']);
  285.             }
  286.         }
  287.         return $data;
  288.     }
  289.     /**
  290.      * @param $array
  291.      * @return array
  292.      */
  293.     private function unsetNullFields($array): array
  294.     {
  295.         foreach($array as $fieldKey => $fieldValue) {
  296.             /** Start the same process for the child entities */
  297.             if($fieldValue instanceof Entity) {
  298.                 $array[$fieldKey] = current($this->processData([ $fieldValue ]));
  299.             }
  300.             /** Recursive action  */
  301.             elseif(is_array($fieldValue)) {
  302.                 $array[$fieldKey] = $this->unsetNullFields($fieldValue);
  303.             }
  304.             /** Unset field, if its null */
  305.             elseif(null === $fieldValue) {
  306.                 unset($array[$fieldKey]);
  307.             }
  308.         }
  309.         return $array;
  310.     }
  311. }