Completed
Push — develop ( 292cbe...631186 )
by Alexander
15:04
created

ArticleSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 1
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Bundle\ArticleBundle\Document\Subscriber;
13
14
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
15
use Sulu\Bundle\ArticleBundle\Document\Index\IndexerInterface;
16
use Sulu\Component\DocumentManager\DocumentManagerInterface;
17
use Sulu\Component\DocumentManager\Event\AbstractMappingEvent;
18
use Sulu\Component\DocumentManager\Event\CopyEvent;
19
use Sulu\Component\DocumentManager\Event\FlushEvent;
20
use Sulu\Component\DocumentManager\Event\RemoveEvent;
21
use Sulu\Component\DocumentManager\Event\UnpublishEvent;
22
use Sulu\Component\DocumentManager\Events;
23
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
24
25
/**
26
 * Indexes article and generate route on persist and removes it from index and routing on delete.
27
 */
28
class ArticleSubscriber implements EventSubscriberInterface
29
{
30
    /**
31
     * @var IndexerInterface
32
     */
33
    private $indexer;
34
35
    /**
36
     * @var IndexerInterface
37
     */
38
    private $liveIndexer;
39
40
    /**
41
     * @var DocumentManagerInterface
42
     */
43
    private $documentManager;
44
45
    /**
46
     * @var array
47
     */
48
    private $documents = [];
49
50
    /**
51
     * @var array
52
     */
53
    private $liveDocuments = [];
54
55
    /**
56
     * @param IndexerInterface $indexer
57
     * @param IndexerInterface $liveIndexer
58
     * @param DocumentManagerInterface $documentManager
59
     */
60 36
    public function __construct(
61
        IndexerInterface $indexer,
62
        IndexerInterface $liveIndexer,
63
        DocumentManagerInterface $documentManager
64
    ) {
65 36
        $this->indexer = $indexer;
66 36
        $this->liveIndexer = $liveIndexer;
67 36
        $this->documentManager = $documentManager;
68 36
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 31
    public static function getSubscribedEvents()
74
    {
75
        return [
76 31
            Events::PERSIST => [['handleScheduleIndex', -500]],
77 31
            Events::REMOVE => [['handleRemove', -500], ['handleRemoveLive', -500]],
78 31
            Events::PUBLISH => [['handleScheduleIndexLive', 0], ['handleScheduleIndex', 0]],
79 31
            Events::UNPUBLISH => 'handleUnpublish',
80 31
            Events::REMOVE_DRAFT => ['handleScheduleIndex', -1024],
81 31
            Events::FLUSH => [['handleFlush', -2048], ['handleFlushLive', -2048]],
82 31
            Events::COPY => ['handleCopy'],
83
        ];
84
    }
85
86
    /**
87
     * Schedule article document for index.
88
     *
89
     * @param AbstractMappingEvent $event
90
     */
91 32 View Code Duplication
    public function handleScheduleIndex(AbstractMappingEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    {
93 32
        $document = $event->getDocument();
94 32
        if (!$document instanceof ArticleDocument) {
95 1
            return;
96
        }
97
98 32
        $this->documents[$document->getUuid()] = [
99 32
            'uuid' => $document->getUuid(),
100 32
            'locale' => $document->getLocale(),
101
        ];
102 32
    }
103
104
    /**
105
     * Schedule article document for live index.
106
     *
107
     * @param AbstractMappingEvent $event
108
     */
109 13 View Code Duplication
    public function handleScheduleIndexLive(AbstractMappingEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111 13
        $document = $event->getDocument();
112 13
        if (!$document instanceof ArticleDocument) {
113 1
            return;
114
        }
115
116 13
        $this->liveDocuments[$document->getUuid()] = [
117 13
            'uuid' => $document->getUuid(),
118 13
            'locale' => $document->getLocale(),
119
        ];
120 13
    }
121
122
    /**
123
     * Index all scheduled article documents with default indexer.
124
     *
125
     * @param FlushEvent $event
126
     */
127 31 View Code Duplication
    public function handleFlush(FlushEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
    {
129 31
        if (count($this->documents) < 1) {
130 3
            return;
131
        }
132
133 31
        foreach ($this->documents as $document) {
134 31
            $this->indexer->index(
135 31
                $this->documentManager->find(
136 31
                    $document['uuid'],
137 31
                    $document['locale']
138
                )
139
            );
140
        }
141 31
        $this->indexer->flush();
142 31
        $this->documents = [];
143 31
    }
144
145
    /**
146
     * Index all scheduled article documents with live indexer.
147
     *
148
     * @param FlushEvent $event
149
     */
150 31 View Code Duplication
    public function handleFlushLive(FlushEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
    {
152 31
        if (count($this->liveDocuments) < 1) {
153 20
            return;
154
        }
155
156 12
        foreach ($this->liveDocuments as $document) {
157 12
            $this->liveIndexer->index(
158 12
                $this->documentManager->find(
159 12
                    $document['uuid'],
160 12
                    $document['locale']
161
                )
162
            );
163
        }
164 12
        $this->liveIndexer->flush();
165 12
        $this->liveDocuments = [];
166 12
    }
167
168
    /**
169
     * Indexes for article-document in live index.
170
     *
171
     * @param AbstractMappingEvent $event
172
     */
173
    public function handleIndexLive(AbstractMappingEvent $event)
174
    {
175
        $document = $event->getDocument();
176
        if (!$document instanceof ArticleDocument) {
177
            return;
178
        }
179
180
        $this->liveIndexer->index($document);
181
        $this->liveIndexer->flush();
182
    }
183
184
    /**
185
     * Removes document from live index and unpublish document in default index.
186
     *
187
     * @param UnpublishEvent $event
188
     */
189
    public function handleUnpublish(UnpublishEvent $event)
190
    {
191
        $document = $event->getDocument();
192
        if (!$document instanceof ArticleDocument) {
193
            return;
194
        }
195
196
        $this->liveIndexer->remove($document);
197
        $this->liveIndexer->flush();
198
199
        $this->indexer->setUnpublished($document->getUuid());
200
    }
201
202
    /**
203
     * Removes article-document.
204
     *
205
     * @param RemoveEvent $event
206
     */
207 3
    public function handleRemove(RemoveEvent $event)
208
    {
209 3
        $document = $event->getDocument();
210 3
        if (!$document instanceof ArticleDocument) {
211
            return;
212
        }
213
214 3
        $this->indexer->remove($document);
215 3
        $this->indexer->flush();
216 3
    }
217
218
    /**
219
     * Removes article-document.
220
     *
221
     * @param RemoveEvent|UnpublishEvent $event
222
     */
223 3
    public function handleRemoveLive($event)
224
    {
225 3
        $document = $event->getDocument();
226 3
        if (!$document instanceof ArticleDocument) {
227
            return;
228
        }
229
230 3
        $this->liveIndexer->remove($document);
231 3
        $this->liveIndexer->flush();
232 3
    }
233
234
    /**
235
     * Schedule document to index.
236
     *
237
     * @param CopyEvent $event
238
     */
239 View Code Duplication
    public function handleCopy(CopyEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
240
    {
241
        $document = $event->getDocument();
242
        if (!$document instanceof ArticleDocument) {
243
            return;
244
        }
245
246
        $uuid = $event->getCopiedNode()->getIdentifier();
247
        $this->documents[$uuid] = [
248
            'uuid' => $uuid,
249
            'locale' => $document->getLocale(),
250
        ];
251
    }
252
}
253