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

CreateUserGroup::receive()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 28
Code Lines 15

Duplication

Lines 28
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 3
b 0
f 0
nc 7
nop 1
dl 28
loc 28
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\FullTextIndexing;
17
18
/**
19
 * A Search Engine slot handling CreateUserGroupSignal.
20
 */
21 View Code Duplication
class CreateUserGroup extends Slot
0 ignored issues
show
Duplication introduced by
This class 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...
22
{
23
    /**
24
     * Receive the given $signal and react on it.
25
     *
26
     * @param \eZ\Publish\Core\SignalSlot\Signal $signal
27
     */
28
    public function receive(Signal $signal)
29
    {
30
        if (!$signal instanceof Signal\UserService\CreateUserGroupSignal || !$this->canIndex()) {
31
            return;
32
        }
33
34
        $userGroupContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo(
35
            $signal->userGroupId
36
        );
37
38
        if ($this->canIndexContent()) {
39
            $this->searchHandler->indexContent(
40
                $this->persistenceHandler->contentHandler()->load(
41
                    $userGroupContentInfo->id,
42
                    $userGroupContentInfo->currentVersionNo
43
                )
44
            );
45
        }
46
47
        if ($this->canIndexLocation()) {
48
            $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent(
49
                $userGroupContentInfo->id
50
            );
51
            foreach ($locations as $location) {
52
                $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...
53
            }
54
        }
55
    }
56
57
    protected function canIndexContent()
58
    {
59
        return $this->searchHandler instanceof ContentIndexing || $this->searchHandler instanceof FullTextIndexing;
0 ignored issues
show
Bug introduced by
The class eZ\Publish\Core\Search\Common\Slot\ContentIndexing does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
60
    }
61
}
62