Completed
Push — ezp_31113_for_master ( c9e502 )
by
unknown
14:49
created

onAssignSectionToSubtree()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
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\Events\Section\AssignSectionToSubtreeEvent;
18
use eZ\Publish\API\Repository\Values\Content\Location;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
21
class LocationEventSubscriber extends AbstractSearchEventSubscriber implements EventSubscriberInterface
22
{
23
    public static function getSubscribedEvents(): array
24
    {
25
        return [
26
            AssignSectionToSubtreeEvent::class => 'onAssignSectionToSubtree',
27
            CopySubtreeEvent::class => 'onCopySubtree',
28
            CreateLocationEvent::class => 'onCreateLocation',
29
            DeleteLocationEvent::class => 'onDeleteLocation',
30
            HideLocationEvent::class => 'onHideLocation',
31
            MoveSubtreeEvent::class => 'onMoveSubtree',
32
            SwapLocationEvent::class => 'onSwapLocation',
33
            UnhideLocationEvent::class => 'onUnhideLocation',
34
            UpdateLocationEvent::class => 'onUpdateLocation',
35
        ];
36
    }
37
38
    public function onCopySubtree(CopySubtreeEvent $event)
39
    {
40
        $this->indexSubtree($event->getLocation()->id);
41
    }
42
43 View Code Duplication
    public function onCreateLocation(CreateLocationEvent $event)
44
    {
45
        $contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo(
46
            $event->getContentInfo()->id
47
        );
48
49
        $this->searchHandler->indexContent(
50
            $this->persistenceHandler->contentHandler()->load(
51
                $contentInfo->id,
52
                $contentInfo->currentVersionNo
53
            )
54
        );
55
    }
56
57
    public function onDeleteLocation(DeleteLocationEvent $event)
58
    {
59
        $this->searchHandler->deleteLocation(
60
            $event->getLocation()->id,
61
            $event->getLocation()->contentId
62
        );
63
    }
64
65
    public function onHideLocation(HideLocationEvent $event)
66
    {
67
        $this->indexSubtree($event->getHiddenLocation()->id);
68
    }
69
70
    public function onMoveSubtree(MoveSubtreeEvent $event)
71
    {
72
        $this->indexSubtree($event->getLocation()->id);
73
    }
74
75
    public function onSwapLocation(SwapLocationEvent $event)
76
    {
77
        $locations = [
78
            $event->getLocation1(),
79
            $event->getLocation2(),
80
        ];
81
82
        array_walk($locations, function (Location $location) {
83
            $contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($location->contentId);
84
85
            $this->searchHandler->indexContent(
86
                $this->persistenceHandler->contentHandler()->load(
87
                    $location->contentId,
88
                    $contentInfo->currentVersionNo
89
                )
90
            );
91
92
            $this->searchHandler->indexLocation(
93
                $this->persistenceHandler->locationHandler()->load($location->id)
94
            );
95
        });
96
    }
97
98
    public function onUnhideLocation(UnhideLocationEvent $event)
99
    {
100
        $this->indexSubtree($event->getRevealedLocation()->id);
101
    }
102
103 View Code Duplication
    public function onUpdateLocation(UpdateLocationEvent $event)
104
    {
105
        $contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo(
106
            $event->getLocation()->contentId
107
        );
108
109
        $this->searchHandler->indexContent(
110
            $this->persistenceHandler->contentHandler()->load(
111
                $event->getLocation()->contentId,
112
                $contentInfo->currentVersionNo
113
            )
114
        );
115
116
        $this->searchHandler->indexLocation(
117
            $this->persistenceHandler->locationHandler()->load(
118
                $event->getLocation()->id
119
            )
120
        );
121
    }
122
123
    public function onAssignSectionToSubtree(AssignSectionToSubtreeEvent $event): void
124
    {
125
        $this->indexSubtree($event->getLocation()->id);
126
    }
127
}
128