Completed
Push — EZP-25088-search-handler-inter... ( 170b1b...34d2d2 )
by
unknown
18:19
created

SetContentState::receive()   D

Complexity

Conditions 9
Paths 8

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 9
eloc 18
c 3
b 0
f 0
nc 8
nop 1
dl 0
loc 32
rs 4.909
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\Search\Common\Slot;
12
13
use eZ\Publish\Core\SignalSlot\Signal;
14
use eZ\Publish\Core\Search\Common\Slot;
15
use eZ\Publish\SPI\Search\Indexing\ContentIndexing;
16
use eZ\Publish\SPI\Search\Indexing\FullTextIndexing;
17
use eZ\Publish\SPI\Search\Indexing\LocationIndexing;
18
19
/**
20
 * A Search Engine slot handling SetContentStateSignal.
21
 */
22
class SetContentState extends Slot
23
{
24
    /**
25
     * Receive the given $signal and react on it.
26
     *
27
     * @param \eZ\Publish\Core\SignalSlot\Signal $signal
28
     */
29
    public function receive(Signal $signal)
30
    {
31
        if (!$signal instanceof Signal\ObjectStateService\SetContentStateSignal || !$this->canIndex()) {
32
            return;
33
        }
34
35
        if (
36
            $this->searchHandler instanceof FullTextIndexing &&
37
            !$this->searchHandler instanceof ContentIndexing &&
38
            !$this->searchHandler instanceof LocationIndexing
39
        ) {
40
            return;
41
        }
42
43
        $contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($signal->contentId);
44
45
        if ($this->canIndexContent()) {
46
            $this->searchHandler->indexContent(
47
                $this->persistenceHandler->contentHandler()->load(
48
                    $contentInfo->id,
49
                    $contentInfo->currentVersionNo
50
                )
51
            );
52
        }
53
54
        if ($this->canIndexLocation()) {
55
            $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($contentInfo->id);
56
            foreach ($locations as $location) {
57
                $this->searchHandler->indexLocation($location);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eZ\Publish\SPI\Search\Handler as the method indexLocation() does only exist in the following implementations of said interface: eZ\Publish\Core\Search\E...csearch\Content\Handler.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
58
            }
59
        }
60
    }
61
}
62