mirror of
				https://github.com/kevinveenbirkenbach/infinito.git
				synced 2025-11-04 03:07:58 +00:00 
			
		
		
		
	Implemented FixtureManagement
This commit is contained in:
		@@ -3,6 +3,8 @@
 | 
			
		||||
namespace App\DBAL\Types;
 | 
			
		||||
 | 
			
		||||
use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
 | 
			
		||||
use App\Domain\FixtureManagement\FixtureSource\ImpressumFixtureSource;
 | 
			
		||||
use App\Domain\FixtureManagement\FixtureSource\GuestUserFixtureSource;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Containes the system slugs.
 | 
			
		||||
@@ -15,10 +17,23 @@ use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
 | 
			
		||||
 */
 | 
			
		||||
final class SystemSlugType extends AbstractEnumType
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @deprecated
 | 
			
		||||
     * @see ImpressumFixtureSource
 | 
			
		||||
     */
 | 
			
		||||
    public const IMPRINT = 'IMPRINT';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @deprecated
 | 
			
		||||
     * @see GuestUserFixtureSource
 | 
			
		||||
     */
 | 
			
		||||
    public const GUEST_USER = 'GUEST_USER';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @deprecated
 | 
			
		||||
     *
 | 
			
		||||
     * @var array
 | 
			
		||||
     */
 | 
			
		||||
    protected static $choices = [
 | 
			
		||||
        self::IMPRINT => 'imprint',
 | 
			
		||||
        self::GUEST_USER => 'guest user',
 | 
			
		||||
 
 | 
			
		||||
@@ -4,34 +4,13 @@ namespace App\DataFixtures;
 | 
			
		||||
 | 
			
		||||
use Doctrine\Bundle\FixturesBundle\Fixture;
 | 
			
		||||
use Doctrine\Common\Persistence\ObjectManager;
 | 
			
		||||
use App\Entity\Source\Primitive\Text\TextSource;
 | 
			
		||||
use App\Entity\Source\Primitive\Text\TextSourceInterface;
 | 
			
		||||
use App\DBAL\Types\SystemSlugType;
 | 
			
		||||
use App\Entity\Source\Complex\UserSource;
 | 
			
		||||
use App\Entity\Source\Complex\UserSourceInterface;
 | 
			
		||||
use App\Entity\Meta\Right;
 | 
			
		||||
use App\DBAL\Types\Meta\Right\LayerType;
 | 
			
		||||
use App\DBAL\Types\Meta\Right\CRUDType;
 | 
			
		||||
use App\Entity\Meta\RightInterface;
 | 
			
		||||
use App\Domain\SourceManagement\SourceRightManager;
 | 
			
		||||
use App\Domain\FixtureManagement\FixtureSourceFactory;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author kevinfrantz
 | 
			
		||||
 *
 | 
			
		||||
 * @todo Create a collection class for all users
 | 
			
		||||
 */
 | 
			
		||||
class SourceFixtures extends Fixture
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @var TextSourceInterface The example source for the impressum
 | 
			
		||||
     */
 | 
			
		||||
    private $impressumSource;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @var UserSourceInterface The UserSource which should be used for the anonymous user
 | 
			
		||||
     */
 | 
			
		||||
    private $guestUserSource;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * {@inheritdoc}
 | 
			
		||||
     *
 | 
			
		||||
@@ -39,41 +18,10 @@ class SourceFixtures extends Fixture
 | 
			
		||||
     */
 | 
			
		||||
    public function load(ObjectManager $manager)
 | 
			
		||||
    {
 | 
			
		||||
        $this->setGuestUserSource();
 | 
			
		||||
        $this->setImpressumSource();
 | 
			
		||||
        $manager->persist($this->impressumSource);
 | 
			
		||||
        $manager->persist($this->getImpressumRight());
 | 
			
		||||
        $manager->persist($this->guestUserSource);
 | 
			
		||||
        $fixtureSources = FixtureSourceFactory::getAllFixtureSources();
 | 
			
		||||
        foreach ($fixtureSources as $fixtureSource) {
 | 
			
		||||
            $manager->persist($fixtureSource->getORMReadyObject());
 | 
			
		||||
        }
 | 
			
		||||
        $manager->flush();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function setImpressumSource(): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->impressumSource = new TextSource();
 | 
			
		||||
        $this->impressumSource->setText('Example Impressum');
 | 
			
		||||
        $this->impressumSource->setSlug(SystemSlugType::IMPRINT);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @todo Implement that right gets automaticly created by persisting of law
 | 
			
		||||
     *
 | 
			
		||||
     * @return RightInterface
 | 
			
		||||
     */
 | 
			
		||||
    private function getImpressumRight(): RightInterface
 | 
			
		||||
    {
 | 
			
		||||
        $right = new Right();
 | 
			
		||||
        $sourceRightManager = new SourceRightManager($this->impressumSource);
 | 
			
		||||
        $sourceRightManager->addRight($right);
 | 
			
		||||
        $right->setLayer(LayerType::SOURCE);
 | 
			
		||||
        $right->setCrud(CRUDType::READ);
 | 
			
		||||
        $right->setReciever($this->guestUserSource);
 | 
			
		||||
 | 
			
		||||
        return $right;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function setGuestUserSource(): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->guestUserSource = new UserSource();
 | 
			
		||||
        $this->guestUserSource->setSlug(SystemSlugType::GUEST_USER);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,12 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Domain\FixtureManagement\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
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,26 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Domain\FixtureManagement\FixtureSource;
 | 
			
		||||
 | 
			
		||||
use App\Entity\Source\SourceInterface;
 | 
			
		||||
use App\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;
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,37 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Domain\FixtureManagement\FixtureSource;
 | 
			
		||||
 | 
			
		||||
use App\Entity\Source\SourceInterface;
 | 
			
		||||
use App\Entity\Source\Complex\UserSource;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This class containes the guest user.
 | 
			
		||||
 *
 | 
			
		||||
 * @author kevinfrantz
 | 
			
		||||
 */
 | 
			
		||||
final class GuestUserFixtureSource extends AbstractFixtureSource
 | 
			
		||||
{
 | 
			
		||||
    const SLUG = 'GUEST_USER';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * {@inheritdoc}
 | 
			
		||||
     *
 | 
			
		||||
     * @see \App\Domain\FixtureManagement\FixtureSource\FixtureSourceInterface::getORMReadyObject()
 | 
			
		||||
     */
 | 
			
		||||
    public function getORMReadyObject(): SourceInterface
 | 
			
		||||
    {
 | 
			
		||||
        $userSource = new UserSource();
 | 
			
		||||
        $userSource->setSlug(self::SLUG);
 | 
			
		||||
 | 
			
		||||
        return $userSource;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return string
 | 
			
		||||
     */
 | 
			
		||||
    public static function getSlug(): string
 | 
			
		||||
    {
 | 
			
		||||
        return self::SLUG;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,36 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Domain\FixtureManagement\FixtureSource;
 | 
			
		||||
 | 
			
		||||
use App\Entity\Source\SourceInterface;
 | 
			
		||||
use App\Entity\Source\Primitive\Text\TextSource;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author kevinfrantz
 | 
			
		||||
 */
 | 
			
		||||
final class ImpressumFixtureSource extends AbstractFixtureSource
 | 
			
		||||
{
 | 
			
		||||
    const SLUG = 'IMPRINT';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * {@inheritdoc}
 | 
			
		||||
     *
 | 
			
		||||
     * @see \App\Domain\FixtureManagement\FixtureSource\FixtureSourceInterface::getORMReadyObject()
 | 
			
		||||
     */
 | 
			
		||||
    public function getORMReadyObject(): SourceInterface
 | 
			
		||||
    {
 | 
			
		||||
        $impressumSource = new TextSource();
 | 
			
		||||
        $impressumSource->setText('Example Impressum');
 | 
			
		||||
        $impressumSource->setSlug(self::SLUG);
 | 
			
		||||
 | 
			
		||||
        return $impressumSource;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return string
 | 
			
		||||
     */
 | 
			
		||||
    public static function getSlug(): string
 | 
			
		||||
    {
 | 
			
		||||
        return self::SLUG;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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.
 | 
			
		||||
@@ -0,0 +1,64 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Domain\FixtureManagement;
 | 
			
		||||
 | 
			
		||||
use HaydenPierce\ClassFinder\ClassFinder;
 | 
			
		||||
use App\Domain\FixtureManagement\FixtureSource\FixtureSourceInterface;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author kevinfrantz
 | 
			
		||||
 */
 | 
			
		||||
final class FixtureSourceFactory implements FixtureSourceFactoryInterface
 | 
			
		||||
{
 | 
			
		||||
    const FIXTURE_SOURCE_NAMESPACE = 'App\Domain\FixtureManagement\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) {
 | 
			
		||||
            $objects[] = new $class();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $objects;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static function getAllFixtureSources(): array
 | 
			
		||||
    {
 | 
			
		||||
        $unfilteredClasses = self::getAllClassesInSourceFixtureNamespace();
 | 
			
		||||
        $filtered = self::filterFinal($unfilteredClasses);
 | 
			
		||||
        $objects = self::loadObjects($filtered);
 | 
			
		||||
 | 
			
		||||
        return $objects;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,18 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Domain\FixtureManagement;
 | 
			
		||||
 | 
			
		||||
use App\Domain\FixtureManagement\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;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user