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

ContentEventSubscriber::onPublishVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace eZ\Publish\Core\Search\Common\EventSubscriber;
4
5
use eZ\Publish\Core\Event\Content\ContentEvents;
6
use eZ\Publish\Core\Event\Content\CopyContentEvent;
7
use eZ\Publish\Core\Event\Content\DeleteContentEvent;
8
use eZ\Publish\Core\Event\Content\DeleteTranslationEvent;
9
use eZ\Publish\Core\Event\Content\HideContentEvent;
10
use eZ\Publish\Core\Event\Content\PublishVersionEvent;
11
use eZ\Publish\Core\Event\Content\RevealContentEvent;
12
use eZ\Publish\Core\Event\Content\UpdateContentMetadataEvent;
13
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
16
class ContentEventSubscriber extends AbstractSearchEventSubscriber implements EventSubscriberInterface
17
{
18
    public static function getSubscribedEvents(): array
19
    {
20
        return [
21
            ContentEvents::COPY_CONTENT => 'onCopyContent',
22
            ContentEvents::DELETE_CONTENT => 'onDeleteContent',
23
            ContentEvents::DELETE_TRANSLATION => 'onDeleteTranslation',
24
            ContentEvents::HIDE_CONTENT => 'onHideContent',
25
            ContentEvents::PUBLISH_VERSION => 'onPublishVersion',
26
            ContentEvents::REVEAL_CONTENT => 'onRevealContent',
27
            ContentEvents::UPDATE_CONTENT_METADATA => 'onUpdateContentMetadata',
28
        ];
29
    }
30
31
    public function onCopyContent(CopyContentEvent $event)
32
    {
33
        $this->searchHandler->indexContent(
34
            $this->persistenceHandler->contentHandler()->load(
35
                $event->getContent()->getVersionInfo()->getContentInfo()->id,
36
                $event->getContent()->getVersionInfo()->versionNo
37
            )
38
        );
39
40
        $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent(
41
            $event->getContent()->getVersionInfo()->getContentInfo()->id
42
        );
43
44
        foreach ($locations as $location) {
45
            $this->searchHandler->indexLocation($location);
46
        }
47
    }
48
49
    public function onDeleteContent(DeleteContentEvent $event)
50
    {
51
        $this->searchHandler->deleteContent($event->getContentInfo()->id);
52
53
        /** @var \eZ\Publish\API\Repository\Values\Content\Location $location */
54
        foreach ($event->getLocations() as $location) {
55
            $this->searchHandler->deleteLocation($location->id, $event->getContentInfo()->id);
56
        }
57
    }
58
59 View Code Duplication
    public function onDeleteTranslation(DeleteTranslationEvent $event)
60
    {
61
        $contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo(
62
            $event->getContentInfo()->id
63
        );
64
65
        if (!$contentInfo->status === ContentInfo::STATUS_PUBLISHED) {
66
            return;
67
        }
68
69
        $this->searchHandler->indexContent(
70
            $this->persistenceHandler->contentHandler()->load(
71
                $contentInfo->id,
72
                $contentInfo->currentVersionNo
73
            )
74
        );
75
76
        $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent(
77
            $contentInfo->id
78
        );
79
80
        foreach ($locations as $location) {
81
            $this->searchHandler->indexLocation($location);
82
        }
83
    }
84
85 View Code Duplication
    public function onHideContent(HideContentEvent $event)
86
    {
87
        $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($event->getContentInfo()->id);
88
        foreach ($locations as $location) {
89
            $this->indexSubtree($location->id);
90
        }
91
    }
92
93
    public function onPublishVersion(PublishVersionEvent $event)
94
    {
95
        $this->searchHandler->indexContent(
96
            $this->persistenceHandler->contentHandler()->load($event->getContent()->id, $event->getContent()->getVersionInfo()->versionNo)
97
        );
98
99
        $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($event->getContent()->id);
100
        foreach ($locations as $location) {
101
            $this->searchHandler->indexLocation($location);
102
        }
103
    }
104
105 View Code Duplication
    public function onRevealContent(RevealContentEvent $event)
106
    {
107
        $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($event->getContentInfo()->id);
108
        foreach ($locations as $location) {
109
            $this->indexSubtree($location->id);
110
        }
111
    }
112
113
    public function onUpdateContentMetadata(UpdateContentMetadataEvent $event)
114
    {
115
        $contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($event->getContent()->id);
116
        if (!$contentInfo->status === ContentInfo::STATUS_PUBLISHED) {
117
            return;
118
        }
119
        $this->searchHandler->indexContent(
120
            $this->persistenceHandler->contentHandler()->load($contentInfo->id, $contentInfo->currentVersionNo)
121
        );
122
        $this->searchHandler->indexLocation($this->persistenceHandler->locationHandler()->load($contentInfo->mainLocationId));
123
    }
124
}