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

LocationEventSubscriber   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 12.87 %

Coupling/Cohesion

Components 2
Dependencies 16

Importance

Changes 0
Metric Value
dl 13
loc 101
rs 10
c 0
b 0
f 0
wmc 9
lcom 2
cbo 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 13 1
A onCopySubtree() 0 4 1
A onCreateLocation() 13 13 1
A onDeleteLocation() 0 7 1
A onHideLocation() 0 4 1
A onMoveSubtree() 0 4 1
A onSwapLocation() 0 22 1
A onUnhideLocation() 0 4 1
A onUpdateLocation() 0 19 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}