Completed
Push — master ( 770316...74fc07 )
by Jeroen
09:08 queued 02:44
created

EventListener/NodeIndexUpdateEventListener.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\NodeTranslation;
8
use Kunstmaan\NodeBundle\Entity\StructureNode;
9
use Kunstmaan\NodeBundle\Event\NodeEvent;
10
use Kunstmaan\NodeSearchBundle\Configuration\NodePagesConfiguration;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
13
/**
14
 * EventListener which will be triggered when a Node has been updated in order to update its related documents
15
 * in the index
16
 */
17
class NodeIndexUpdateEventListener implements NodeIndexUpdateEventListenerInterface
18
{
19
    /** @var ContainerInterface */
20
    private $container;
21
22
    /** @var EntityManagerInterface */
23
    private $em;
24
25
    /** @var NodePagesConfiguration */
26
    private $nodePagesConfiguration;
27
28
    /** @var array */
29
    private $entityChangeSet;
30
31 5
    public function __construct(/* NodePagesConfiguration */
32
        $nodePagesConfiguration, /* EntityManagerInterface */
33
        $em = null)
34
    {
35 5
        if ($nodePagesConfiguration instanceof ContainerInterface) {
36 1
            @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);
37
38 1
            $this->container = $nodePagesConfiguration;
39 1
            $this->nodePagesConfiguration = $this->container->get('kunstmaan_node_search.search_configuration.node');
40
41 1
            if (null === $em) {
42
                $this->em = $this->container->get('doctrine.orm.default_entity_manager');
43
            }
44
45 1
            return;
46
        }
47
48 4
        if (!($em instanceof EntityManagerInterface)) {
49
            @trigger_error(sprintf('Passing null or something other than an entitymanagerinterface as the second argument of "%s" is deprecated in KunstmaanNodeSearchBundle 5.2 and will be removed in KunstmaanNodeSearchBundle 6.0. Inject the "%s" service instead.', __CLASS__, 'doctrine.orm.default_entity_manager'), 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...
50
        }
51
52 4
        $this->nodePagesConfiguration = $nodePagesConfiguration;
53 4
        $this->em = $em;
54 4
    }
55
56
    /**
57
     * @param LifecycleEventArgs $args
58
     */
59
    public function preUpdate(LifecycleEventArgs $args)
60
    {
61
        if ($args->getObject() instanceof NodeTranslation) {
62
            // unfortunately we have to keep a state to see what has changed
63
            $this->entityChangeSet = [
64
                'nodeTranslationId' => $args->getObject()->getId(),
65
                'changeSet' => $args->getEntityManager()->getUnitOfWork()->getEntityChangeSet($args->getObject()),
66
            ];
67
        }
68
    }
69
70
    /**
71
     * @param NodeEvent $event
72
     */
73
    public function onPostPublish(NodeEvent $event)
74
    {
75
        $this->index($event);
76
    }
77
78
    /**
79
     * @param NodeEvent $event
80
     */
81 4
    public function onPostPersist(NodeEvent $event)
82
    {
83
        $reIndexChildren = (
84 4
            !\is_null($this->entityChangeSet)
85 4
            && $this->entityChangeSet['nodeTranslationId'] == $event->getNodeTranslation()->getId()
86 4
            && isset($this->entityChangeSet['changeSet']['url'])
87
        );
88 4
        $this->index($event, $reIndexChildren);
89 4
    }
90
91
    /**
92
     * @param NodeEvent $event
93
     * @param bool      $reIndexChildren
94
     */
95 4
    private function index(NodeEvent $event, $reIndexChildren = false)
96
    {
97 4
        $nodeTranslation = $event->getNodeTranslation();
98
99 4
        if ($this->hasOfflineParents($nodeTranslation)) {
100 2
            return;
101
        }
102
103 2
        $this->nodePagesConfiguration->indexNodeTranslation($nodeTranslation, true);
104
105 2
        if ($reIndexChildren) {
106
            $this->nodePagesConfiguration->indexChildren($event->getNode(), $nodeTranslation->getLang());
107
        }
108 2
    }
109
110
    /**
111
     * @param NodeEvent $event
112
     */
113
    public function onPostDelete(NodeEvent $event)
114
    {
115
        $this->delete($event);
116
    }
117
118
    /**
119
     * @param NodeEvent $event
120
     */
121
    public function onPostUnPublish(NodeEvent $event)
122
    {
123
        $this->delete($event);
124
    }
125
126
    /**
127
     * @param NodeEvent $event
128
     */
129
    public function delete(NodeEvent $event)
130
    {
131
        $this->nodePagesConfiguration->deleteNodeTranslation($event->getNodeTranslation());
132
    }
133
134
    /**
135
     * @param $nodeTranslation
136
     *
137
     * @return bool
138
     */
139 4
    private function hasOfflineParents(NodeTranslation $nodeTranslation)
140
    {
141 4
        $lang = $nodeTranslation->getLang();
142 4
        $node = $nodeTranslation->getNode();
143 4
        if (null !== $this->em) {
144 4
            $em = $this->em;
145
        } else {
146
            $lang = $nodeTranslation->getLang();
147
            foreach ($nodeTranslation->getNode()->getParents() as $node) {
148
                $nodeNT = $node->getNodeTranslation($lang, true);
149
                if ($nodeNT && !$nodeNT->isOnline()) {
150
                    return true;
151
                }
152
            }
153
154
            return false;
155
        }
156
157 4
        foreach ($node->getParents() as $parent) {
158 4
            $parentNodeTranslation = $parent->getNodeTranslation($lang, true);
159 4
            if (null === $parentNodeTranslation) {
160
                continue;
161
            }
162 4
            $parentRef = $parentNodeTranslation->getRef($em);
163
            // Continue looping unless we find an offline page that is not a StructureNode
164 4
            if ($parentRef instanceof StructureNode || $parentNodeTranslation->isOnline()) {
165 3
                continue;
166
            }
167
168 2
            return true;
169
        }
170
171 2
        return false;
172
    }
173
}
174