Completed
Pull Request — master (#366)
by Beñat
04:55
created

NotificationPublishedEventHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 27
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isSubscribeTo() 0 4 1
A handle() 0 12 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\Domain\ReadEvent\Inbox\Notification;
16
17
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationPublished;
18
use Kreta\Notifier\Domain\ReadModel\Inbox\Notification\Notification;
19
use Kreta\Notifier\Domain\ReadModel\Inbox\Notification\NotificationView;
20
use Kreta\SharedKernel\Domain\Model\DomainEvent;
21
use Kreta\SharedKernel\Domain\ReadEvent\EventHandler;
22
23
class NotificationPublishedEventHandler implements EventHandler
24
{
25
    private $view;
26
27
    public function __construct(NotificationView $view)
28
    {
29
        $this->view = $view;
30
    }
31
32
    public function isSubscribeTo() : string
33
    {
34
        return NotificationPublished::class;
35
    }
36
37
    public function handle(DomainEvent $event) : void
38
    {
39
        $notification = new Notification(
40
            $event->notificationId()->id(),
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 notificationId() does only exist in the following implementations of said interface: Kreta\Notifier\Domain\Mo...otificationMarkedAsRead, Kreta\Notifier\Domain\Mo...ificationMarkedAsUnread, Kreta\Notifier\Domain\Mo...n\NotificationPublished.

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
            $event->userId()->id(),
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...otificationMarkedAsRead, Kreta\Notifier\Domain\Mo...ificationMarkedAsUnread, Kreta\Notifier\Domain\Mo...n\NotificationPublished, Kreta\Notifier\Domain\Model\Inbox\UserSignedUp, 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...
42
            $event->body()->body(),
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 body() does only exist in the following implementations of said interface: Kreta\Notifier\Domain\Mo...n\NotificationPublished.

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...
43
            $event->occurredOn()->getTimestamp(),
44
            $event->status()->status()
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 status() does only exist in the following implementations of said interface: Kreta\Notifier\Domain\Mo...otificationMarkedAsRead, Kreta\Notifier\Domain\Mo...ificationMarkedAsUnread, Kreta\Notifier\Domain\Mo...n\NotificationPublished.

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...
45
        );
46
47
        $this->view->save($notification);
48
    }
49
}
50