]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Event/Subscriber/ChangesSubscriber.php
Save changes
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Event / Subscriber / ChangesSubscriber.php
diff --git a/src/Wallabag/CoreBundle/Event/Subscriber/ChangesSubscriber.php b/src/Wallabag/CoreBundle/Event/Subscriber/ChangesSubscriber.php
new file mode 100644 (file)
index 0000000..1ba18a4
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+
+namespace Wallabag\CoreBundle\Event\Subscriber;
+
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+use Doctrine\ORM\EntityManager;
+use Psr\Log\LoggerInterface;
+use Wallabag\CoreBundle\Entity\Change;
+use Wallabag\CoreBundle\Event\EntryTaggedEvent;
+use Wallabag\CoreBundle\Event\EntryUpdatedEvent;
+
+class ChangesSubscriber implements EventSubscriberInterface
+{
+    /** @var LoggerInterface $logger */
+    private $logger;
+
+    /** @var EntityManager $em */
+    private $em;
+
+    public function __construct(EntityManager $em, LoggerInterface $logger)
+    {
+        $this->logger = $logger;
+        $this->em = $em;
+    }
+
+    public static function getSubscribedEvents()
+    {
+        return [
+            EntryUpdatedEvent::NAME => 'onEntryUpdated',
+            EntryTaggedEvent::NAME => 'onEntryTagged',
+        ];
+    }
+
+    /**
+     * @param EntryUpdatedEvent $event
+     */
+    public function onEntryUpdated(EntryUpdatedEvent $event)
+    {
+        $change = new Change(Change::MODIFIED_TYPE, $event->getEntry());
+
+        $this->em->persist($change);
+        $this->em->flush();
+
+        $this->logger->debug('saved updated entry '.$event->getEntry()->getId().' event ');
+    }
+
+    /**
+     * @param EntryTaggedEvent $event
+     */
+    public function onEntryTagged(EntryTaggedEvent $event)
+    {
+        $change = new Change(Change::CHANGED_TAG_TYPE, $event->getEntry());
+
+        $this->em->persist($change);
+        $this->em->flush();
+
+        $this->logger->debug('saved (un)tagged entry '.$event->getEntry()->getId().' event ');
+    }
+}