Completed
Pull Request — develop (#111)
by Wachter
12:56
created

ArticleSubscriber   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 205
Duplicated Lines 27.8 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 78.48%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 6
dl 57
loc 205
ccs 62
cts 79
cp 0.7848
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getSubscribedEvents() 0 11 1
A handleScheduleIndex() 12 12 2
A handleScheduleIndexLive() 12 12 2
A handleFlush() 16 17 3
A handleFlushLive() 17 17 3
A handleIndexLive() 0 10 2
A handleUnpublish() 0 12 2
A handleRemove() 0 10 2
A handleRemoveLive() 0 10 2

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
/*
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 36
    public function __construct(
60
        IndexerInterface $indexer,
61
        IndexerInterface $liveIndexer,
62
        DocumentManagerInterface $documentManager
63
    ) {
64 36
        $this->indexer = $indexer;
65 36
        $this->liveIndexer = $liveIndexer;
66 36
        $this->documentManager = $documentManager;
67 36
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 31
    public static function getSubscribedEvents()
73
    {
74
        return [
75 31
            Events::PERSIST => [['handleScheduleIndex', -500]],
76 31
            Events::REMOVE => [['handleRemove', -500], ['handleRemoveLive', -500]],
77 31
            Events::PUBLISH => [['handleScheduleIndexLive', 0], ['handleScheduleIndex', 0]],
78 31
            Events::UNPUBLISH => 'handleUnpublish',
79 31
            Events::REMOVE_DRAFT => ['handleScheduleIndex', -1024],
80 31
            Events::FLUSH => [['handleFlush', -2048], ['handleFlushLive', -2048]],
81
        ];
82
    }
83
84
    /**
85
     * Schedule article document for index.
86
     *
87
     * @param AbstractMappingEvent $event
88
     */
89 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...
90
    {
91 32
        $document = $event->getDocument();
92 32
        if (!$document instanceof ArticleDocument) {
93 1
            return;
94
        }
95
96 32
        $this->documents[$document->getUuid()] = [
97 32
            'uuid' => $document->getUuid(),
98 32
            'locale' => $document->getLocale(),
99
        ];
100 32
    }
101
102
    /**
103
     * Schedule article document for live index.
104
     *
105
     * @param AbstractMappingEvent $event
106
     */
107 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...
108
    {
109 13
        $document = $event->getDocument();
110 13
        if (!$document instanceof ArticleDocument) {
111 1
            return;
112
        }
113
114 13
        $this->liveDocuments[$document->getUuid()] = [
115 13
            'uuid' => $document->getUuid(),
116 13
            'locale' => $document->getLocale(),
117
        ];
118 13
    }
119
120
    /**
121
     * Index all scheduled article documents with default indexer.
122
     *
123
     * @param FlushEvent $event
124
     */
125 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...
126
    {
127 31
        if (count($this->documents) < 1) {
128 3
            return;
129
        }
130
131 31
        foreach ($this->documents as $document) {
132 31
            $this->indexer->index(
133 31
                $this->documentManager->find(
134 31
                    $document['uuid'],
135 31
                    $document['locale']
136
                )
137
            );
138
        }
139 31
        $this->indexer->flush();
140 31
        $this->documents = [];
141 31
    }
142
143
    /**
144
     * Index all scheduled article documents with live indexer.
145
     *
146
     * @param FlushEvent $event
147
     */
148 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...
149
    {
150 31
        if (count($this->liveDocuments) < 1) {
151 20
            return;
152
        }
153
154 12
        foreach ($this->liveDocuments as $document) {
155 12
            $this->liveIndexer->index(
156 12
                $this->documentManager->find(
157 12
                    $document['uuid'],
158 12
                    $document['locale']
159
                )
160
            );
161
        }
162 12
        $this->liveIndexer->flush();
163 12
        $this->liveDocuments = [];
164 12
    }
165
166
    /**
167
     * Indexes for article-document in live index.
168
     *
169
     * @param AbstractMappingEvent $event
170
     */
171
    public function handleIndexLive(AbstractMappingEvent $event)
172
    {
173
        $document = $event->getDocument();
174
        if (!$document instanceof ArticleDocument) {
175
            return;
176
        }
177
178
        $this->liveIndexer->index($document);
179
        $this->liveIndexer->flush();
180
    }
181
182
    /**
183
     * Removes document from live index and unpublish document in default index.
184
     *
185
     * @param UnpublishEvent $event
186
     */
187
    public function handleUnpublish(UnpublishEvent $event)
188
    {
189
        $document = $event->getDocument();
190
        if (!$document instanceof ArticleDocument) {
191
            return;
192
        }
193
194
        $this->liveIndexer->remove($document);
195
        $this->liveIndexer->flush();
196
197
        $this->indexer->setUnpublished($document->getUuid());
198
    }
199
200
    /**
201
     * Removes article-document.
202
     *
203
     * @param RemoveEvent $event
204
     */
205 3
    public function handleRemove(RemoveEvent $event)
206
    {
207 3
        $document = $event->getDocument();
208 3
        if (!$document instanceof ArticleDocument) {
209
            return;
210
        }
211
212 3
        $this->indexer->remove($document);
213 3
        $this->indexer->flush();
214 3
    }
215
216
    /**
217
     * Removes article-document.
218
     *
219
     * @param RemoveEvent|UnpublishEvent $event
220
     */
221 3
    public function handleRemoveLive($event)
222
    {
223 3
        $document = $event->getDocument();
224 3
        if (!$document instanceof ArticleDocument) {
225
            return;
226
        }
227
228 3
        $this->liveIndexer->remove($document);
229 3
        $this->liveIndexer->flush();
230 3
    }
231
}
232