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

Recover::receive()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 14
c 3
b 0
f 0
nc 6
nop 1
dl 0
loc 26
rs 8.439
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;
16
use eZ\Publish\SPI\Search\Indexing\ContentIndexing;
17
use eZ\Publish\SPI\Search\Indexing\FullTextIndexing;
18
use eZ\Publish\SPI\Search\Indexing\LocationIndexing;
19
20
/**
21
 * A Search Engine slot handling RecoverSignal.
22
 */
23
class Recover extends Slot
24
{
25
    /**
26
     * Receive the given $signal and react on it.
27
     *
28
     * @param \eZ\Publish\Core\SignalSlot\Signal $signal
29
     */
30
    public function receive(Signal $signal)
31
    {
32
        if (!$signal instanceof Signal\TrashService\RecoverSignal || !$this->canIndex()) {
33
            return;
34
        }
35
36
        $contentHandler = $this->persistenceHandler->contentHandler();
37
        $subtreeIds = $this->persistenceHandler->locationHandler()->loadSubtreeIds(
38
            $signal->newLocationId
39
        );
40
41
        foreach ($subtreeIds as $contentId) {
42
            if ($this->canIndexContent()) {
43
                $contentInfo = $contentHandler->loadContentInfo($contentId);
44
                $this->searchHandler->indexContent(
45
                    $contentHandler->load($contentInfo->id, $contentInfo->currentVersionNo)
46
                );
47
            }
48
49
            if ($this->canIndexLocation()) {
50
                $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...
51
                    $this->persistenceHandler->locationHandler()->load($signal->newLocationId)
52
                );
53
            }
54
        }
55
    }
56
57
    protected function canIndexContent()
58
    {
59
        return $this->searchHandler instanceof ContentIndexing || $this->searchHandler instanceof FullTextIndexing;
60
    }
61
}
62