refactor factory using key=>value for create god instance
This commit is contained in:
@@ -34,6 +34,14 @@ class ActionEventFactory
|
|||||||
protected $action;
|
protected $action;
|
||||||
protected $dispatcher;
|
protected $dispatcher;
|
||||||
|
|
||||||
|
protected $className = array(
|
||||||
|
"addArticle" => "Thelia\Core\Event\CartEvent",
|
||||||
|
"deleteArticle" => "Thelia\Core\Event\CartEvent",
|
||||||
|
"modifyArticle" => "Thelia\Core\Event\CartEvent",
|
||||||
|
);
|
||||||
|
|
||||||
|
protected $defaultClassName = "Thelia\Core\Event\DefaultActionEvent";
|
||||||
|
|
||||||
public function __construct(Request $request, $action, EventDispatcherInterface $dispatcher)
|
public function __construct(Request $request, $action, EventDispatcherInterface $dispatcher)
|
||||||
{
|
{
|
||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
@@ -43,31 +51,28 @@ class ActionEventFactory
|
|||||||
|
|
||||||
public function createActionEvent()
|
public function createActionEvent()
|
||||||
{
|
{
|
||||||
$className = "Thelia\\Core\\Event\\".$this->action."Event";
|
|
||||||
$class = null;
|
$class = null;
|
||||||
if (class_exists($className)) {
|
if (array_key_exists($this->action, $this->className)) {
|
||||||
$class = new \ReflectionClass($className);
|
$class = new \ReflectionClass($this->className[$this->action]);
|
||||||
|
|
||||||
// return $class->newInstance($this->request, $this->action);
|
// return $class->newInstance($this->request, $this->action);
|
||||||
} else {
|
} else {
|
||||||
$actionEventClass = new ActionEventClass($this->action);
|
$actionEventClass = new ActionEventClass($this->action);
|
||||||
$this->dispatcher->dispatch("action.searchClass", $actionEventClass);
|
$this->dispatcher->dispatch("action.searchClass", $actionEventClass);
|
||||||
|
|
||||||
if ($actionEventClass->hasClassName()) {
|
if ($actionEventClass->hasClassName()) {
|
||||||
$class = new \ReflectionClass($className);
|
$class = new \ReflectionClass($actionEventClass->getClassName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if( is_null($class)) {
|
if (is_null($class)) {
|
||||||
$class = new \ReflectionClass("Thelia\Core\Event\DefaultActionEvent");
|
$class = new \ReflectionClass($this->defaultClassName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($class->isSubclassOf("Thelia\Core\Event\ActionEvent") === false) {
|
if ($class->isSubclassOf("Thelia\Core\Event\ActionEvent") === false) {
|
||||||
|
throw new \RuntimeException("%s must be a subclass of Thelia\Core\Event\ActionEvent", $class->getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
return $class->newInstance($this->request, $this->action);
|
return $class->newInstance($this->request, $this->action);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user