Completed
Push — ezp-30646 ( a44352...64df2d )
by
unknown
15:12
created

LocationEventSubscriber::onUpdateLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 19
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\Search\Common\EventSubscriber;
8
9
use eZ\Publish\API\Repository\Events\Location\CopySubtreeEvent;
10
use eZ\Publish\API\Repository\Events\Location\CreateLocationEvent;
11
use eZ\Publish\API\Repository\Events\Location\DeleteLocationEvent;
12
use eZ\Publish\API\Repository\Events\Location\HideLocationEvent;
13
use eZ\Publish\API\Repository\Events\Location\MoveSubtreeEvent;
14
use eZ\Publish\API\Repository\Events\Location\SwapLocationEvent;
15
use eZ\Publish\API\Repository\Events\Location\UnhideLocationEvent;
16
use eZ\Publish\API\Repository\Events\Location\UpdateLocationEvent;
17
use eZ\Publish\API\Repository\Values\Content\Location;
18
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19
20
class LocationEventSubscriber extends AbstractSearchEventSubscriber implements EventSubscriberInterface
21
{
22
    public static function getSubscribedEvents(): array
23
    {
24
        return [
25
            CopySubtreeEvent::class => 'onCopySubtree',
26
            CreateLocationEvent::class => 'onCreateLocation',
27
            DeleteLocationEvent::class => 'onDeleteLocation',
28
            HideLocationEvent::class => 'onHideLocation',
29
            MoveSubtreeEvent::class => 'onMoveSubtree',
30
            SwapLocationEvent::class => 'onSwapLocation',
31
            UnhideLocationEvent::class => 'onUnhideLocation',
32
            UpdateLocationEvent::class => 'onUpdateLocation',
33
        ];
34
    }
35
36
    public function onCopySubtree(CopySubtreeEvent $event)
37
    {
38
        $this->indexSubtree($event->getLocation()->id);
39
    }
40
41 View Code Duplication
    public function onCreateLocation(CreateLocationEvent $event)
42
    {
43
        $contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo(
44
            $event->getContentInfo()->id
45
        );
46
47
        $this->searchHandler->indexContent(
48
            $this->persistenceHandler->contentHandler()->load(
49
                $contentInfo->id,
50
                $contentInfo->currentVersionNo
51
            )
52
        );
53
    }
54
55
    public function onDeleteLocation(DeleteLocationEvent $event)
56
    {
57
        $this->searchHandler->deleteLocation(
58
            $event->getLocation()->id,
59
            $event->getLocation()->contentId
60
        );
61
    }
62
63
    public function onHideLocation(HideLocationEvent $event)
64
    {
65
        $this->indexSubtree($event->getHiddenLocation()->id);
66
    }
67
68
    public function onMoveSubtree(MoveSubtreeEvent $event)
69
    {
70
        $this->indexSubtree($event->getLocation()->id);
71
    }
72
73
    public function onSwapLocation(SwapLocationEvent $event)
74
    {
75
        $locations = [
76
            $event->getLocation1(),
77
            $event->getLocation2(),
78
        ];
79
80
        array_walk($locations, function (Location $location) {
81
            $contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($location->contentId);
82
83
            $this->searchHandler->indexContent(
84
                $this->persistenceHandler->contentHandler()->load(
85
                    $location->contentId,
86
                    $contentInfo->currentVersionNo
87
                )
88
            );
89
90
            $this->searchHandler->indexLocation(
91
                $this->persistenceHandler->locationHandler()->load($location->id)
92
            );
93
        });
94
    }
95
96
    public function onUnhideLocation(UnhideLocationEvent $event)
97
    {
98
        $this->indexSubtree($event->getRevealedLocation()->id);
99
    }
100
101 View Code Duplication
    public function onUpdateLocation(UpdateLocationEvent $event)
102
    {
103
        $contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo(
104
            $event->getLocation()->contentId
105
        );
106
107
        $this->searchHandler->indexContent(
108
            $this->persistenceHandler->contentHandler()->load(
109
                $event->getLocation()->contentId,
110
                $contentInfo->currentVersionNo
111
            )
112
        );
113
114
        $this->searchHandler->indexLocation(
115
            $this->persistenceHandler->locationHandler()->load(
116
                $event->getLocation()->id
117
            )
118
        );
119
    }
120
}
121