Completed
Push — master ( bc3591...499283 )
by Ruud
11:23 queued 11s
created

NodeIndexUpdateEventListener::hasOfflineParents()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 0
cts 0
cp 0
rs 8.0555
c 0
b 0
f 0
cc 9
nc 7
nop 1
crap 90
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 5
    private $entityChangeSet;
31
32 5
    public function __construct(/* NodePagesConfiguration */
33 1
        $nodePagesConfiguration, /* EntityManagerInterface */
34
        $em = null)
35 1
    {
36 1
        if ($nodePagesConfiguration instanceof ContainerInterface) {
37
            @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 Jeroen Thora
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...
38 1
39
            $this->container = $nodePagesConfiguration;
40
            $this->nodePagesConfiguration = $this->container->get('kunstmaan_node_search.search_configuration.node');
41 4
42 4
            if (null === $em) {
43
                $this->em = $this->container->get('doctrine.orm.default_entity_manager');
44
            }
45
46
            return;
47
        }
48
49
        if (!($em instanceof EntityManagerInterface)) {
50
            @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 Kevin Jossart
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...
51
        }
52
53
        $this->nodePagesConfiguration = $nodePagesConfiguration;
54
        $this->em = $em;
55
    }
56
57
    /**
58
     * @param LifecycleEventArgs $args
59
     */
60
    public function preUpdate(LifecycleEventArgs $args)
61
    {
62
        if ($args->getObject() instanceof NodeTranslation) {
63
            // unfortunately we have to keep a state to see what has changed
64
            $this->entityChangeSet = [
65
                'nodeTranslationId' => $args->getObject()->getId(),
66
                'changeSet' => $args->getEntityManager()->getUnitOfWork()->getEntityChangeSet($args->getObject()),
67
            ];
68
        }
69 4
    }
70
71
    /**
72 4
     * @param NodeEvent $event
73 4
     */
74 4
    public function onPostPublish(NodeEvent $event)
75
    {
76 4
        $this->index($event);
77 4
    }
78
79
    /**
80
     * @param NodeEvent $event
81
     */
82
    public function onPostPersist(NodeEvent $event)
83 4
    {
84
        $reIndexChildren = (
85 4
            !is_null($this->entityChangeSet)
86
            && $this->entityChangeSet['nodeTranslationId'] == $event->getNodeTranslation()->getId()
87 4
            && isset($this->entityChangeSet['changeSet']['url'])
88 2
        );
89
        $this->index($event, $reIndexChildren);
90
    }
91 2
92
    /**
93 2
     * @param NodeEvent $event
94
     * @param bool      $reIndexChildren
95
     */
96 2
    private function index(NodeEvent $event, $reIndexChildren = false)
97
    {
98
        $nodeTranslation = $event->getNodeTranslation();
99
100
        if ($this->hasOfflineParents($nodeTranslation)) {
101
            return;
102
        }
103
104
        $this->nodePagesConfiguration->indexNodeTranslation($nodeTranslation, true);
105
106
        if ($reIndexChildren) {
107
            $this->nodePagesConfiguration->indexChildren($event->getNode(), $nodeTranslation->getLang());
108
        }
109
    }
110
111
    /**
112
     * @param NodeEvent $event
113
     */
114
    public function onPostDelete(NodeEvent $event)
115
    {
116
        $this->delete($event);
117
    }
118
119
    /**
120
     * @param NodeEvent $event
121
     */
122
    public function onPostUnPublish(NodeEvent $event)
123
    {
124
        $this->delete($event);
125
    }
126
127 4
    /**
128
     * @param NodeEvent $event
129 4
     */
130 4
    public function delete(NodeEvent $event)
131 4
    {
132 4
        $this->nodePagesConfiguration->deleteNodeTranslation($event->getNodeTranslation());
133 2
    }
134
135
    /**
136
     * @param $nodeTranslation
137 2
     *
138
     * @return bool
139
     */
140
    private function hasOfflineParents(NodeTranslation $nodeTranslation)
141
    {
142
        $lang = $nodeTranslation->getLang();
143
        $node = $nodeTranslation->getNode();
144
        if (null !== $this->em) {
145
            $em = $this->em;
146
        } else {
147
            $lang = $nodeTranslation->getLang();
148
            foreach ($nodeTranslation->getNode()->getParents() as $node) {
149
                $nodeNT = $node->getNodeTranslation($lang, true);
150
                if ($nodeNT && !$nodeNT->isOnline()) {
151
                    return true;
152
                }
153
            }
154
            return false;
155
        }
156
157
        foreach ($node->getParents() as $parent) {
158
            $parentNodeTranslation = $parent->getNodeTranslation($lang, true);
159
            if (null === $parentNodeTranslation) {
160
                continue;
161
            }
162
            $parentRef = $parentNodeTranslation->getRef($em);
0 ignored issues
show
Compatibility introduced by Kevin Jossart
$em of type object<Doctrine\ORM\EntityManagerInterface> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\ORM\EntityManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
163
            // Continue looping unless we find an offline page that is not a StructureNode
164
            if ($parentRef instanceof StructureNode || $parentNodeTranslation->isOnline()) {
165
                continue;
166
            }
167
168
            return true;
169
        }
170
171
        return false;
172
    }
173
}
174