partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

RootCategoryListener.php (2417B)


      1 <?php
      2 
      3 namespace PartKeepr\CategoryBundle\EventListener;
      4 
      5 use Doctrine\ORM\Event\OnFlushEventArgs;
      6 use PartKeepr\CategoryBundle\Entity\AbstractCategory;
      7 use PartKeepr\CategoryBundle\Exception\OnlySingleRootNodeAllowedException;
      8 use PartKeepr\CategoryBundle\Exception\RootMayNotBeDeletedException;
      9 use PartKeepr\CategoryBundle\Exception\RootNodeNotFoundException;
     10 use Symfony\Component\DependencyInjection\Container;
     11 use Symfony\Component\DependencyInjection\ContainerAware;
     12 
     13 class RootCategoryListener extends ContainerAware
     14 {
     15     /**
     16      * @var string
     17      */
     18     private $service;
     19 
     20     public function __construct(Container $container, $service)
     21     {
     22         $this->setContainer($container);
     23         $this->service = $service;
     24     }
     25 
     26     /**
     27      * Checks that only one root category exists.
     28      *
     29      * @param OnFlushEventArgs $eventArgs The event arguments as given by Doctrine
     30      */
     31     public function onFlush(OnFlushEventArgs $eventArgs)
     32     {
     33         $entityManager = $eventArgs->getEntityManager();
     34         $uow = $entityManager->getUnitOfWork();
     35 
     36         foreach ($uow->getScheduledEntityInsertions() as $insertion) {
     37             if (is_a($insertion, $this->container->get($this->service)->getEntityClass())) {
     38                 $this->checkForRoot($insertion);
     39             }
     40         }
     41 
     42         foreach ($uow->getScheduledEntityUpdates() as $updated) {
     43             if (is_a($updated, $this->container->get($this->service)->getEntityClass())) {
     44                 $this->checkForRoot($updated);
     45             }
     46         }
     47 
     48         foreach ($uow->getScheduledEntityDeletions() as $deletion) {
     49             if (is_a($deletion, $this->container->get($this->service)->getEntityClass())) {
     50                 $this->ensureRootStays($deletion);
     51             }
     52         }
     53     }
     54 
     55     protected function ensureRootstays(AbstractCategory $category)
     56     {
     57         if ($category->getParent() === null) {
     58             throw new RootMayNotBeDeletedException();
     59         }
     60     }
     61 
     62     protected function checkForRoot(AbstractCategory $category)
     63     {
     64         if ($category->getParent() === null) {
     65             try {
     66                 $rootNode = $this->container->get($this->service)->getRootNode();
     67 
     68                 if ($rootNode->getId() != $category->getId()) {
     69                     throw new OnlySingleRootNodeAllowedException();
     70                 }
     71             } catch (RootNodeNotFoundException $e) {
     72             }
     73         }
     74     }
     75 }