]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Event/Subscriber/SQLiteCascadeDeleteSubscriber.php
Merge pull request #3596 from glego/add-secure-email
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Event / Subscriber / SQLiteCascadeDeleteSubscriber.php
CommitLineData
191564b7 1<?php
8c61fd12 2
535bfcbe 3namespace Wallabag\CoreBundle\Event\Subscriber;
191564b7 4
f808b016 5use Doctrine\Bundle\DoctrineBundle\Registry;
191564b7 6use Doctrine\Common\EventSubscriber;
191564b7
JB
7use Doctrine\ORM\Event\LifecycleEventArgs;
8use Wallabag\CoreBundle\Entity\Entry;
191564b7
JB
9
10/**
11 * SQLite doesn't care about cascading remove, so we need to manually remove associated stuf for an Entry.
12 * Foreign Key Support can be enabled by running `PRAGMA foreign_keys = ON;` at runtime (AT RUNTIME !).
13 * But it needs a compilation flag that not all SQLite instance has ...
14 *
15 * @see https://www.sqlite.org/foreignkeys.html#fk_enable
16 */
17class SQLiteCascadeDeleteSubscriber implements EventSubscriber
18{
19 private $doctrine;
20
21 /**
22 * @param \Doctrine\Bundle\DoctrineBundle\Registry $doctrine
23 */
24 public function __construct(Registry $doctrine)
25 {
26 $this->doctrine = $doctrine;
27 }
28
29 /**
30 * @return array
31 */
32 public function getSubscribedEvents()
33 {
34 return [
35 'preRemove',
36 ];
37 }
38
39 /**
40 * We removed everything related to the upcoming removed entry because SQLite can't handle it on it own.
8c61fd12 41 * We do it in the preRemove, because we can't retrieve tags in the postRemove (because the entry id is gone).
191564b7
JB
42 *
43 * @param LifecycleEventArgs $args
44 */
45 public function preRemove(LifecycleEventArgs $args)
46 {
47 $entity = $args->getEntity();
7ab5eb95 48 if (!$this->doctrine->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform
49 || !$entity instanceof Entry) {
191564b7
JB
50 return;
51 }
52
53 $em = $this->doctrine->getManager();
54
55 if (null !== $entity->getTags()) {
56 foreach ($entity->getTags() as $tag) {
57 $entity->removeTag($tag);
58 }
59 }
60
61 if (null !== $entity->getAnnotations()) {
62 foreach ($entity->getAnnotations() as $annotation) {
63 $em->remove($annotation);
64 }
65 }
66
67 $em->flush();
68 }
69}