|
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\Collection; |
|
18
|
|
|
use Kreta\SharedKernel\Domain\Model\CollectionElementAlreadyRemovedException; |
|
19
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserId; |
|
20
|
|
|
|
|
21
|
|
|
abstract class MemberCollection extends Collection |
|
22
|
|
|
{ |
|
23
|
|
|
public function contains($element) : bool |
|
24
|
|
|
{ |
|
25
|
|
|
return $this->containsUserId($element->userId()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function containsUserId(UserId $userId) : bool |
|
29
|
|
|
{ |
|
30
|
|
|
$members = $this->toArray(); |
|
31
|
|
|
foreach ($members as $member) { |
|
32
|
|
|
if ($userId->equals($member->userId())) { |
|
33
|
|
|
return true; |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return false; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function removeByUserId(UserId $userId) : void |
|
41
|
|
|
{ |
|
42
|
|
|
$members = $this->toArray(); |
|
43
|
|
|
foreach ($members as $member) { |
|
44
|
|
|
if ($userId->equals($member->userId())) { |
|
45
|
|
|
$this->removeMember($member); |
|
46
|
|
|
|
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
throw new CollectionElementAlreadyRemovedException(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function removeMember(Member $member) : void |
|
54
|
|
|
{ |
|
55
|
|
|
$this->remove($member); |
|
56
|
|
|
|
|
57
|
|
|
// This line it's a hack for Doctrine ORM |
|
58
|
|
|
// to enforce the bidirectional relationship |
|
59
|
|
|
// between member and organization. |
|
60
|
|
|
$memberReflection = new \ReflectionClass($member); |
|
61
|
|
|
$property = $memberReflection->getProperty('organization'); |
|
62
|
|
|
$property->setAccessible(true); |
|
63
|
|
|
$property->setValue($member, null); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|