|
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) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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
|
|
|
$this->organizationMembers = new OrganizationMemberCollection($this->organizationMembers->getValues()); |
|
125
|
|
|
|
|
126
|
|
|
return $this->organizationMembers; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function organizationMember(UserId $userId) |
|
|
|
|
|
|
130
|
|
|
{ |
|
131
|
|
|
foreach ($this->organizationMembers() as $member) { |
|
132
|
|
|
if ($userId->equals($member->userId())) { |
|
133
|
|
|
return $member; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $this->owner($userId); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function name() : OrganizationName |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->name; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function owners() : OwnerCollection |
|
146
|
|
|
{ |
|
147
|
|
|
$this->owners = new OwnerCollection($this->owners->getValues()); |
|
148
|
|
|
|
|
149
|
|
|
return $this->owners; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function owner(UserId $userId) : ?Owner |
|
|
|
|
|
|
153
|
|
|
{ |
|
154
|
|
|
foreach ($this->owners() as $owner) { |
|
155
|
|
|
if ($userId->equals($owner->userId())) { |
|
156
|
|
|
return $owner; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return null; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function slug() : Slug |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->slug; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function updatedOn() : \DateTimeInterface |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->updatedOn; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function __toString() : string |
|
174
|
|
|
{ |
|
175
|
|
|
return (string) $this->id->id(); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
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.