From 84c3b37abb8616d007a7cb4baf5b82a47d445ea5 Mon Sep 17 00:00:00 2001 From: Philipp Cordes Date: Wed, 30 Oct 2024 20:25:30 +0100 Subject: [PATCH] WIP: Zwischenstand --- webservice/src/Core/.gitignore | 0 .../Model/AbstractMessageRecipient.php | 50 +++++++++++ .../Core/MessageRecipient/Model/Delegated.php | 15 ++++ .../src/Core/MessageRecipient/Model/Group.php | 77 +++++++++++++++++ .../Model/MessageRecipient.php | 15 ++++ .../Core/MessageRecipient/Model/Person.php | 39 +++++++++ .../src/Core/MessageRecipient/Model/Role.php | 38 +++++++++ .../Model/AbstractMessageRecipientTest.php | 42 ++++++++++ .../MessageRecipient/Model/PersonTest.php | 42 ++++++++++ .../Core/MessageRecipient/Model/RoleTest.php | 82 +++++++++++++++++++ 10 files changed, 400 insertions(+) delete mode 100644 webservice/src/Core/.gitignore create mode 100644 webservice/src/Core/MessageRecipient/Model/AbstractMessageRecipient.php create mode 100644 webservice/src/Core/MessageRecipient/Model/Delegated.php create mode 100644 webservice/src/Core/MessageRecipient/Model/Group.php create mode 100644 webservice/src/Core/MessageRecipient/Model/MessageRecipient.php create mode 100644 webservice/src/Core/MessageRecipient/Model/Person.php create mode 100644 webservice/src/Core/MessageRecipient/Model/Role.php create mode 100644 webservice/tests/Core/MessageRecipient/Model/AbstractMessageRecipientTest.php create mode 100644 webservice/tests/Core/MessageRecipient/Model/PersonTest.php create mode 100644 webservice/tests/Core/MessageRecipient/Model/RoleTest.php diff --git a/webservice/src/Core/.gitignore b/webservice/src/Core/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/webservice/src/Core/MessageRecipient/Model/AbstractMessageRecipient.php b/webservice/src/Core/MessageRecipient/Model/AbstractMessageRecipient.php new file mode 100644 index 0000000..24b7775 --- /dev/null +++ b/webservice/src/Core/MessageRecipient/Model/AbstractMessageRecipient.php @@ -0,0 +1,50 @@ + Group::class, + Person::DISCRIMINATOR => Person::class, + Role::DISCRIMINATOR => Role::class, +])] +abstract class AbstractMessageRecipient implements MessageRecipient +{ + #[ORM\Column(type: UlidType::NAME)] + #[ORM\Id] + public readonly Ulid $id; + + #[ORM\Column] + public string $name; + + /** + * @var Collection + */ + #[ORM\ManyToMany(Group::class, mappedBy: 'members')] + private Collection $groups; + + public function __construct(string $name, ?Ulid $id = null) + { + $this->id = $id ?? new Ulid(); + $this->name = $name; + $this->groups = new ArrayCollection(); + } + + /** + * @return list + */ + public function getGroups(): array + { + return $this->groups->getValues(); + } +} diff --git a/webservice/src/Core/MessageRecipient/Model/Delegated.php b/webservice/src/Core/MessageRecipient/Model/Delegated.php new file mode 100644 index 0000000..0cc31ab --- /dev/null +++ b/webservice/src/Core/MessageRecipient/Model/Delegated.php @@ -0,0 +1,15 @@ + + */ + public function resolve(): array; +} diff --git a/webservice/src/Core/MessageRecipient/Model/Group.php b/webservice/src/Core/MessageRecipient/Model/Group.php new file mode 100644 index 0000000..b148136 --- /dev/null +++ b/webservice/src/Core/MessageRecipient/Model/Group.php @@ -0,0 +1,77 @@ + + */ + #[ORM\ManyToMany(AbstractMessageRecipient::class, inversedBy: 'groups')] + private Collection $members; + + public function __construct(string $name, ?Ulid $id = null) + { + parent::__construct($name, $id); + + $this->members = new ArrayCollection(); + } + + public function addMember(AbstractMessageRecipient $member): void + { + $this->members->add($member); + } + + public function removeMember(AbstractMessageRecipient $member): void + { + $this->members->removeElement($member); + } + + /** + * @return list + */ + public function getMembers(): array + { + return $this->members->getValues(); + } + + /** + * @return Traversable + */ + public function getMembersRecursively(): Traversable + { + foreach ($this->members as $member) { + if ($member instanceof self) { + yield from $member->getMembersRecursively(); + } else { + yield $member; + } + } + } + + public function canResolve(): bool + { + return !$this->members->isEmpty(); + } + + public function resolve(): array + { + if ($this->members->isEmpty()) { + throw new LogicException(); + } + + return $this->members->getValues(); + } +} diff --git a/webservice/src/Core/MessageRecipient/Model/MessageRecipient.php b/webservice/src/Core/MessageRecipient/Model/MessageRecipient.php new file mode 100644 index 0000000..b84d782 --- /dev/null +++ b/webservice/src/Core/MessageRecipient/Model/MessageRecipient.php @@ -0,0 +1,15 @@ + + */ + #[ORM\OneToMany(Role::class, mappedBy: 'person')] + private Collection $roles; + + public function __construct( + string $name, + ?Ulid $id = null, + ) { + parent::__construct($name, $id); + + $this->roles = new ArrayCollection(); + } + + /** + * @return list + */ + public function getRoles(): array + { + return $this->roles->getValues(); + } +} diff --git a/webservice/src/Core/MessageRecipient/Model/Role.php b/webservice/src/Core/MessageRecipient/Model/Role.php new file mode 100644 index 0000000..d9a5dd5 --- /dev/null +++ b/webservice/src/Core/MessageRecipient/Model/Role.php @@ -0,0 +1,38 @@ +person; + } + + public function resolve(): array + { + if (null === $this->person) { + throw new LogicException(); + } + + return [$this->person]; + } +} diff --git a/webservice/tests/Core/MessageRecipient/Model/AbstractMessageRecipientTest.php b/webservice/tests/Core/MessageRecipient/Model/AbstractMessageRecipientTest.php new file mode 100644 index 0000000..9de2734 --- /dev/null +++ b/webservice/tests/Core/MessageRecipient/Model/AbstractMessageRecipientTest.php @@ -0,0 +1,42 @@ +id->toString(); + } else { + self::assertSame($id, $recipient->id); + } + self::assertSame($name, $recipient->name); + self::assertSame([], $recipient->getGroups()); + } +} diff --git a/webservice/tests/Core/MessageRecipient/Model/PersonTest.php b/webservice/tests/Core/MessageRecipient/Model/PersonTest.php new file mode 100644 index 0000000..1f78b72 --- /dev/null +++ b/webservice/tests/Core/MessageRecipient/Model/PersonTest.php @@ -0,0 +1,42 @@ +id->toString(); + } else { + self::assertSame($id, $person->id); + } + self::assertSame($name, $person->name); + self::assertSame([], $person->getRoles()); + } +} diff --git a/webservice/tests/Core/MessageRecipient/Model/RoleTest.php b/webservice/tests/Core/MessageRecipient/Model/RoleTest.php new file mode 100644 index 0000000..fd12a82 --- /dev/null +++ b/webservice/tests/Core/MessageRecipient/Model/RoleTest.php @@ -0,0 +1,82 @@ +id->toString(); + } else { + self::assertSame($id, $role->id); + } + self::assertSame($name, $role->name); + self::assertSame($person, $role->person); + } + + /** + * @return array{0: bool, 1: ?Person}[] + */ + public static function canResolveProvider(): array + { + return [ + [true, new Person('Bob')], + [false, null], + ]; + } + + #[DataProvider('canResolveProvider')] + public function testCanResolve(bool $expected, ?Person $person): void + { + $role = new Role('Important', $person); + + self::assertSame($expected, $role->canResolve()); + } + + public function testResolve(): void + { + $person = new Person('Eve'); + $role = new Role('Somebody', $person); + + $result = $role->resolve(); + + self::assertSame([$person], $result); + } + + public function testResolveException(): void + { + $role = new Role('Nobody', null); + + self::expectException(LogicException::class); + + $role->resolve(); + } +}