Completed
Pull Request — master (#366)
by Beñat
14:40
created

handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\Notifier\Infrastructure\Projection\EventHandler\Doctrine\ORM\Inbox;
16
17
use Doctrine\ORM\EntityManagerInterface;
18
use Kreta\Notifier\Domain\Model\Inbox\NotificationStatus;
19
use Kreta\Notifier\Domain\Model\Inbox\UserReadNotification;
20
use Kreta\Notifier\Infrastructure\Projection\ReadModel\Inbox\User;
21
use Kreta\SharedKernel\Domain\Model\DomainEvent;
22
use Kreta\SharedKernel\Projection\EventHandler;
23
24 View Code Duplication
class DoctrineORMUserReadNotificationEventHandler implements EventHandler
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...
25
{
26
    private $manager;
27
28
    public function __construct(EntityManagerInterface $manager)
29
    {
30
        $this->manager = $manager;
31
    }
32
33
    public function eventType() : string
34
    {
35
        return UserReadNotification::class;
36
    }
37
38
    public function handle(DomainEvent $event) : void
39
    {
40
        $user = $this->manager->getRepository(User::class)->find($event->userId());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Kreta\SharedKernel\Domain\Model\DomainEvent as the method userId() does only exist in the following implementations of said interface: Kreta\Notifier\Domain\Mo...ox\UserReadNotification, Kreta\Notifier\Domain\Mo...serReceivedNotification, Kreta\Notifier\Domain\Model\Inbox\UserSignedUp, Kreta\Notifier\Domain\Mo...\UserUnreadNotification, Kreta\TaskManager\Domain...OrganizationMemberAdded, Kreta\TaskManager\Domain...ganizationMemberRemoved, Kreta\TaskManager\Domain...Organization\OwnerAdded, Kreta\TaskManager\Domain...ganization\OwnerRemoved.

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...
41
42
        foreach ($user->notifications as $index => $notification) {
43
            $user->notifications[$index]->readOn = $event->occurredOn();
44
            $user->notifications[$index]->status = (NotificationStatus::read())->status();
45
        }
46
47
        $this->manager->persist($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->manager->getRepos...>find($event->userId()) on line 40 can also be of type null; however, Doctrine\Common\Persiste...bjectManager::persist() 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...
48
        $this->manager->flush();
49
    }
50
}
51