move dispatch during customer creation process in post/preInsert methods

This commit is contained in:
Manuel Raynaud
2013-08-09 10:19:36 +02:00
parent 52ea82ae1b
commit 654220882a
3 changed files with 35 additions and 15 deletions

View File

@@ -69,7 +69,7 @@ class Category extends BaseAction implements EventSubscriberInterface
$successUrl = str_replace('_ID_', $category->getId(), $categoryCreationForm->getSuccessUrl());
// Redirect to the success URL
Redirect::exec($successUrl);
$this->redirect($successUrl);
} catch (PropelException $e) {
Tlog::getInstance()->error(sprintf('error during creating category with message "%s"', $e->getMessage()));

View File

@@ -66,8 +66,7 @@ class Customer extends BaseAction implements EventSubscriberInterface
$data = $form->getData();
$customer = new CustomerModel();
$event->getDispatcher()->dispatch(TheliaEvents::BEFORE_CREATECUSTOMER, $event);
$customer->setDispatcher($event->getDispatcher());
$customer->createOrUpdate(
$data["title"],
@@ -85,8 +84,7 @@ class Customer extends BaseAction implements EventSubscriberInterface
$request->getSession()->getLang()
);
$customerEvent = new CustomerEvent($customer);
$event->getDispatcher()->dispatch(TheliaEvents::AFTER_CREATECUSTOMER, $customerEvent);
// Connect the newly created user,and redirect to the success URL
$this->processSuccessfullLogin($event, $customer, $customerCreationForm, true);
@@ -261,13 +259,25 @@ class Customer extends BaseAction implements EventSubscriberInterface
*/
protected function processSuccessfullLogin(ActionEvent $event, CustomerModel $user, BaseForm $form, $sendLoginEvent = false)
{
// Success -> store user in security context
$this->getSecurityContext()->setUser($user);
$successUrl = $form->getSuccessUrl();
if ($this->securityContext->getContext() === SecurityContext::CONTEXT_FRONT_OFFICE) {
$this->processSuccessfullFrontEndLogin($event, $user, $form, $sendLoginEvent);
} else {
$successUrl = str_replace("__ID__", $user->getId(), $successUrl);
}
// Redirect to the success URL
$this->redirect($successUrl);
}
protected function processSuccessfullFrontEndLogin(ActionEvent $event, CustomerModel $user, BaseForm $form, $sendLoginEvent = false)
{
// Success -> store user in security context
$this->getSecurityContext()->setUser($user);
if ($sendLoginEvent) $event->getDispatcher()->dispatch(TheliaEvents::CUSTOMER_LOGIN, $event);
if ($sendLoginEvent) $event->getDispatcher()->dispatch(TheliaEvents::CUSTOMER_LOGIN, $event);
// Redirect to the success URL
$this->redirect($form->getSuccessUrl());
}
/**
@@ -276,7 +286,7 @@ class Customer extends BaseAction implements EventSubscriberInterface
* @return SecurityContext the security context
*/
protected function getSecurityContext() {
$this->securityContext->setContext(SecurityContext::CONTEXT_FRONT_OFFICE);
//$this->securityContext->setContext(SecurityContext::CONTEXT_FRONT_OFFICE);
return $this->securityContext;
}

View File

@@ -3,6 +3,7 @@
namespace Thelia\Model;
use Symfony\Component\Config\Definition\Exception\Exception;
use Thelia\Core\Event\CustomerEvent;
use Thelia\Model\Base\Customer as BaseCustomer;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
@@ -105,16 +106,25 @@ class Customer extends BaseCustomer implements UserInterface
public function preInsert(ConnectionInterface $con = null)
{
$customerRef = new CustomRefEvent($this);
$this->setRef($this->generateRef());
$customerEvent = new CustomerEvent($this);
if (!is_null($this->dispatcher)) {
$this->dispatcher->dispatch(TheliaEvents::CREATECUSTOMER_CUSTOMREF, $customerRef);
$this->dispatcher->dispatch(TheliaEvents::BEFORE_CREATECUSTOMER, $customerEvent);
}
$this->setRef($customerRef->hasRef()? $customerRef->getRef() : $this->generateRef());
return true;
}
public function postInsert(ConnectionInterface $con = null)
{
$customerEvent = new CustomerEvent($this);
if (!is_null($this->dispatcher)) {
$this->dispatcher->dispatch(TheliaEvents::AFTER_CREATECUSTOMER, $customerEvent);
}
}
protected function generateRef()
{
return uniqid(substr($this->getLastname(), 0, (strlen($this->getLastname()) >= 3) ? 3 : strlen($this->getLastname())), true);