Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
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
    public function preUpdate(LifecycleEventArgs $args)
57
    {
58
        if ($args->getObject() instanceof NodeTranslation) {
59
            // unfortunately we have to keep a state to see what has changed
60
            $this->entityChangeSet = [
61
                'nodeTranslationId' => $args->getObject()->getId(),
62
                'changeSet' => $args->getEntityManager()->getUnitOfWork()->getEntityChangeSet($args->getObject()),
63
            ];
64
        }
65
    }
66
67
    public function onPostPublish(NodeEvent $event)
68
    {
69
        $this->index($event);
70
    }
71
72 4
    public function onPostPersist(NodeEvent $event)
73
    {
74
        $reIndexChildren = (
75 4
            !\is_null($this->entityChangeSet)
76 4
            && $this->entityChangeSet['nodeTranslationId'] == $event->getNodeTranslation()->getId()
77 4
            && isset($this->entityChangeSet['changeSet']['url'])
78
        );
79 4
        $this->index($event, $reIndexChildren);
80 4
    }
81
82
    /**
83
     * @param bool $reIndexChildren
84
     */
85 4
    private function index(NodeEvent $event, $reIndexChildren = false)
86
    {
87 4
        $nodeTranslation = $event->getNodeTranslation();
88
89 4
        if ($this->hasOfflineParents($nodeTranslation)) {
90 2
            return;
91
        }
92
93 2
        $this->nodePagesConfiguration->indexNodeTranslation($nodeTranslation, true);
94
95 2
        if ($reIndexChildren) {
96
            $this->nodePagesConfiguration->indexChildren($event->getNode(), $nodeTranslation->getLang());
97
        }
98 2
    }
99
100
    public function onPostDelete(NodeEvent $event)
101
    {
102
        $this->delete($event);
103
    }
104
105
    public function onPostUnPublish(NodeEvent $event)
106
    {
107
        $this->delete($event);
108
    }
109
110
    public function delete(NodeEvent $event)
111
    {
112
        $this->nodePagesConfiguration->deleteNodeTranslation($event->getNodeTranslation());
113
    }
114
115
    /**
116
     * @param $nodeTranslation
117
     *
118
     * @return bool
119
     */
120 4
    private function hasOfflineParents(NodeTranslation $nodeTranslation)
121
    {
122 4
        $lang = $nodeTranslation->getLang();
123 4
        $node = $nodeTranslation->getNode();
124 4
        if (null !== $this->em) {
125 4
            $em = $this->em;
126
        } else {
127
            $lang = $nodeTranslation->getLang();
128
            foreach ($nodeTranslation->getNode()->getParents() as $node) {
129
                $nodeNT = $node->getNodeTranslation($lang, true);
130
                if ($nodeNT && !$nodeNT->isOnline()) {
131
                    return true;
132
                }
133
            }
134
135
            return false;
136
        }
137
138 4
        foreach ($node->getParents() as $parent) {
139 4
            $parentNodeTranslation = $parent->getNodeTranslation($lang, true);
140 4
            if (null === $parentNodeTranslation) {
141
                continue;
142
            }
143 4
            $parentRef = $parentNodeTranslation->getRef($em);
144
            // Continue looping unless we find an offline page that is not a StructureNode
145 4
            if ($parentRef instanceof StructureNode || $parentNodeTranslation->isOnline()) {
146 3
                continue;
147
            }
148
149 2
            return true;
150
        }
151
152 2
        return false;
153
    }
154
}
155