diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Event/Subscriber/ChangesSubscriber.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Event/Subscriber/ChangesSubscriber.php | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Event/Subscriber/ChangesSubscriber.php b/src/Wallabag/CoreBundle/Event/Subscriber/ChangesSubscriber.php new file mode 100644 index 00000000..1ba18a4f --- /dev/null +++ b/src/Wallabag/CoreBundle/Event/Subscriber/ChangesSubscriber.php | |||
@@ -0,0 +1,59 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Event\Subscriber; | ||
4 | |||
5 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
6 | use Doctrine\ORM\EntityManager; | ||
7 | use Psr\Log\LoggerInterface; | ||
8 | use Wallabag\CoreBundle\Entity\Change; | ||
9 | use Wallabag\CoreBundle\Event\EntryTaggedEvent; | ||
10 | use Wallabag\CoreBundle\Event\EntryUpdatedEvent; | ||
11 | |||
12 | class ChangesSubscriber implements EventSubscriberInterface | ||
13 | { | ||
14 | /** @var LoggerInterface $logger */ | ||
15 | private $logger; | ||
16 | |||
17 | /** @var EntityManager $em */ | ||
18 | private $em; | ||
19 | |||
20 | public function __construct(EntityManager $em, LoggerInterface $logger) | ||
21 | { | ||
22 | $this->logger = $logger; | ||
23 | $this->em = $em; | ||
24 | } | ||
25 | |||
26 | public static function getSubscribedEvents() | ||
27 | { | ||
28 | return [ | ||
29 | EntryUpdatedEvent::NAME => 'onEntryUpdated', | ||
30 | EntryTaggedEvent::NAME => 'onEntryTagged', | ||
31 | ]; | ||
32 | } | ||
33 | |||
34 | /** | ||
35 | * @param EntryUpdatedEvent $event | ||
36 | */ | ||
37 | public function onEntryUpdated(EntryUpdatedEvent $event) | ||
38 | { | ||
39 | $change = new Change(Change::MODIFIED_TYPE, $event->getEntry()); | ||
40 | |||
41 | $this->em->persist($change); | ||
42 | $this->em->flush(); | ||
43 | |||
44 | $this->logger->debug('saved updated entry '.$event->getEntry()->getId().' event '); | ||
45 | } | ||
46 | |||
47 | /** | ||
48 | * @param EntryTaggedEvent $event | ||
49 | */ | ||
50 | public function onEntryTagged(EntryTaggedEvent $event) | ||
51 | { | ||
52 | $change = new Change(Change::CHANGED_TAG_TYPE, $event->getEntry()); | ||
53 | |||
54 | $this->em->persist($change); | ||
55 | $this->em->flush(); | ||
56 | |||
57 | $this->logger->debug('saved (un)tagged entry '.$event->getEntry()->getId().' event '); | ||
58 | } | ||
59 | } | ||