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

User   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 108
Duplicated Lines 22.22 %

Coupling/Cohesion

Components 2
Dependencies 12

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 12
dl 24
loc 108
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A signUp() 0 7 1
A receiveNotification() 0 10 1
A readNotification() 0 6 1
A unreadNotification() 0 6 1
A id() 0 4 1
A __toString() 0 4 1
A notification() 0 10 3
A applyUserReceivedNotification() 0 9 1
A applyUserReadNotification() 7 10 3
A applyUserUnreadNotification() 7 10 3
A reconstitute() 10 10 2

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;
16
17
use Kreta\SharedKernel\Domain\Model\AggregateRoot;
18
use Kreta\SharedKernel\Domain\Model\EventSourcedAggregateRoot;
19
use Kreta\SharedKernel\Event\EventStream;
20
21
class User extends AggregateRoot implements EventSourcedAggregateRoot
22
{
23
    private $id;
24
    private $notifications;
25
26
    private function __construct(UserId $id)
27
    {
28
        $this->id = $id;
29
        $this->notifications = new Notifications();
30
    }
31
32
    public static function signUp(UserId $id) : self
33
    {
34
        $instance = new self($id);
35
        $instance->publish(new UserSignedUp($id));
36
37
        return $instance;
38
    }
39
40
    public function receiveNotification(NotificationId $notificationId, NotificationBody $body) : void
41
    {
42
        $this->publish(
43
            new UserReceivedNotification(
44
                $this->id,
45
                $notificationId,
46
                $body
47
            )
48
        );
49
    }
50
51
    public function readNotification(Notification $notification) : void
52
    {
53
        $this->publish(
54
            new UserReadNotification($this->id, $notification->id())
55
        );
56
    }
57
58
    public function unreadNotification(Notification $notification) : void
59
    {
60
        $this->publish(
61
            new UserUnreadNotification($this->id, $notification->id())
62
        );
63
    }
64
65
    public function id() : UserId
66
    {
67
        return $this->id;
68
    }
69
70
    public function __toString() : string
71
    {
72
        return (string) $this->id()->id();
73
    }
74
75
    public function notification(NotificationId $notificationId) : Notification
76
    {
77
        foreach ($this->notifications as $notification) {
78
            if ($notificationId->equals($notification->id())) {
79
                return $notification;
80
            }
81
        }
82
83
        throw new NotificationDoesNotExist();
84
    }
85
86
    protected function applyUserReceivedNotification(UserReceivedNotification $event) : void
87
    {
88
        $this->notifications->add(
89
            Notification::broadcast(
90
                $event->notificationId(),
91
                $event->body()
92
            )
93
        );
94
    }
95
96
    protected function applyUserReadNotification(UserReadNotification $event) : void
97
    {
98 View Code Duplication
        foreach ($this->notifications as $key => $notification) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
99
            if ($notification->id()->equals($event->notificationId())) {
100
                $this->notifications->of($key)->read();
101
102
                break;
103
            }
104
        }
105
    }
106
107
    protected function applyUserUnreadNotification(UserUnreadNotification $event) : void
108
    {
109 View Code Duplication
        foreach ($this->notifications as $key => $notification) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
110
            if ($notification->id()->equals($event->notificationId())) {
111
                $this->notifications->of($key)->unread();
112
113
                break;
114
            }
115
        }
116
    }
117
118 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...
119
    {
120
        $receiver = 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\Domain\Model\Inbox\UserId>. 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...
121
        $events = $stream->events()->toArray();
122
        foreach ($events as $event) {
123
            $receiver->apply($event);
124
        }
125
126
        return $receiver;
127
    }
128
}
129