vendor/sulu/sulu/src/Sulu/Bundle/SnippetBundle/Snippet/DefaultSnippetManager.php line 115

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\SnippetBundle\Snippet;
  11. use Sulu\Bundle\ActivityBundle\Application\Collector\DomainEventCollectorInterface;
  12. use Sulu\Bundle\SnippetBundle\Document\SnippetDocument;
  13. use Sulu\Bundle\SnippetBundle\Domain\Event\WebspaceDefaultSnippetModifiedEvent;
  14. use Sulu\Bundle\SnippetBundle\Domain\Event\WebspaceDefaultSnippetRemovedEvent;
  15. use Sulu\Component\DocumentManager\DocumentManagerInterface;
  16. use Sulu\Component\DocumentManager\DocumentRegistry;
  17. use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
  18. use Sulu\Component\Webspace\Settings\SettingsManagerInterface;
  19. use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
  20. /**
  21.  * Manages default snippets.
  22.  */
  23. class DefaultSnippetManager implements DefaultSnippetManagerInterface
  24. {
  25.     /**
  26.      * @var WebspaceManagerInterface
  27.      */
  28.     private $webspaceManager;
  29.     /**
  30.      * @var SettingsManagerInterface
  31.      */
  32.     private $settingsManager;
  33.     /**
  34.      * @var DocumentManagerInterface
  35.      */
  36.     private $documentManager;
  37.     /**
  38.      * @var DocumentRegistry
  39.      */
  40.     private $registry;
  41.     /**
  42.      * @var DomainEventCollectorInterface
  43.      */
  44.     private $domainEventCollector;
  45.     /**
  46.      * @var FrozenParameterBag
  47.      */
  48.     private $areas;
  49.     public function __construct(
  50.         SettingsManagerInterface $settingsManager,
  51.         DocumentManagerInterface $documentManager,
  52.         WebspaceManagerInterface $webspaceManager,
  53.         DocumentRegistry $registry,
  54.         DomainEventCollectorInterface $domainEventCollector,
  55.         array $areas
  56.     ) {
  57.         $this->settingsManager $settingsManager;
  58.         $this->documentManager $documentManager;
  59.         $this->webspaceManager $webspaceManager;
  60.         $this->registry $registry;
  61.         $this->domainEventCollector $domainEventCollector;
  62.         $this->areas = new FrozenParameterBag($areas);
  63.     }
  64.     public function save($webspaceKey$type$uuid$locale)
  65.     {
  66.         $document $this->documentManager->find($uuid$locale);
  67.         if (!$document instanceof SnippetDocument) {
  68.             throw new SnippetNotFoundException($uuid);
  69.         }
  70.         if (!$this->checkTemplate($document$type)) {
  71.             throw new WrongSnippetTypeException($document->getStructureType(), $type$document);
  72.         }
  73.         $this->settingsManager->save(
  74.             $webspaceKey,
  75.             'snippets-' $type,
  76.             $this->registry->getNodeForDocument($document)
  77.         );
  78.         $this->domainEventCollector->collect(
  79.             new WebspaceDefaultSnippetModifiedEvent($webspaceKey$type$document)
  80.         );
  81.         $this->domainEventCollector->dispatch();
  82.         return $document;
  83.     }
  84.     public function remove($webspaceKey$type)
  85.     {
  86.         $this->settingsManager->remove($webspaceKey'snippets-' $type);
  87.         $this->domainEventCollector->collect(
  88.             new WebspaceDefaultSnippetRemovedEvent($webspaceKey$type)
  89.         );
  90.         $this->domainEventCollector->dispatch();
  91.     }
  92.     public function load($webspaceKey$type$locale)
  93.     {
  94.         $snippetNode $this->settingsManager->load($webspaceKey'snippets-' $type);
  95.         if (null === $snippetNode) {
  96.             return;
  97.         }
  98.         $uuid $snippetNode->getIdentifier();
  99.         /** @var SnippetDocument $document */
  100.         $document $this->documentManager->find($uuid$locale);
  101.         if (null !== $document && !$this->checkTemplate($document$type)) {
  102.             throw new WrongSnippetTypeException($document->getStructureType(), $type$document);
  103.         }
  104.         return $document;
  105.     }
  106.     public function isDefault($uuid)
  107.     {
  108.         foreach ($this->webspaceManager->getWebspaceCollection() as $webspace) {
  109.             $settings $this->settingsManager->loadStringByWildcard($webspace->getKey(), 'snippets-*');
  110.             if (\in_array($uuid$settings)) {
  111.                 return true;
  112.             }
  113.         }
  114.         return false;
  115.     }
  116.     public function loadType($uuid)
  117.     {
  118.         foreach ($this->webspaceManager->getWebspaceCollection() as $webspace) {
  119.             $settings $this->settingsManager->loadStringByWildcard($webspace->getKey(), 'snippets-*');
  120.             if (!\in_array($uuid$settings)) {
  121.                 continue;
  122.             }
  123.             $index \array_search($uuid$settings);
  124.             return \substr($index9);
  125.         }
  126.         return null;
  127.     }
  128.     public function loadWebspaces($uuid)
  129.     {
  130.         $webspaces = [];
  131.         foreach ($this->webspaceManager->getWebspaceCollection() as $webspace) {
  132.             $webspaceKey $webspace->getKey();
  133.             $settings $this->settingsManager->loadStringByWildcard($webspaceKey'snippets-*');
  134.             if (!\in_array($uuid$settings)) {
  135.                 continue;
  136.             }
  137.             $webspaces[] = $webspace;
  138.         }
  139.         return $webspaces;
  140.     }
  141.     public function loadIdentifier($webspaceKey$type)
  142.     {
  143.         /** @var array{key: string, template: string, title: array<string, string>}|null $area */
  144.         $area $this->areas->get($type);
  145.         if (!$area) {
  146.             return null;
  147.         }
  148.         return $this->settingsManager->loadString($webspaceKey'snippets-' $area['key']);
  149.     }
  150.     public function getTypeForArea(string $area): ?string
  151.     {
  152.         /** @var array{key: string, template: string, title: array<string, string>}|null $area */
  153.         $area $this->areas->get($area);
  154.         if (!$area) {
  155.             return null;
  156.         }
  157.         return $area['template'];
  158.     }
  159.     /**
  160.      * Check template.
  161.      *
  162.      * @param SnippetDocument $document
  163.      * @param string $type
  164.      *
  165.      * @return bool
  166.      */
  167.     private function checkTemplate($document$type)
  168.     {
  169.         /** @var array{key: string, template: string, title: array<string, string>}|null $area */
  170.         $area $this->areas->get($type);
  171.         if (!$area) {
  172.             return false;
  173.         }
  174.         return $document->getStructureType() === $area['template'];
  175.     }
  176. }