Completed
Pull Request — master (#172)
by Gorka
05:15
created

Organization::owners()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\TaskManager\Domain\Model\Organization;
16
17
use Kreta\SharedKernel\Domain\Model\AggregateRoot;
18
use Kreta\SharedKernel\Domain\Model\Identity\Slug;
19
use Kreta\TaskManager\Domain\Model\User\UserId;
20
21
class Organization extends AggregateRoot
22
{
23
    private $id;
24
    private $createdOn;
25
    private $organizationMembers;
26
    private $name;
27
    private $owners;
28
    private $slug;
29
    private $updatedOn;
30
31
    public function __construct(OrganizationId $id, OrganizationName $name, Slug $slug, UserId $userId)
32
    {
33
        $this->id = $id;
34
        $this->name = $name;
35
        $this->slug = $slug;
36
        $this->organizationMembers = new OrganizationMemberCollection();
37
        $this->owners = new OwnerCollection();
38
        $this->createdOn = new \DateTimeImmutable();
39
        $this->updatedOn = new \DateTimeImmutable();
40
        $this->addOwner($userId);
41
42
        $this->publish(
43
            new OrganizationCreated($id, $name, $slug)
44
        );
45
    }
46
47
    public function addOrganizationMember(UserId $userId)
48
    {
49
        if ($this->isOwner($userId)) {
50
            throw new OrganizationMemberIsAlreadyAnOwnerException($userId);
51
        }
52
        $organizationMember = new OrganizationMember(OrganizationMemberId::generate(), $userId, $this);
53
        $this->organizationMembers->add($organizationMember);
54
        $this->updatedOn = new \DateTimeImmutable();
55
        $this->publish(
56
            new OrganizationMemberAdded($organizationMember->id(), $userId, $this->id)
0 ignored issues
show
Compatibility introduced by
$organizationMember->id() of type object<Kreta\TaskManager...\Organization\MemberId> is not a sub-type of object<Kreta\TaskManager...n\OrganizationMemberId>. It seems like you assume a child class of the class Kreta\TaskManager\Domain...l\Organization\MemberId 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...
57
        );
58
    }
59
60
    public function addOwner(UserId $userId)
61
    {
62
        if (!$this->isOwner($userId) && $this->isOrganizationMember($userId)) {
63
            $this->removeOrganizationMember($userId);
64
        }
65
        $owner = new Owner(OwnerId::generate(), $userId, $this);
66
        $this->owners->add($owner);
67
        $this->updatedOn = new \DateTimeImmutable();
68
        $this->publish(
69
            new OwnerAdded($owner->id(), $userId, $this->id)
0 ignored issues
show
Compatibility introduced by
$owner->id() of type object<Kreta\TaskManager...\Organization\MemberId> is not a sub-type of object<Kreta\TaskManager...l\Organization\OwnerId>. It seems like you assume a child class of the class Kreta\TaskManager\Domain...l\Organization\MemberId 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...
70
        );
71
    }
72
73
    public function edit(OrganizationName $name, Slug $slug)
74
    {
75
        $this->name = $name;
76
        $this->slug = $slug;
77
        $this->updatedOn = new \DateTimeImmutable();
78
        $this->publish(
79
            new OrganizationEdited($this->id, $name, $slug)
80
        );
81
    }
82
83
    public function removeOrganizationMember(UserId $userId)
84
    {
85
        $this->organizationMembers->removeByUserId($userId);
86
        $this->updatedOn = new \DateTimeImmutable();
87
88
        $this->publish(new OrganizationMemberRemoved($this->id, $userId));
89
    }
90
91
    public function removeOwner(UserId $userId)
92
    {
93
        if ($this->owners()->count() === 1) {
94
            throw new UnauthorizedRemoveOwnerException();
95
        }
96
        $this->owners->removeByUserId($userId);
97
        $this->updatedOn = new \DateTimeImmutable();
98
99
        $this->publish(new OwnerRemoved($this->id, $userId));
100
    }
101
102
    public function isOwner(UserId $userId) : bool
103
    {
104
        return $this->owners()->containsUserId($userId);
105
    }
106
107
    public function isOrganizationMember(UserId $userId) : bool
108
    {
109
        return $this->organizationMembers()->containsUserId($userId) || $this->isOwner($userId);
110
    }
111
112
    public function id() : OrganizationId
113
    {
114
        return $this->id;
115
    }
116
117
    public function createdOn() : \DateTimeInterface
118
    {
119
        return $this->createdOn;
120
    }
121
122
    public function organizationMembers()
123
    {
124
        return new OrganizationMemberCollection($this->organizationMembers->getValues());
125
    }
126
127
    public function organizationMember(UserId $userId)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
128
    {
129
        foreach ($this->organizationMembers() as $member) {
130
            if ($userId->equals($member->userId())) {
131
                return $member;
132
            }
133
        }
134
135
        return $this->owner($userId);
136
    }
137
138
    public function name() : OrganizationName
139
    {
140
        return $this->name;
141
    }
142
143
    public function owners() : OwnerCollection
144
    {
145
        return new OwnerCollection($this->owners->getValues());
146
    }
147
148
    public function owner(UserId $userId)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
149
    {
150
        foreach ($this->owners() as $owner) {
151
            if ($userId->equals($owner->userId())) {
152
                return $owner;
153
            }
154
        }
155
    }
156
157
    public function slug() : Slug
158
    {
159
        return $this->slug;
160
    }
161
162
    public function updatedOn() : \DateTimeInterface
163
    {
164
        return $this->updatedOn;
165
    }
166
167
    public function __toString() : string
168
    {
169
        return (string) $this->id->id();
170
    }
171
}
172