Completed
Push — ezp-30646 ( 6400c7...ee4911 )
by
unknown
21:35
created

LocationEventSubscriber::onSwapLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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