|
@@ -0,0 +1,82 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace HostBundle\EventListener;
|
|
|
+
|
|
|
+use Doctrine\Common\EventSubscriber;
|
|
|
+use Doctrine\ORM\Event\LifecycleEventArgs;
|
|
|
+use HostBundle\Entity\Host;
|
|
|
+use KeaBundle\Services\LeaseService;
|
|
|
+
|
|
|
+class HostStatusChangeSubscriber implements EventSubscriber
|
|
|
+{
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var ContainerInterface
|
|
|
+ */
|
|
|
+ private $serviceContainer;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param ContainerInterface $serviceContainer
|
|
|
+ */
|
|
|
+ public function __construct($serviceContainer)
|
|
|
+ {
|
|
|
+ $this->serviceContainer = $serviceContainer;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getSubscribedEvents()
|
|
|
+ {
|
|
|
+ return array(
|
|
|
+ 'postUpdate',
|
|
|
+ 'preRemove',
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param LifecycleEventArgs $args
|
|
|
+ */
|
|
|
+ public function postUpdate(LifecycleEventArgs $args)
|
|
|
+ {
|
|
|
+ $this->execute($args);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param LifecycleEventArgs $args
|
|
|
+ */
|
|
|
+ public function preRemove(LifecycleEventArgs $args)
|
|
|
+ {
|
|
|
+ $this->execute($args);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param LifecycleEventArgs $args
|
|
|
+ * @param boolean $remove
|
|
|
+ */
|
|
|
+ public function execute(LifecycleEventArgs $args)
|
|
|
+ {
|
|
|
+ $flashbag = $this->serviceContainer->get('session')->getFlashBag();
|
|
|
+ $translator = $this->serviceContainer->get('translator');
|
|
|
+ try {
|
|
|
+ // checking if the entity is a Host and the status has changed
|
|
|
+ $em = $args->getEntityManager();
|
|
|
+ $uow = $em->getUnitOfWork();
|
|
|
+ $uow->computeChangeSets();
|
|
|
+ $entity = $args->getEntity();
|
|
|
+ $changeset = $uow->getEntityChangeSet($entity);
|
|
|
+ if ($entity instanceof Host && isset($changeset['state'])) {
|
|
|
+ $leaseService = $this->serviceContainer->get('kea.lease.service');
|
|
|
+ // invalidating the host lease
|
|
|
+ $lease4 = $leaseService->invalidate($entity->getMac());
|
|
|
+ $flashbag->add("success", $translator->trans('host.lease.invalidate.ok', ['%hwaddr%' => $entity->getMac()], 'KeaBundle'));
|
|
|
+ }
|
|
|
+ } catch (\Exception $ex) {
|
|
|
+ $flashbag->add("error", $ex->getMessage());
|
|
|
+ } catch (\Throwable $ex) {
|
|
|
+ $flashbag->add("error", $ex->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|