Completed
Pull Request — develop (#199)
by Wachter
13:59
created

ArticleIndexer::getRoutePathProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 12
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
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\Index;
13
14
use ONGR\ElasticsearchBundle\Collection\Collection;
15
use ONGR\ElasticsearchBundle\Service\Manager;
16
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
17
use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery;
18
use Sulu\Bundle\ArticleBundle\Content\PageTreeRouteContentType;
19
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
20
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocumentInterface;
21
use Sulu\Bundle\ArticleBundle\Document\Index\Factory\ExcerptFactory;
22
use Sulu\Bundle\ArticleBundle\Document\Index\Factory\SeoFactory;
23
use Sulu\Bundle\ArticleBundle\Document\LocalizationStateViewObject;
24
use Sulu\Bundle\ArticleBundle\Document\Subscriber\RoutableSubscriber;
25
use Sulu\Bundle\ArticleBundle\Event\Events;
26
use Sulu\Bundle\ArticleBundle\Event\IndexEvent;
27
use Sulu\Bundle\ArticleBundle\Metadata\ArticleTypeTrait;
28
use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait;
29
use Sulu\Bundle\ContactBundle\Entity\ContactRepository;
30
use Sulu\Bundle\SecurityBundle\UserManager\UserManager;
31
use Sulu\Component\Content\Document\LocalizationState;
32
use Sulu\Component\Content\Document\WorkflowStage;
33
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
34
use Sulu\Component\Content\Metadata\PropertyMetadata;
35
use Sulu\Component\Content\Metadata\StructureMetadata;
36
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
37
use Symfony\Component\Translation\TranslatorInterface;
38
39
/**
40
 * Provides methods to index articles.
41
 */
42
class ArticleIndexer implements IndexerInterface
43
{
44
    use ArticleTypeTrait;
45
    use ArticleViewDocumentIdTrait;
46
47
    /**
48
     * @var StructureMetadataFactoryInterface
49
     */
50
    protected $structureMetadataFactory;
51
52
    /**
53
     * @var UserManager
54
     */
55
    protected $userManager;
56
57
    /**
58
     * @var ContactRepository
59
     */
60
    protected $contactRepository;
61
62
    /**
63
     * @var DocumentFactoryInterface
64
     */
65
    protected $documentFactory;
66
67
    /**
68
     * @var Manager
69
     */
70
    protected $manager;
71
72
    /**
73
     * @var ExcerptFactory
74
     */
75
    protected $excerptFactory;
76
77
    /**
78
     * @var SeoFactory
79
     */
80
    protected $seoFactory;
81
82
    /**
83
     * @var EventDispatcherInterface
84
     */
85
    protected $eventDispatcher;
86
87
    /**
88
     * @var TranslatorInterface
89
     */
90
    protected $translator;
91
92
    /**
93
     * @var array
94
     */
95
    protected $typeConfiguration;
96
97
    /**
98
     * @param StructureMetadataFactoryInterface $structureMetadataFactory
99
     * @param UserManager $userManager
100
     * @param ContactRepository $contactRepository
101
     * @param DocumentFactoryInterface $documentFactory
102
     * @param Manager $manager
103
     * @param ExcerptFactory $excerptFactory
104
     * @param SeoFactory $seoFactory
105 51
     * @param EventDispatcherInterface $eventDispatcher
106
     * @param TranslatorInterface $translator
107
     * @param array $typeConfiguration
108
     */
109
    public function __construct(
110
        StructureMetadataFactoryInterface $structureMetadataFactory,
111
        UserManager $userManager,
112
        ContactRepository $contactRepository,
113
        DocumentFactoryInterface $documentFactory,
114
        Manager $manager,
115
        ExcerptFactory $excerptFactory,
116
        SeoFactory $seoFactory,
117 51
        EventDispatcherInterface $eventDispatcher,
118 51
        TranslatorInterface $translator,
119 51
        array $typeConfiguration
120 51
    ) {
121 51
        $this->structureMetadataFactory = $structureMetadataFactory;
122 51
        $this->userManager = $userManager;
123 51
        $this->contactRepository = $contactRepository;
124 51
        $this->documentFactory = $documentFactory;
125 51
        $this->manager = $manager;
126 51
        $this->excerptFactory = $excerptFactory;
127 51
        $this->seoFactory = $seoFactory;
128
        $this->eventDispatcher = $eventDispatcher;
129
        $this->translator = $translator;
130
        $this->typeConfiguration = $typeConfiguration;
131
    }
132
133
    /**
134
     * Returns translation for given article type.
135
     *
136 50
     * @param string $type
137
     *
138 50
     * @return string
139 50
     */
140
    private function getTypeTranslation($type)
141
    {
142
        if (!array_key_exists($type, $this->typeConfiguration)) {
143
            return ucfirst($type);
144
        }
145
146
        $typeTranslationKey = $this->typeConfiguration[$type]['translation_key'];
147
148
        return $this->translator->trans($typeTranslationKey, [], 'backend');
149
    }
150
151 50
    /**
152
     * @param ArticleDocument $document
153 50
     * @param ArticleViewDocumentInterface $article
154 50
     */
155
    protected function dispatchIndexEvent(ArticleDocument $document, ArticleViewDocumentInterface $article)
156
    {
157
        $this->eventDispatcher->dispatch(Events::INDEX_EVENT, new IndexEvent($document, $article));
158
    }
159
160
    /**
161
     * @param ArticleDocument $document
162
     * @param string $locale
163 50
     * @param string $localizationState
164
     *
165
     * @return ArticleViewDocumentInterface
166
     */
167
    protected function createOrUpdateArticle(
168 50
        ArticleDocument $document,
169 50
        $locale,
170 5
        $localizationState = LocalizationState::LOCALIZED
171
    ) {
172
        $article = $this->findOrCreateViewDocument($document, $locale, $localizationState);
173 50
        if (!$article) {
174 50
            return;
175 50
        }
176
177
        $structureMetadata = $this->structureMetadataFactory->getStructureMetadata(
178 50
            'article',
179 50
            $document->getStructureType()
180 50
        );
181 50
182 50
        $article->setTitle($document->getTitle());
183 50
        $article->setRoutePath($document->getRoutePath());
184 50
        $this->setParentPageUuid($structureMetadata, $document, $article);
0 ignored issues
show
Bug introduced by
It seems like $structureMetadata defined by $this->structureMetadata...nt->getStructureType()) on line 177 can be null; however, Sulu\Bundle\ArticleBundl...er::setParentPageUuid() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
185 50
        $article->setChanged($document->getChanged());
186
        $article->setCreated($document->getCreated());
187 50
        $article->setAuthored($document->getAuthored());
188 50
        if ($document->getAuthor() && $author = $this->contactRepository->find($document->getAuthor())) {
189 50
            $article->setAuthorFullName($author->getFullName());
190
            $article->setAuthorId($author->getId());
191 50
        }
192 50
        if ($document->getChanger() && $changer = $this->userManager->getUserById($document->getChanger())) {
193 50
            $article->setChangerFullName($changer->getFullName());
194
            $article->setChangerContactId($changer->getContact()->getId());
195 50
        }
196 50
        if ($document->getCreator() && $creator = $this->userManager->getUserById($document->getCreator())) {
197 50
            $article->setCreatorFullName($creator->getFullName());
198 50
            $article->setCreatorContactId($creator->getContact()->getId());
199 50
        }
200 50
        $article->setType($this->getType($structureMetadata));
0 ignored issues
show
Bug introduced by
It seems like $structureMetadata defined by $this->structureMetadata...nt->getStructureType()) on line 177 can be null; however, Sulu\Bundle\ArticleBundl...cleTypeTrait::getType() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
201 50
        $article->setStructureType($document->getStructureType());
202
        $article->setPublished($document->getPublished());
0 ignored issues
show
Documentation introduced by
$document->getPublished() is of type boolean, but the function expects a null|object<DateTime>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
203 50
        $article->setPublishedState($document->getWorkflowStage() === WorkflowStage::PUBLISHED);
204
        $article->setTypeTranslation($this->getTypeTranslation($this->getType($structureMetadata)));
0 ignored issues
show
Bug introduced by
It seems like $structureMetadata defined by $this->structureMetadata...nt->getStructureType()) on line 177 can be null; however, Sulu\Bundle\ArticleBundl...cleTypeTrait::getType() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
205
        $article->setLocalizationState(
206
            new LocalizationStateViewObject(
207 50
                $localizationState,
208 50
                (LocalizationState::LOCALIZED === $localizationState) ? null : $document->getLocale()
209 50
            )
210
        );
211 50
212 50
        $extensions = $document->getExtensionsData()->toArray();
213
        if (array_key_exists('excerpt', $extensions)) {
214 50
            $article->setExcerpt($this->excerptFactory->create($extensions['excerpt'], $document->getLocale()));
215 1
        }
216 1
        if (array_key_exists('seo', $extensions)) {
217 1
            $article->setSeo($this->seoFactory->create($extensions['seo']));
218
        }
219
        if ($structureMetadata->hasPropertyWithTagName('sulu.teaser.description')) {
220 50
            $descriptionProperty = $structureMetadata->getPropertyByTagName('sulu.teaser.description');
221 1
            $article->setTeaserDescription(
222 1
                $document->getStructure()->getProperty($descriptionProperty->getName())->getValue()
0 ignored issues
show
Documentation introduced by
$document->getStructure(...>getName())->getValue() is of type array<integer|string,*>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
223 1
            );
224 1
        }
225
        if ($structureMetadata->hasPropertyWithTagName('sulu.teaser.media')) {
226
            $mediaProperty = $structureMetadata->getPropertyByTagName('sulu.teaser.media');
227
            $mediaData = $document->getStructure()->getProperty($mediaProperty->getName())->getValue();
228 50
            if (null !== $mediaData && array_key_exists('ids', $mediaData)) {
229
                $article->setTeaserMediaId(reset($mediaData['ids']) ?: null);
230 50
            }
231
        }
232
233
        $this->mapPages($document, $article);
234
235
        return $article;
236
    }
237
238
    /**
239
     * Returns view-document from index or create a new one.
240
     *
241
     * @param ArticleDocument $document
242 50
     * @param string $locale
243
     * @param string $localizationState
244 50
     *
245
     * @return ArticleViewDocumentInterface
246 50
     */
247
    protected function findOrCreateViewDocument(ArticleDocument $document, $locale, $localizationState)
248 50
    {
249
        $articleId = $this->getViewDocumentId($document->getUuid(), $locale);
250 18
        /** @var ArticleViewDocumentInterface $article */
251 18
        $article = $this->manager->find($this->documentFactory->getClass('article'), $articleId);
252
253 5
        if ($article) {
254
            // Only index ghosts when the article isn't a ghost himself.
255
            if (LocalizationState::GHOST === $localizationState
256 18
                && LocalizationState::GHOST !== $article->getLocalizationState()->state
257
            ) {
258
                return null;
259 50
            }
260 50
261 50
            return $article;
262 50
        }
263
264 50
        $article = $this->documentFactory->create('article');
265
        $article->setId($articleId);
266
        $article->setUuid($document->getUuid());
267
        $article->setLocale($locale);
268
269
        return $article;
270
    }
271
272
    /**
273 50
     * Maps pages from document to view-document.
274
     *
275 50
     * @param ArticleDocument $document
276 50
     * @param ArticleViewDocumentInterface $article
277 8
     */
278 8
    private function mapPages(ArticleDocument $document, ArticleViewDocumentInterface $article)
279 8
    {
280 8
        $pages = [];
281 8
        foreach ($document->getChildren() as $child) {
282
            $pages[] = $page = $this->documentFactory->create('article_page');
283
            $page->uuid = $child->getUuid();
0 ignored issues
show
Bug introduced by
Accessing uuid on the interface Sulu\Bundle\ArticleBundl...leViewDocumentInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
284 50
            $page->pageNumber = $child->getPageNumber();
0 ignored issues
show
Bug introduced by
Accessing pageNumber on the interface Sulu\Bundle\ArticleBundl...leViewDocumentInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
285 50
            $page->title = $child->getPageTitle();
0 ignored issues
show
Bug introduced by
Accessing title on the interface Sulu\Bundle\ArticleBundl...leViewDocumentInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
286
            $page->routePath = $child->getRoutePath();
0 ignored issues
show
Bug introduced by
Accessing routePath on the interface Sulu\Bundle\ArticleBundl...leViewDocumentInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
287
        }
288
289
        $article->setPages(new Collection($pages));
290
    }
291
292
    /**
293
     * Set parent-page-uuid to view-document.
294
     *
295
     * @param StructureMetadata $metadata
296
     * @param ArticleDocument $document
297
     * @param ArticleViewDocumentInterface $article
298
     */
299
    private function setParentPageUuid(
300
        StructureMetadata $metadata,
301
        ArticleDocument $document,
302
        ArticleViewDocumentInterface $article
303
    ) {
304
        $propertyMetadata = $this->getRoutePathProperty($metadata);
305
        if (!$propertyMetadata) {
306 2
            return;
307
        }
308 2
309 2
        $property = $document->getStructure()->getProperty($propertyMetadata->getName());
310 2
        if (!$property || $propertyMetadata->getType() !== PageTreeRouteContentType::NAME || !$property->getValue()) {
311 2
            return;
312 2
        }
313 2
314
        $value = $property->getValue();
315 2
        if (!$value || !isset($value['page']) || !isset($value['page']['uuid'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
316
            return;
317
        }
318
319
        $article->setParentPageUuid($value['page']['uuid']);
320 50
    }
321
322 50
    /**
323 50
     * Returns property-metadata for route-path property.
324
     *
325
     * @param StructureMetadata $metadata
326
     *
327
     * @return PropertyMetadata
328 17
     */
329 View Code Duplication
    private function getRoutePathProperty(StructureMetadata $metadata)
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...
330 17
    {
331 17
        if ($metadata->hasTag(RoutableSubscriber::TAG_NAME)) {
332 17
            return $metadata->getPropertyByTagName(RoutableSubscriber::TAG_NAME);
333 17
        }
334 17
335
        if (!$metadata->hasProperty(RoutableSubscriber::ROUTE_FIELD)) {
336
            return;
337 17
        }
338 17
339 15
        return $metadata->getProperty(RoutableSubscriber::ROUTE_FIELD);
340
    }
341
342 17
    /**
343 17
     * @param string $id
344
     */
345 17
    protected function removeArticle($id)
346 17
    {
347 17
        $article = $this->manager->find(
348
            $this->documentFactory->getClass('article'),
349
            $id
350
        );
351
        if (null === $article) {
352
            return;
353
        }
354
355
        $this->manager->remove($article);
356
    }
357
358
    /**
359
     * {@inheritdoc}
360
     */
361
    public function remove($document)
362
    {
363
        $repository = $this->manager->getRepository($this->documentFactory->getClass('article'));
364
        $search = $repository->createSearch()
365
            ->addQuery(new TermQuery('uuid', $document->getUuid()))
366
            ->setSize(1000);
367
        foreach ($repository->findDocuments($search) as $viewDocument) {
368
            $this->manager->remove($viewDocument);
0 ignored issues
show
Bug introduced by
It seems like $viewDocument defined by $viewDocument on line 367 can also be of type array or null; however, ONGR\ElasticsearchBundle\Service\Manager::remove() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
369 17
        }
370
    }
371 17
372 17
    /**
373 17
     * {@inheritdoc}
374 17
     */
375
    public function flush()
376
    {
377
        $this->manager->commit();
378
    }
379
380
    /**
381
     * {@inheritdoc}
382
     */
383
    public function clear()
384
    {
385
        $pageSize = 500;
386
        $repository = $this->manager->getRepository($this->documentFactory->getClass('article'));
387
        $search = $repository->createSearch()
388
            ->addQuery(new MatchAllQuery())
389
            ->setSize($pageSize);
390
391
        do {
392
            $result = $repository->findDocuments($search);
393
            foreach ($result as $document) {
394
                $this->manager->remove($document);
0 ignored issues
show
Bug introduced by
It seems like $document defined by $document on line 393 can also be of type array or null; however, ONGR\ElasticsearchBundle\Service\Manager::remove() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
395
            }
396
397
            $this->manager->commit();
398
        } while ($result->count() !== 0);
399
400
        $this->manager->clearCache();
401
        $this->manager->flush();
402
    }
403
404
    /**
405
     * {@inheritdoc}
406
     */
407
    public function setUnpublished($uuid)
408
    {
409
        $article = $this->manager->find($this->documentFactory->getClass('article'), $uuid);
410
411
        if (!$article) {
412
            return;
413
        }
414
415
        $article->setPublished(null);
416
        $article->setPublishedState(false);
417
418
        $this->manager->persist($article);
419
    }
420
421
    /**
422
     * {@inheritdoc}
423
     */
424
    public function index(ArticleDocument $document)
425
    {
426
        $article = $this->createOrUpdateArticle($document, $document->getLocale());
427
        $this->dispatchIndexEvent($document, $article);
0 ignored issues
show
Bug introduced by
It seems like $article defined by $this->createOrUpdateArt...$document->getLocale()) on line 426 can be null; however, Sulu\Bundle\ArticleBundl...r::dispatchIndexEvent() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
428
        $this->manager->persist($article);
0 ignored issues
show
Bug introduced by
It seems like $article defined by $this->createOrUpdateArt...$document->getLocale()) on line 426 can be null; however, ONGR\ElasticsearchBundle...vice\Manager::persist() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
429
    }
430
}
431