diff --git a/application/src/Domain/SourceManagement/SourceRightManager.php b/application/src/Domain/SourceManagement/SourceRightManager.php new file mode 100644 index 0000000..fb1630b --- /dev/null +++ b/application/src/Domain/SourceManagement/SourceRightManager.php @@ -0,0 +1,86 @@ +source = $source; + } + + /** + * @throws AllreadyDefinedException If the attribut is allready defined + */ + private function checkRightAttributes(RightInterface $right): void + { + $attributes = ['source', 'law']; + foreach ($attributes as $attribut) { + try { + $right->{'get'.ucfirst($attribut)}(); + throw new AllreadyDefinedException("The attribut \"$attribut\" is allready defined!"); + } catch (\Error $error) { + //Expected + } + } + } + + /** + * @return ArrayCollection|RightInterface[] + */ + private function getRights(): ArrayCollection + { + return $this->source->getLaw()->getRights(); + } + + /** + * {@inheritdoc} + * + * @see \App\Domain\SourceManagement\SourceRightManagerInterface::addRight() + */ + public function addRight(RightInterface $right): void + { + if ($this->getRights()->contains($right)) { + throw new AllreadySetException('The right was allready added.'); + } + $this->checkRightAttributes($right); + $right->setSource($this->source); + $right->setLaw($this->source->getLaw()); + $this->getRights()->add($right); + } + + /** + * {@inheritdoc} + * + * @see \App\Domain\SourceManagement\SourceRightManagerInterface::removeRight() + */ + public function removeRight(RightInterface $right): void + { + $right->setSource(new class() extends AbstractSource { + }); + $right->setLaw(new Law()); + if (!$this->getRights()->removeElement($right)) { + throw new NotSetException('The right to remove is not set.'); + } + } +} diff --git a/application/src/Domain/SourceManagement/SourceRightManagerInterface.php b/application/src/Domain/SourceManagement/SourceRightManagerInterface.php new file mode 100644 index 0000000..f17557f --- /dev/null +++ b/application/src/Domain/SourceManagement/SourceRightManagerInterface.php @@ -0,0 +1,26 @@ +source = $this->createSourceMock(); + $this->sourceRightManager = new SourceRightManager($this->source); + $this->right = new Right(); + } + + public function testLawException(): void + { + $this->right->setLaw(new Law()); + $this->expectException(AllreadyDefinedException::class); + $this->sourceRightManager->addRight($this->right); + } + + public function testSourceException(): void + { + $this->right->setSource($this->createSourceMock()); + $this->expectException(AllreadyDefinedException::class); + $this->sourceRightManager->addRight($this->right); + } + + public function testNotSetException(): void + { + $this->expectException(NotSetException::class); + $this->sourceRightManager->removeRight($this->right); + } + + public function testAllreadSetException(): void + { + $this->sourceRightManager->addRight($this->right); + $this->expectException(AllreadySetException::class); + $this->sourceRightManager->addRight($this->right); + } + + public function testRightAdd(): void + { + $this->assertNull($this->sourceRightManager->addRight($this->right)); + $this->assertEquals($this->source, $this->right->getSource()); + $this->assertEquals($this->right, $this->source->getLaw()->getRights()->get(0)); + $this->assertEquals($this->right->getLaw(), $this->source->getLaw()); + $this->assertNull($this->sourceRightManager->removeRight($this->right)); + $this->assertNotEquals($this->source, $this->right->getSource()); + $this->assertNotEquals($this->right->getLaw(), $this->source->getLaw()); + $this->assertEquals(0, $this->source->getLaw()->getRights()->count()); + } +}