Renamed domain FixtureManagement to Fixture

This commit is contained in:
Kevin Frantz
2019-05-30 16:10:09 +02:00
parent 1e166cf199
commit 8307016460
29 changed files with 54 additions and 54 deletions

View File

@@ -0,0 +1,44 @@
<?php
namespace Infinito\Domain\Fixture;
use Infinito\Entity\Meta\Right;
use Infinito\DBAL\Types\Meta\Right\LayerType;
use Infinito\DBAL\Types\ActionType;
use Infinito\Entity\Source\SourceInterface;
use Infinito\Entity\Meta\RightInterface;
use Infinito\Entity\Meta\LawInterface;
/**
* @author kevinfrantz
*/
final class EntityTemplateFactory extends Right
{
/**
* @param LawInterface $law
* @param RightInterface $right
*/
private static function addRightToLaw(LawInterface $law, RightInterface $right)
{
$right->setLaw($law);
$law->getRights()->add($right);
}
/**
* @param SourceInterface $source
*/
public static function createStandartPublicRights(SourceInterface $source): void
{
$law = $source->getLaw();
$readRight = new Right();
self::addRightToLaw($law, $readRight);
$readRight->setSource($source);
$readRight->setLayer(LayerType::SOURCE);
$readRight->setActionType(ActionType::READ);
$executeRight = new Right();
self::addRightToLaw($law, $executeRight);
$executeRight->setSource($source);
$executeRight->setLayer(LayerType::SOURCE);
$executeRight->setActionType(ActionType::EXECUTE);
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace Infinito\Domain\Fixture\FixtureSource;
/**
* Classes which inhiere from this class and should be loaded by SourceFixtures MUST be declared as final.
*
* @author kevinfrantz
*/
abstract class AbstractFixtureSource implements FixtureSourceInterface
{
/**
* @var string a human readable name
*/
protected $name = null;
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Fixture\FixtureSource\FixtureSourceInterface::getName()
*/
public function getName(): string
{
return $this->name ?? self::getSlug();
}
/**
* @return string
*/
public static function getSlug(): string
{
$className = get_called_class();
$exploded = explode('\\', $className);
$shortname = $exploded[count($exploded) - 1];
$key = str_replace('FixtureSource', '', $shortname);
$lower = strtolower($key);
return $lower;
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Infinito\Domain\Fixture\FixtureSource;
use Infinito\Entity\Source\SourceInterface;
use Infinito\Attribut\SlugAttributInterface;
/**
* This interface allows to save the configuration values of an fixture in a class.
*
* @author kevinfrantz
*/
interface FixtureSourceInterface
{
/**
* @return SourceInterface An source, which can be handled by Doctrine ORM persist
*/
public function getORMReadyObject(): SourceInterface;
/**
* It's necessary for tests and to address the object correct.
*
* @return SlugAttributInterface
*/
public static function getSlug(): string;
/**
* @see https://fontawesome.com
*
* @return string|null a fontawesome css class
*/
public static function getIcon(): string;
/**
* @return string A human readable name, if defined, otherwise a slug
*/
public function getName(): string;
}

View File

@@ -0,0 +1,42 @@
<?php
namespace Infinito\Domain\Fixture\FixtureSource;
use Infinito\Entity\Source\SourceInterface;
use Infinito\Entity\Source\Complex\UserSource;
use Infinito\Domain\Fixture\EntityTemplateFactory;
/**
* This class containes the guest user.
*
* @author kevinfrantz
*/
final class GuestUserFixtureSource extends AbstractFixtureSource
{
/**
* @var string
*/
protected $name = 'guest user';
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Fixture\FixtureSource\FixtureSourceInterface::getORMReadyObject()
*/
public function getORMReadyObject(): SourceInterface
{
$userSource = new UserSource();
$userSource->setSlug(self::getSlug());
EntityTemplateFactory::createStandartPublicRights($userSource);
return $userSource;
}
/**
* @return string
*/
public static function getIcon(): string
{
return 'fas fa-user';
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Infinito\Domain\Fixture\FixtureSource;
use Infinito\Entity\Source\SourceInterface;
use Infinito\Entity\Source\Primitive\Text\TextSource;
use Infinito\Domain\Fixture\EntityTemplateFactory;
/**
* @author kevinfrantz
*/
final class HelpFixtureSource extends AbstractFixtureSource
{
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Fixture\FixtureSource\FixtureSourceInterface::getORMReadyObject()
*/
public function getORMReadyObject(): SourceInterface
{
$helpSource = new TextSource();
$helpSource->setText('See https://github.com/KevinFrantz/infinito/issues.');
$helpSource->setSlug(self::getSlug());
EntityTemplateFactory::createStandartPublicRights($helpSource);
return $helpSource;
}
/**
* @return string
*/
public static function getIcon(): string
{
return 'fas fa-question';
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace Infinito\Domain\Fixture\FixtureSource;
use Infinito\Entity\Source\SourceInterface;
use Infinito\Entity\Source\Primitive\Text\TextSource;
use Infinito\Domain\Fixture\EntityTemplateFactory;
/**
* @author kevinfrantz
*/
final class HomepageFixtureSource extends AbstractFixtureSource
{
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Fixture\FixtureSource\FixtureSourceInterface::getORMReadyObject()
*/
public function getORMReadyObject(): SourceInterface
{
$homepage = new TextSource();
$homepage->setText('Welcome to infinito!');
$homepage->setSlug(self::getSlug());
EntityTemplateFactory::createStandartPublicRights($homepage);
return $homepage;
}
public static function getIcon(): string
{
return 'fas fa-home';
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Infinito\Domain\Fixture\FixtureSource;
use Infinito\Entity\Source\SourceInterface;
use Infinito\Entity\Source\Primitive\Text\TextSource;
use Infinito\Domain\Fixture\EntityTemplateFactory;
/**
* @author kevinfrantz
*/
final class ImpressumFixtureSource extends AbstractFixtureSource
{
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Fixture\FixtureSource\FixtureSourceInterface::getORMReadyObject()
*/
public function getORMReadyObject(): SourceInterface
{
$impressumSource = new TextSource();
$impressumSource->setText('Example Impressum');
$impressumSource->setSlug(self::getSlug());
EntityTemplateFactory::createStandartPublicRights($impressumSource);
return $impressumSource;
}
/**
* @return string
*/
public static function getIcon(): string
{
return 'fas fa-address-card';
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Infinito\Domain\Fixture\FixtureSource;
use Infinito\Entity\Source\SourceInterface;
use Infinito\Entity\Source\Primitive\Text\TextSource;
use Infinito\Domain\Fixture\EntityTemplateFactory;
/**
* @author kevinfrantz
*/
final class InformationFixtureSource extends AbstractFixtureSource
{
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Fixture\FixtureSource\FixtureSourceInterface::getORMReadyObject()
*/
public function getORMReadyObject(): SourceInterface
{
$informationSource = new TextSource();
$informationSource->setText('See https://github.com/KevinFrantz/infinito/issues.');
$informationSource->setSlug(self::getSlug());
EntityTemplateFactory::createStandartPublicRights($informationSource);
return $informationSource;
}
/**
* @return string
*/
public static function getIcon(): string
{
return 'fas fa-info';
}
}

View File

@@ -0,0 +1,2 @@
# Fixture Sources
This folder containes all source class objects which will be load into the database. This structure allows unit tests for the source fixtures.

View File

@@ -0,0 +1,71 @@
<?php
namespace Infinito\Domain\Fixture;
use HaydenPierce\ClassFinder\ClassFinder;
use Infinito\Domain\Fixture\FixtureSource\FixtureSourceInterface;
/**
* @author kevinfrantz
*/
final class FixtureSourceFactory implements FixtureSourceFactoryInterface
{
/**
* @var string Namespace in which the fixture sources are saved
*/
private const FIXTURE_SOURCE_NAMESPACE = 'Infinito\Domain\Fixture\FixtureSource';
/**
* @return array|FixtureSourceInterface[]
*/
private static function getAllClassesInSourceFixtureNamespace(): array
{
return ClassFinder::getClassesInNamespace(self::FIXTURE_SOURCE_NAMESPACE);
}
/**
* @param array $unfilteredClasses|FixtureSourceInterface[]
*
* @return array|FixtureSourceInterface[] Returns just the classes which are final
*/
private static function filterFinal(array $unfilteredClasses): array
{
$filtered = [];
foreach ($unfilteredClasses as $unfilteredClass) {
$unfilteredClassReflection = new \ReflectionClass($unfilteredClass);
if ($unfilteredClassReflection->isFinal()) {
$filtered[] = $unfilteredClass;
}
}
return $filtered;
}
/**
* @param array $classes|FixtureSourceInterface[]
*
* @return array|FixtureSourceInterface[]
*/
private static function loadObjects(array $classes): array
{
$objects = [];
foreach ($classes as $class) {
$object = new $class();
$objects[$object->getSlug()] = $object;
}
return $objects;
}
/**
* @return array
*/
public static function getAllFixtureSources(): array
{
$unfilteredClasses = self::getAllClassesInSourceFixtureNamespace();
$filtered = self::filterFinal($unfilteredClasses);
$objects = self::loadObjects($filtered);
return $objects;
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Infinito\Domain\Fixture;
use Infinito\Domain\Fixture\FixtureSource\FixtureSourceInterface;
/**
* Offers a Factory to produce sources.
*
* @author kevinfrantz
*/
interface FixtureSourceFactoryInterface
{
/**
* @return array|FixtureSourceInterface[] Returns all existing fixture sources
*/
public static function getAllFixtureSources(): array;
}