refactor factory using key=>value for create god instance

This commit is contained in:
Manuel Raynaud
2013-05-21 16:27:56 +02:00
parent c44f9a78a1
commit 4f42e2cef2

View File

@@ -34,6 +34,14 @@ class ActionEventFactory
protected $action;
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)
{
$this->request = $request;
@@ -43,31 +51,28 @@ class ActionEventFactory
public function createActionEvent()
{
$className = "Thelia\\Core\\Event\\".$this->action."Event";
$class = null;
if (class_exists($className)) {
$class = new \ReflectionClass($className);
if (array_key_exists($this->action, $this->className)) {
$class = new \ReflectionClass($this->className[$this->action]);
// return $class->newInstance($this->request, $this->action);
} else {
$actionEventClass = new ActionEventClass($this->action);
$this->dispatcher->dispatch("action.searchClass", $actionEventClass);
if ($actionEventClass->hasClassName()) {
$class = new \ReflectionClass($className);
$class = new \ReflectionClass($actionEventClass->getClassName());
}
}
if( is_null($class)) {
$class = new \ReflectionClass("Thelia\Core\Event\DefaultActionEvent");
if (is_null($class)) {
$class = new \ReflectionClass($this->defaultClassName);
}
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);
}
}