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

EventListener/NodeIndexUpdateEventListener.php (1 issue)

Labels
Severity

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\Event\LifecycleEventArgs;
6
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
7
use Kunstmaan\NodeBundle\Entity\StructureNode;
8
use Kunstmaan\NodeBundle\Event\NodeEvent;
9
use Kunstmaan\NodeSearchBundle\Configuration\NodePagesConfiguration;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
/**
13
 * EventListener which will be triggered when a Node has been updated in order to update its related documents
14
 * in the index
15
 */
16
class NodeIndexUpdateEventListener implements NodeIndexUpdateEventListenerInterface
17
{
18
    /** @var ContainerInterface */
19
    private $container;
20
21
    /** @var NodePagesConfiguration */
22
    private $nodePagesConfiguration;
23
24
    /** @var array */
25
    private $entityChangeSet;
26
27
    /**
28
     * @param ContainerInterface $container
0 ignored issues
show
There is no parameter named $container. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
29
     */
30 5
    public function __construct(/* NodePagesConfiguration */ $nodePagesConfiguration)
31
    {
32 5
        if ($nodePagesConfiguration instanceof ContainerInterface) {
33 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);
34
35 1
            $this->container = $nodePagesConfiguration;
36 1
            $this->nodePagesConfiguration = $this->container->get('kunstmaan_node_search.search_configuration.node');
37
38 1
            return;
39
        }
40
41 4
        $this->nodePagesConfiguration = $nodePagesConfiguration;
42 4
    }
43
44
    /**
45
     * @param LifecycleEventArgs $args
46
     */
47
    public function preUpdate(LifecycleEventArgs $args)
48
    {
49
        if ($args->getObject() instanceof NodeTranslation) {
50
            // unfortunately we have to keep a state to see what has changed
51
            $this->entityChangeSet = [
52
                'nodeTranslationId' => $args->getObject()->getId(),
53
                'changeSet' => $args->getEntityManager()->getUnitOfWork()->getEntityChangeSet($args->getObject()),
54
            ];
55
        }
56
    }
57
58
    /**
59
     * @param NodeEvent $event
60
     */
61
    public function onPostPublish(NodeEvent $event)
62
    {
63
        $this->index($event);
64
    }
65
66
    /**
67
     * @param NodeEvent $event
68
     */
69 4
    public function onPostPersist(NodeEvent $event)
70
    {
71
        $reIndexChildren = (
72 4
            !is_null($this->entityChangeSet)
73 4
            && $this->entityChangeSet['nodeTranslationId'] == $event->getNodeTranslation()->getId()
74 4
            && isset($this->entityChangeSet['changeSet']['url'])
75
        );
76 4
        $this->index($event, $reIndexChildren);
77 4
    }
78
79
    /**
80
     * @param NodeEvent $event
81
     * @param bool      $reIndexChildren
82
     */
83 4
    private function index(NodeEvent $event, $reIndexChildren = false)
84
    {
85 4
        $nodeTranslation = $event->getNodeTranslation();
86
87 4
        if ($this->hasOfflineParents($nodeTranslation)) {
88 2
            return;
89
        }
90
91 2
        $this->nodePagesConfiguration->indexNodeTranslation($nodeTranslation, true);
92
93 2
        if ($reIndexChildren) {
94
            $this->nodePagesConfiguration->indexChildren($event->getNode(), $nodeTranslation->getLang());
95
        }
96 2
    }
97
98
    /**
99
     * @param NodeEvent $event
100
     */
101
    public function onPostDelete(NodeEvent $event)
102
    {
103
        $this->delete($event);
104
    }
105
106
    /**
107
     * @param NodeEvent $event
108
     */
109
    public function onPostUnPublish(NodeEvent $event)
110
    {
111
        $this->delete($event);
112
    }
113
114
    /**
115
     * @param NodeEvent $event
116
     */
117
    public function delete(NodeEvent $event)
118
    {
119
        $this->nodePagesConfiguration->deleteNodeTranslation($event->getNodeTranslation());
120
    }
121
122
    /**
123
     * @param $nodeTranslation
124
     *
125
     * @return bool
126
     */
127 4
    private function hasOfflineParents(NodeTranslation $nodeTranslation)
128
    {
129 4
        $lang = $nodeTranslation->getLang();
130 4
        foreach ($nodeTranslation->getNode()->getParents() as $node) {
131 4
            $nodeNT = $node->getNodeTranslation($lang, true);
132 4
            if ($nodeNT && !$nodeNT->isOnline() && !$nodeNT instanceof StructureNode) {
133 2
                return true;
134
            }
135
        }
136
137 2
        return false;
138
    }
139
}
140