Implemented SourceRightManager

This commit is contained in:
Kevin Frantz 2019-01-03 20:57:39 +01:00
parent 67428c2fea
commit b39e7c25d4
5 changed files with 213 additions and 0 deletions

View File

@ -0,0 +1,86 @@
<?php
namespace App\Domain\SourceManagement;
use App\Entity\Meta\RightInterface;
use App\Entity\Source\SourceInterface;
use App\Exception\AllreadySetException;
use App\Entity\Source\AbstractSource;
use App\Entity\Meta\Law;
use App\Exception\AllreadyDefinedException;
use App\Exception\NotSetException;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @author kevinfrantz
*/
final class SourceRightManager implements SourceRightManagerInterface
{
/**
* @var SourceInterface
*/
private $source;
/**
* @param SourceInterface $source
*/
public function __construct(SourceInterface $source)
{
$this->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.');
}
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace App\Domain\SourceManagement;
use App\Entity\Meta\RightInterface;
use App\Exception\AllreadySetException;
use App\Exception\AllreadyDefinedException;
use App\Exception\NotSetException;
interface SourceRightManagerInterface
{
/**
* @param RightInterface $right
*
* @throws AllreadySetException
* @throws AllreadyDefinedException
*/
public function addRight(RightInterface $right): void;
/**
* @param RightInterface $right
*
* @throws NotSetException
*/
public function removeRight(RightInterface $right): void;
}

View File

@ -0,0 +1,7 @@
<?php
namespace App\Exception;
final class AllreadyDefinedException extends \Exception
{
}

View File

@ -0,0 +1,9 @@
<?php
namespace App\Exception;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
final class NotSetException extends ConflictHttpException
{
}

View File

@ -0,0 +1,85 @@
<?php
namespace Unit\Domain\SourceManagement;
use PHPUnit\Framework\TestCase;
use App\Entity\Source\SourceInterface;
use App\Domain\SourceManagement\SourceRightManagerInterface;
use App\Entity\Source\AbstractSource;
use App\Domain\SourceManagement\SourceRightManager;
use App\Entity\Meta\RightInterface;
use App\Entity\Meta\Right;
use App\Entity\Meta\Law;
use App\Exception\AllreadySetException;
use App\Exception\NotSetException;
use App\Exception\AllreadyDefinedException;
class SourceRightManagerTest extends TestCase
{
/**
* @var SourceInterface
*/
private $source;
/**
* @var SourceRightManagerInterface
*/
private $sourceRightManager;
/**
* @var RightInterface
*/
private $right;
private function createSourceMock()
{
return new class() extends AbstractSource {
};
}
public function setUp(): void
{
$this->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());
}
}