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

UpdateLocation::receive()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 8
eloc 18
c 4
b 0
f 0
nc 6
nop 1
dl 0
loc 33
rs 5.3846
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 UpdateLocationSignal.
21
 */
22
class UpdateLocation 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\LocationService\UpdateLocationSignal || !$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(
44
            $signal->contentId
45
        );
46
47
        if ($this->canIndexContent()) {
48
            $this->searchHandler->indexContent(
49
                $this->persistenceHandler->contentHandler()->load(
50
                    $signal->contentId,
51
                    $contentInfo->currentVersionNo
52
                )
53
            );
54
        }
55
56
        if ($this->canIndexLocation()) {
57
            $this->searchHandler->indexLocation(
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
                $this->persistenceHandler->locationHandler()->load($signal->locationId)
59
            );
60
        }
61
    }
62
}
63