|
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
|
|
|
|
|
20
|
|
|
class Organization extends AggregateRoot |
|
21
|
|
|
{ |
|
22
|
|
|
private $id; |
|
23
|
|
|
private $createdOn; |
|
24
|
|
|
private $members; |
|
25
|
|
|
private $name; |
|
26
|
|
|
private $owners; |
|
27
|
|
|
private $slug; |
|
28
|
|
|
private $updatedOn; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(OrganizationId $id, OrganizationName $name, Slug $slug, Owner $creator) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->id = $id; |
|
33
|
|
|
$this->name = $name; |
|
34
|
|
|
$this->slug = $slug; |
|
35
|
|
|
$this->members = new MemberCollection(); |
|
36
|
|
|
$this->owners = new OwnerCollection(); |
|
37
|
|
|
$this->createdOn = new \DateTimeImmutable(); |
|
38
|
|
|
$this->updatedOn = new \DateTimeImmutable(); |
|
39
|
|
|
$this->addOwner($creator); |
|
40
|
|
|
|
|
41
|
|
|
$this->publish( |
|
42
|
|
|
new OrganizationCreated($id, $name, $slug) |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
View Code Duplication |
public function addMember(Member $member) |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$this->members->add($member); |
|
49
|
|
|
$this->updatedOn = new \DateTimeImmutable(); |
|
50
|
|
|
$this->publish( |
|
51
|
|
|
new MemberAdded($this->id, $member->id()) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function addOwner(Owner $owner) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->owners->add($owner); |
|
58
|
|
|
$this->updatedOn = new \DateTimeImmutable(); |
|
59
|
|
|
$this->publish( |
|
60
|
|
|
new OwnerAdded($this->id, $owner->id()) |
|
|
|
|
|
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function edit(OrganizationName $name, Slug $slug) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->name = $name; |
|
67
|
|
|
$this->slug = $slug; |
|
68
|
|
|
$this->updatedOn = new \DateTimeImmutable(); |
|
69
|
|
|
$this->publish( |
|
70
|
|
|
new OrganizationEdited($this->id, $name, $slug) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
View Code Duplication |
public function removeMember(Member $member) |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
$this->members->remove($member); |
|
77
|
|
|
$this->updatedOn = new \DateTimeImmutable(); |
|
78
|
|
|
$this->publish( |
|
79
|
|
|
new MemberRemoved($this->id, $member->id()) |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function removeOwner(Owner $owner) |
|
84
|
|
|
{ |
|
85
|
|
|
if ($this->owners()->count() === 1) { |
|
86
|
|
|
throw new UnauthorizedRemoveOwnerException(); |
|
87
|
|
|
} |
|
88
|
|
|
$this->owners->remove($owner); |
|
89
|
|
|
$this->updatedOn = new \DateTimeImmutable(); |
|
90
|
|
|
$this->publish( |
|
91
|
|
|
new OwnerRemoved($this->id, $owner->id()) |
|
|
|
|
|
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function isOwner(MemberId $ownerId) : bool |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->owners->exists( |
|
98
|
|
|
function ($key, $element) use ($ownerId) { |
|
99
|
|
|
return $ownerId->equals($element->id()); |
|
100
|
|
|
} |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function isMember(MemberId $memberId) : bool |
|
105
|
|
|
{ |
|
106
|
|
|
$isMember = $this->members->exists( |
|
107
|
|
|
function ($key, $element) use ($memberId) { |
|
108
|
|
|
return $memberId->equals($element->id()); |
|
109
|
|
|
} |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
return $isMember || $this->isOwner($memberId); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function id() : OrganizationId |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->id; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function createdOn() : \DateTimeInterface |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->createdOn; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function members() : MemberCollection |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->members; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function name() : OrganizationName |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->name; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function owners() : OwnerCollection |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->owners; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function slug() : Slug |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->slug; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function updatedOn() : \DateTimeInterface |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->updatedOn; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function __toString() : string |
|
151
|
|
|
{ |
|
152
|
|
|
return (string) $this->id->id(); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
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.