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

Notification   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 10.99 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 8
dl 10
loc 91
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A broadcast() 0 15 1
A markAsRead() 0 9 1
A markAsUnread() 0 9 1
A applyNotificationPublished() 0 7 1
A applyNotificationMarkedAsRead() 0 5 1
A applyNotificationMarkedAsUnread() 0 5 1
A reconstitute() 10 10 2
A id() 0 4 1
A __toString() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Model\Inbox\Notification;
16
17
use Kreta\Notifier\Domain\Model\Inbox\UserId;
18
use Kreta\SharedKernel\Domain\Model\AggregateRoot;
19
use Kreta\SharedKernel\Domain\Model\EventSourcedAggregateRoot;
20
use Kreta\SharedKernel\Event\EventStream;
21
22
class Notification extends AggregateRoot implements EventSourcedAggregateRoot
23
{
24
    private $id;
25
    private $body;
26
    private $publishedOn;
27
    private $readOn;
28
    private $status;
29
    private $userId;
30
31
    private function __construct(NotificationId $id)
32
    {
33
        $this->id = $id;
34
    }
35
36
    public static function broadcast(NotificationId $id, UserId $userId, NotificationBody $body) : self
37
    {
38
        $notification = new self($id);
39
40
        $notification->publish(
41
            new NotificationPublished(
42
                $id,
43
                $userId,
44
                $body,
45
                NotificationStatus::unread()
0 ignored issues
show
Unused Code introduced by
The call to NotificationPublished::__construct() has too many arguments starting with \Kreta\Notifier\Domain\M...icationStatus::unread().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
46
            )
47
        );
48
49
        return $notification;
50
    }
51
52
    public function markAsRead() : void
53
    {
54
        $this->publish(
55
            new NotificationMarkedAsRead(
56
                $this->id,
57
                $this->userId
58
            )
59
        );
60
    }
61
62
    public function markAsUnread() : void
63
    {
64
        $this->publish(
65
            new NotificationMarkedAsUnread(
66
                $this->id,
67
                $this->userId
68
            )
69
        );
70
    }
71
72
    protected function applyNotificationPublished(NotificationPublished $event) : void
73
    {
74
        $this->body = $event->body();
75
        $this->userId = $event->userId();
76
        $this->publishedOn = $event->occurredOn();
77
        $this->status = $event->status();
78
    }
79
80
    protected function applyNotificationMarkedAsRead(NotificationMarkedAsRead $event) : void
81
    {
82
        $this->readOn = $event->occurredOn();
83
        $this->status = $event->status();
84
    }
85
86
    protected function applyNotificationMarkedAsUnread(NotificationMarkedAsUnread $event) : void
87
    {
88
        $this->readOn = null;
89
        $this->status = $event->status();
90
    }
91
92 View Code Duplication
    public static function reconstitute(EventStream $stream) : EventSourcedAggregateRoot
0 ignored issues
show
Duplication introduced by
This method 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...
93
    {
94
        $notification = new self($stream->aggregateRootId());
0 ignored issues
show
Compatibility introduced by
$stream->aggregateRootId() of type object<Kreta\SharedKerne...\Model\Identity\BaseId> is not a sub-type of object<Kreta\Notifier\Do...ication\NotificationId>. It seems like you assume a concrete implementation of the interface Kreta\SharedKernel\Domain\Model\Identity\BaseId to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
95
        $events = $stream->events()->toArray();
96
        foreach ($events as $event) {
97
            $notification->apply($event);
98
        }
99
100
        return $notification;
101
    }
102
103
    public function id() : NotificationId
104
    {
105
        return $this->id;
106
    }
107
108
    public function __toString() : string
109
    {
110
        return (string) $this->id()->id();
111
    }
112
}
113