Completed
Push — develop ( 7df1b3...61c51f )
by Alexander
12:30
created

ArticleSubscriber::handleRoute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 8
nc 2
nop 1
crap 3
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\FlushEvent;
19
use Sulu\Component\DocumentManager\Event\RemoveEvent;
20
use Sulu\Component\DocumentManager\Event\UnpublishEvent;
21
use Sulu\Component\DocumentManager\Events;
22
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
23
24
/**
25
 * Indexes article and generate route on persist and removes it from index and routing on delete.
26
 */
27
class ArticleSubscriber implements EventSubscriberInterface
28
{
29
    /**
30
     * @var IndexerInterface
31
     */
32
    private $indexer;
33
34
    /**
35
     * @var IndexerInterface
36
     */
37
    private $liveIndexer;
38
39
    /**
40
     * @var DocumentManagerInterface
41
     */
42
    private $documentManager;
43
44
    /**
45
     * @var array
46
     */
47
    private $documents = [];
48
49
    /**
50
     * @var array
51
     */
52
    private $liveDocuments = [];
53
54
    /**
55
     * @param IndexerInterface $indexer
56
     * @param IndexerInterface $liveIndexer
57
     * @param DocumentManagerInterface $documentManager
58
     */
59
    public function __construct(
60
        IndexerInterface $indexer,
61
        IndexerInterface $liveIndexer,
62
        DocumentManagerInterface $documentManager
63
    ) {
64
        $this->indexer = $indexer;
65
        $this->liveIndexer = $liveIndexer;
66
        $this->documentManager = $documentManager;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public static function getSubscribedEvents()
73
    {
74
        return [
75
            Events::PERSIST => [['handleScheduleIndex', -500]],
76
            Events::REMOVE => [['handleRemove', -500], ['handleRemoveLive', -500]],
77
            Events::PUBLISH => [['handleScheduleIndexLive', 0], ['handleScheduleIndex', 0]],
78
            Events::UNPUBLISH => 'handleUnpublish',
79
            Events::REMOVE_DRAFT => ['handleScheduleIndex', -1024],
80
            Events::FLUSH => [['handleFlush', -2048], ['handleFlushLive', -2048]],
81
        ];
82 34
    }
83
84
    /**
85
     * Schedule article document for index.
86
     *
87
     * @param AbstractMappingEvent $event
88
     */
89 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...
90 34
    {
91 34
        $document = $event->getDocument();
92 34
        if (!$document instanceof ArticleDocument) {
93 34
            return;
94 34
        }
95 34
96 34
        $this->documents[$document->getUuid()] = [
97
            'uuid' => $document->getUuid(),
98
            'locale' => $document->getLocale(),
99
        ];
100
    }
101 31
102
    /**
103
     * Schedule article document for live index.
104 31
     *
105 31
     * @param AbstractMappingEvent $event
106 31
     */
107 31 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...
108 31
    {
109 31
        $document = $event->getDocument();
110 31
        if (!$document instanceof ArticleDocument) {
111 31
            return;
112 31
        }
113
114
        $this->liveDocuments[$document->getUuid()] = [
115
            'uuid' => $document->getUuid(),
116
            'locale' => $document->getLocale(),
117
        ];
118
    }
119
120
    /**
121 31
     * Index all scheduled article documents with default indexer.
122
     *
123 31
     * @param FlushEvent $event
124 31
     */
125 29 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...
126
    {
127
        if (count($this->documents) < 1) {
128 31
            return;
129 31
        }
130 30
131
        foreach ($this->documents as $document) {
132
            $this->indexer->index(
133 31
                $this->documentManager->find(
134 31
                    $document['uuid'],
135
                    $document['locale']
136
                )
137
            );
138
        }
139
        $this->indexer->flush();
140
        $this->documents = [];
141 32
    }
142
143 32
    /**
144 32
     * Index all scheduled article documents with live indexer.
145 9
     *
146
     * @param FlushEvent $event
147
     */
148 32 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...
149
    {
150 32
        if (count($this->liveDocuments) < 1) {
151 32
            return;
152 32
        }
153 32
154
        foreach ($this->liveDocuments as $document) {
155
            $this->liveIndexer->index(
156
                $this->documentManager->find(
157
                    $document['uuid'],
158
                    $document['locale']
159
                )
160 31
            );
161
        }
162 31
        $this->liveIndexer->flush();
163 31
        $this->liveDocuments = [];
164 31
    }
165 31
166
    /**
167 30
     * Indexes for article-document in live index.
168
     *
169
     * @param AbstractMappingEvent $event
170 1
     */
171 1
    public function handleIndexLive(AbstractMappingEvent $event)
172 1
    {
173 1
        $document = $event->getDocument();
174 1
        if (!$document instanceof ArticleDocument) {
175
            return;
176
        }
177
178
        $this->liveIndexer->index($document);
179
        $this->liveIndexer->flush();
180
    }
181 30
182
    /**
183 30
     * Removes document from live index and unpublish document in default index.
184 30
     *
185 1
     * @param UnpublishEvent $event
186
     */
187
    public function handleUnpublish(UnpublishEvent $event)
188 30
    {
189 30
        $document = $event->getDocument();
190 30
        if (!$document instanceof ArticleDocument) {
191
            return;
192 30
        }
193
194
        $this->liveIndexer->remove($document);
195
        $this->liveIndexer->flush();
196
197
        $this->indexer->setUnpublished($document->getUuid());
198
    }
199 11
200
    /**
201 11
     * Removes article-document.
202 11
     *
203 1
     * @param RemoveEvent $event
204
     */
205
    public function handleRemove(RemoveEvent $event)
206 11
    {
207 11
        $document = $event->getDocument();
208 11
        if (!$document instanceof ArticleDocument) {
209
            return;
210 11
        }
211
212
        $this->indexer->remove($document);
213
        $this->indexer->flush();
214
    }
215
216
    /**
217 30
     * Removes article-document.
218
     *
219 30
     * @param RemoveEvent|UnpublishEvent $event
220 3
     */
221
    public function handleRemoveLive($event)
222
    {
223 30
        $document = $event->getDocument();
224 30
        if (!$document instanceof ArticleDocument) {
225 30
            return;
226 30
        }
227
228
        $this->liveIndexer->remove($document);
229
        $this->liveIndexer->flush();
230 30
    }
231
}
232