Completed
Pull Request — 5.2 (#2395)
by
unknown
12:07
created

NodeIndexUpdateEventListener::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Kunstmaan\NodeSearchBundle\EventListener;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\Event\LifecycleEventArgs;
7
use Kunstmaan\NodeBundle\Entity\Node;
8
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
9
use Kunstmaan\NodeBundle\Entity\StructureNode;
10
use Kunstmaan\NodeBundle\Event\NodeEvent;
11
use Kunstmaan\NodeSearchBundle\Configuration\NodePagesConfiguration;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
/**
15
 * EventListener which will be triggered when a Node has been updated in order to update its related documents
16
 * in the index
17
 */
18
class NodeIndexUpdateEventListener implements NodeIndexUpdateEventListenerInterface
19
{
20
    /** @var ContainerInterface */
21
    private $container;
22
23
    /** @var EntityManagerInterface */
24
    private $em;
25
26
    /** @var NodePagesConfiguration */
27
    private $nodePagesConfiguration;
28
29
    /** @var array */
30
    private $entityChangeSet;
31
32
    public function __construct(/** NodePagesConfiguration */
33
        $nodePagesConfiguration, /** EntityManagerInterface */
34
        $em = null)
35
    {
36
        if (null !== $em) {
37
            $this->em = $em;
38
        }
39
40
        if ($nodePagesConfiguration instanceof ContainerInterface) {
41
            @trigger_error(sprintf('Passing the container as the first argument of "%s" is deprecated in KunstmaanNodeSearchBundle 5.2 and will be removed in KunstmaanNodeSearchBundle 6.0. Inject the "%s" service instead.', __CLASS__, 'kunstmaan_node_search.search_configuration.node'), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
42
43
            $this->container = $nodePagesConfiguration;
44
            $this->nodePagesConfiguration = $this->container->get('kunstmaan_node_search.search_configuration.node');
45
46
            return;
47
        }
48
49
        $this->nodePagesConfiguration = $nodePagesConfiguration;
50
    }
51
52
    /**
53
     * @param LifecycleEventArgs $args
54
     */
55
    public function preUpdate(LifecycleEventArgs $args)
56
    {
57
        if ($args->getObject() instanceof NodeTranslation) {
58
            // unfortunately we have to keep a state to see what has changed
59
            $this->entityChangeSet = [
60
                'nodeTranslationId' => $args->getObject()->getId(),
61
                'changeSet' => $args->getEntityManager()->getUnitOfWork()->getEntityChangeSet($args->getObject()),
62
            ];
63
        }
64
    }
65
66
    /**
67
     * @param NodeEvent $event
68
     */
69
    public function onPostPublish(NodeEvent $event)
70
    {
71
        $this->index($event);
72
    }
73
74
    /**
75
     * @param NodeEvent $event
76
     */
77
    public function onPostPersist(NodeEvent $event)
78
    {
79
        $reIndexChildren = (
80
            !is_null($this->entityChangeSet)
81
            && $this->entityChangeSet['nodeTranslationId'] == $event->getNodeTranslation()->getId()
82
            && isset($this->entityChangeSet['changeSet']['url'])
83
        );
84
        $this->index($event, $reIndexChildren);
85
    }
86
87
    /**
88
     * @param NodeEvent $event
89
     * @param bool      $reIndexChildren
90
     */
91
    private function index(NodeEvent $event, $reIndexChildren = false)
92
    {
93
        $nodeTranslation = $event->getNodeTranslation();
94
95
        if ($this->hasOfflineParents($nodeTranslation)) {
96
            return;
97
        }
98
99
        $this->nodePagesConfiguration->indexNodeTranslation($nodeTranslation, true);
100
101
        if ($reIndexChildren) {
102
            $this->nodePagesConfiguration->indexChildren($event->getNode(), $nodeTranslation->getLang());
103
        }
104
    }
105
106
    /**
107
     * @param NodeEvent $event
108
     */
109
    public function onPostDelete(NodeEvent $event)
110
    {
111
        $this->delete($event);
112
    }
113
114
    /**
115
     * @param NodeEvent $event
116
     */
117
    public function onPostUnPublish(NodeEvent $event)
118
    {
119
        $this->delete($event);
120
    }
121
122
    /**
123
     * @param NodeEvent $event
124
     */
125
    public function delete(NodeEvent $event)
126
    {
127
        $this->nodePagesConfiguration->deleteNodeTranslation($event->getNodeTranslation());
128
    }
129
130
    /**
131
     * @param $nodeTranslation
132
     *
133
     * @return bool
134
     */
135
    private function hasOfflineParents(NodeTranslation $nodeTranslation)
136
    {
137
        $lang = $nodeTranslation->getLang();
138
        $node = $nodeTranslation->getNode();
139
        if (null !== $this->em) {
140
            $em = $this->em;
141
        } elseif (null !== $this->container) {
142
            $em = $this->container->get('doctrine.orm.entity_manager.abstract');
143
        } else {
144
            /** Revert to bugged behaviour to avoid BC breaking */
145
            return false;
146
        }
147
148
        foreach ($node->getParents() as $parent) {
149
            $parentNodeTranslation = $parent->getNodeTranslation($lang, true);
150
            if (null === $parentNodeTranslation) {
151
                continue;
152
            }
153
            $parentRef = $parentNodeTranslation->getRef($em);
154
            // Continue looping unless we find an offline page that is not a StructureNode
155
            if ($parentRef instanceof StructureNode || $parentNodeTranslation->isOnline()) {
156
                continue;
157
            }
158
159
            return true;
160
        }
161
162
        return false;
163
    }
164
}
165