infinito/application/symfony/src/Domain/Action/ActionFactoryService.php

85 lines
2.2 KiB
PHP
Raw Normal View History

2019-01-27 15:28:25 +01:00
<?php
namespace Infinito\Domain\Action;
2019-01-27 15:28:25 +01:00
use Infinito\Exception\NoDefaultClassException;
2019-01-27 18:38:21 +01:00
2019-01-27 15:28:25 +01:00
/**
* @author kevinfrantz
*/
final class ActionFactoryService extends AbstractActionConstructor implements ActionFactoryServiceInterface
{
2019-04-14 13:02:31 +02:00
/**
* @var string Namespace in which the actions will be found
*/
private const BASE_NAMESPACE = 'Infinito\\Domain\\Action\\';
2019-01-27 15:28:25 +01:00
2019-04-14 13:02:31 +02:00
/**
* @var string Suffix for action classes
*/
private const CLASS_SUFFIX = 'Action';
2019-01-27 15:28:25 +01:00
/**
* @param string $name
*
* @return string
*/
private function ucfirst(string $name): string
{
return ucfirst(strtolower($name));
}
/**
* @param string $action
* @param string $layer
*
* @return string
*/
private function getClassName(string $action, string $layer = ''): string
{
return $this->ucfirst($action).$this->ucfirst($layer).self::CLASS_SUFFIX;
}
/**
* @param string $layer
* @param string $action
*
* @return string
*/
private function getActionNamespace(string $action, string $layer = ''): string
{
return self::BASE_NAMESPACE.$this->ucfirst($action).'\\'.$this->getClassName($action, $layer);
}
/**
* @return string
*/
private function generateFullClassName(): string
{
$requestedAction = $this->actionService->getRequestedAction();
$action = $requestedAction->getActionType();
$layer = $requestedAction->getLayer();
$class = $this->getActionNamespace($action, $layer);
if (class_exists($class)) {
return $class;
}
$defaultClass = $this->getActionNamespace($action);
2019-01-27 18:38:21 +01:00
if (class_exists($defaultClass)) {
return $defaultClass;
}
throw new NoDefaultClassException("There is no default substitution class for $class with attributes {layer:\"$layer\",action:\"$action\"}");
2019-01-27 15:28:25 +01:00
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\ActionFactoryServiceInterface::create()
2019-01-27 15:28:25 +01:00
*/
public function create(): ActionInterface
{
$class = $this->generateFullClassName();
return new $class($this->actionService);
}
}