partkeepr

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

commit b58ffa2e19f843520f96e2e38aa77b8cb67c0b50
parent 656e122dc4db183369c4e0afd704bf99633f3d57
Author: Felicitus <felicitus@felicitus.org>
Date:   Sat, 10 Oct 2015 18:46:33 +0200

Ensure that the root node may never be deleted

Diffstat:
Msrc/PartKeepr/CategoryBundle/EventListener/RootCategoryListener.php | 21++++++++++++++++++---
Msrc/PartKeepr/CategoryBundle/Exception/OnlySingleRootNodeAllowedException.php | 3+--
Asrc/PartKeepr/CategoryBundle/Exception/RootMayNotBeDeletedException.php | 10++++++++++
3 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/src/PartKeepr/CategoryBundle/EventListener/RootCategoryListener.php b/src/PartKeepr/CategoryBundle/EventListener/RootCategoryListener.php @@ -4,6 +4,7 @@ namespace PartKeepr\CategoryBundle\EventListener; use Doctrine\ORM\Event\OnFlushEventArgs; use PartKeepr\CategoryBundle\Entity\AbstractCategory; use PartKeepr\CategoryBundle\Exception\OnlySingleRootNodeAllowedException; +use PartKeepr\CategoryBundle\Exception\RootMayNotBeDeletedException; use PartKeepr\CategoryBundle\Exception\RootNodeNotFoundException; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerAware; @@ -31,10 +32,10 @@ class RootCategoryListener extends ContainerAware $entityManager = $eventArgs->getEntityManager(); $uow = $entityManager->getUnitOfWork(); - foreach ($uow->getScheduledEntityInsertions() as $updated) { + foreach ($uow->getScheduledEntityInsertions() as $insertion) { - if (is_a($updated, $this->container->get($this->service)->getEntityClass())) { - $this->checkForRoot($updated); + if (is_a($insertion, $this->container->get($this->service)->getEntityClass())) { + $this->checkForRoot($insertion); } } @@ -43,6 +44,20 @@ class RootCategoryListener extends ContainerAware $this->checkForRoot($updated); } } + + foreach ($uow->getScheduledEntityDeletions() as $deletion) { + if (is_a($deletion, $this->container->get($this->service)->getEntityClass())) { + $this->ensureRootStays($deletion); + } + } + + } + + protected function ensureRootstays(AbstractCategory $category) + { + if ($category->getParent() === null) { + throw new RootMayNotBeDeletedException(); + } } protected function checkForRoot(AbstractCategory $category) diff --git a/src/PartKeepr/CategoryBundle/Exception/OnlySingleRootNodeAllowedException.php b/src/PartKeepr/CategoryBundle/Exception/OnlySingleRootNodeAllowedException.php @@ -8,7 +8,6 @@ class OnlySingleRootNodeAllowedException extends \Exception { public function __construct($message = "", $code = 0, \Exception $previous = null) { - $message = "There may be only one root node"; - parent::__construct($message, $code, $previous); + parent::__construct("There may be only one root node", $code, $previous); } } diff --git a/src/PartKeepr/CategoryBundle/Exception/RootMayNotBeDeletedException.php b/src/PartKeepr/CategoryBundle/Exception/RootMayNotBeDeletedException.php @@ -0,0 +1,10 @@ +<?php +namespace PartKeepr\CategoryBundle\Exception; + +class RootMayNotBeDeletedException extends \Exception +{ + public function __construct($message = "", $code = 0, \Exception $previous = null) + { + parent::__construct("The root node may not be deleted", $code, $previous); + } +}