refactor action customer
This commit is contained in:
@@ -48,6 +48,22 @@ class Customer extends BaseAction implements EventSubscriberInterface
|
||||
$customer = new CustomerModel();
|
||||
$customer->setDispatcher($this->getDispatcher());
|
||||
|
||||
$this->createOrUpdateCustomer($customer, $event);
|
||||
|
||||
}
|
||||
|
||||
public function modify(CustomerCreateOrUpdateEvent $event)
|
||||
{
|
||||
|
||||
$customer = $event->getCustomer();
|
||||
$customer->setDispatcher($this->getDispatcher());
|
||||
|
||||
$this->createOrUpdateCustomer($customer, $event);
|
||||
|
||||
}
|
||||
|
||||
private function createOrUpdateCustomer(CustomerModel $customer, CustomerCreateOrUpdateEvent $event)
|
||||
{
|
||||
$customer->createOrUpdate(
|
||||
$event->getTitle(),
|
||||
$event->getFirstname(),
|
||||
@@ -71,51 +87,6 @@ class Customer extends BaseAction implements EventSubscriberInterface
|
||||
$event->setCustomer($customer);
|
||||
}
|
||||
|
||||
public function modify(ActionEvent $event)
|
||||
{
|
||||
$request = $event->getRequest();
|
||||
|
||||
try {
|
||||
$customerModification = new CustomerModification($request);
|
||||
|
||||
$form = $this->validateForm($customerModification, "POST");
|
||||
|
||||
$data = $form->getData();
|
||||
|
||||
$customer = CustomerQuery::create()->findPk(1);
|
||||
|
||||
$data = $form->getData();
|
||||
|
||||
$customer->createOrUpdate(
|
||||
$data["title"],
|
||||
$data["firstname"],
|
||||
$data["lastname"],
|
||||
$data["address1"],
|
||||
$data["address2"],
|
||||
$data["address3"],
|
||||
$data["phone"],
|
||||
$data["cellphone"],
|
||||
$data["zipcode"],
|
||||
$data["country"]
|
||||
);
|
||||
|
||||
// Update the logged-in user, and redirect to the success URL (exits)
|
||||
// We don-t send the login event, as the customer si already logged.
|
||||
$this->processSuccessfullLogin($event, $customer, $customerModification);
|
||||
} catch (PropelException $e) {
|
||||
|
||||
Tlog::getInstance()->error(sprintf('error during modifying customer on action/modifyCustomer with message "%s"', $e->getMessage()));
|
||||
|
||||
$message = "Failed to change your account, please try again.";
|
||||
} catch (FormValidationException $e) {
|
||||
|
||||
$message = $e->getMessage();
|
||||
}
|
||||
|
||||
// The form has errors, propagate it.
|
||||
$this->propagateFormError($customerModification, $message, $event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform user logout. The user is redirected to the provided view, if any.
|
||||
*
|
||||
@@ -147,7 +118,7 @@ class Customer extends BaseAction implements EventSubscriberInterface
|
||||
try {
|
||||
$user = $authenticator->getAuthentifiedUser();
|
||||
|
||||
$event->customer = $customer;
|
||||
$event->customer = $user;
|
||||
|
||||
} catch (ValidatorException $ex) {
|
||||
$message = "Missing or invalid information. Please check your input.";
|
||||
|
||||
@@ -26,6 +26,7 @@ use Propel\Runtime\Exception\PropelException;
|
||||
use Thelia\Core\Event\CustomerCreateOrUpdateEvent;
|
||||
use Thelia\Core\Security\SecurityContext;
|
||||
use Thelia\Form\CustomerCreation;
|
||||
use Thelia\Form\CustomerModification;
|
||||
use Thelia\Form\Exception\FormValidationException;
|
||||
use Thelia\Model\Customer;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
@@ -44,27 +45,7 @@ class CustomerController extends BaseFrontController
|
||||
try {
|
||||
$form = $this->validateForm($customerCreation, "post");
|
||||
|
||||
$data = $form->getData();
|
||||
|
||||
$customerCreateEvent = new CustomerCreateOrUpdateEvent(
|
||||
$data["title"],
|
||||
$data["firstname"],
|
||||
$data["lastname"],
|
||||
$data["address1"],
|
||||
$data["address2"],
|
||||
$data["address3"],
|
||||
$data["phone"],
|
||||
$data["cellphone"],
|
||||
$data["zipcode"],
|
||||
$data["city"],
|
||||
$data["country"],
|
||||
$data["email"],
|
||||
$data["password"],
|
||||
$request->getSession()->getLang(),
|
||||
isset($data["reseller"])?$data["reseller"]:null,
|
||||
isset($data["sponsor"])?$data["sponsor"]:null,
|
||||
isset($data["discount"])?$data["discount"]:nullsch
|
||||
);
|
||||
$customerCreateEvent = $this->createEventInstance($form->getData());
|
||||
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::CUSTOMER_CREATEACCOUNT, $customerCreateEvent);
|
||||
|
||||
@@ -85,6 +66,28 @@ class CustomerController extends BaseFrontController
|
||||
public function updateAction()
|
||||
{
|
||||
$request = $this->getRequest();
|
||||
$customerModification = new CustomerModification($request);
|
||||
|
||||
try {
|
||||
|
||||
$customer = $this->getSecurityContext(SecurityContext::CONTEXT_FRONT_OFFICE)->getUser();
|
||||
|
||||
$form = $this->validateForm($customerModification, "post");
|
||||
|
||||
$customerChangeEvent = $this->createEventInstance($form->getData());
|
||||
$customerChangeEvent->setCustomer($customer);
|
||||
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::CUSTOMER_UPDATEACCOUNT, $customerChangeEvent);
|
||||
|
||||
$this->redirectSuccess();
|
||||
|
||||
} catch (FormValidationException $e) {
|
||||
$customerModification->setErrorMessage($e->getMessage());
|
||||
$this->getParserContext()->setErrorForm($customerModification);
|
||||
} catch (PropelException $e) {
|
||||
\Thelia\Log\Tlog::getInstance()->error(sprintf("error during updating customer in front context with message : %s", $e->getMessage()));
|
||||
$this->getParserContext()->setGeneralError($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function loginAction()
|
||||
@@ -103,4 +106,33 @@ class CustomerController extends BaseFrontController
|
||||
if($sendLogin) $this->dispatch(TheliaEvents::CUSTOMER_LOGIN, $event);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return CustomerCreateOrUpdateEvent
|
||||
*/
|
||||
private function createEventInstance($data)
|
||||
{
|
||||
$customerCreateEvent = new CustomerCreateOrUpdateEvent(
|
||||
$data["title"],
|
||||
$data["firstname"],
|
||||
$data["lastname"],
|
||||
$data["address1"],
|
||||
$data["address2"],
|
||||
$data["address3"],
|
||||
$data["phone"],
|
||||
$data["cellphone"],
|
||||
$data["zipcode"],
|
||||
$data["city"],
|
||||
$data["country"],
|
||||
isset($data["email"])?$data["email"]:null,
|
||||
isset($data["password"]) ? $data["password"]:null,
|
||||
$this->getRequest()->getSession()->getLang(),
|
||||
isset($data["reseller"])?$data["reseller"]:null,
|
||||
isset($data["sponsor"])?$data["sponsor"]:null,
|
||||
isset($data["discount"])?$data["discount"]:nullsch
|
||||
);
|
||||
|
||||
return $customerCreateEvent;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -61,6 +61,11 @@ final class TheliaEvents
|
||||
*/
|
||||
const CUSTOMER_CREATEACCOUNT = "action.createCustomer";
|
||||
|
||||
/**
|
||||
* sent on customer account update
|
||||
*/
|
||||
const CUSTOMER_UPDATEACCOUNT = "action.modifyCustomer";
|
||||
|
||||
/**
|
||||
* Sent before the logout of the administrator.
|
||||
*/
|
||||
|
||||
@@ -6,7 +6,7 @@ use Symfony\Component\Config\Definition\Exception\Exception;
|
||||
use Thelia\Core\Event\Internal\CustomerEvent;
|
||||
use Thelia\Model\Base\Customer as BaseCustomer;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
use Thelia\Model\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
use Thelia\Core\Event\CustomRefEvent;
|
||||
@@ -56,7 +56,7 @@ class Customer extends BaseCustomer implements UserInterface
|
||||
* @param int $discount
|
||||
* @throws \Exception|\Symfony\Component\Config\Definition\Exception\Exception
|
||||
*/
|
||||
public function createOrUpdate($titleId, $firstname, $lastname, $address1, $address2, $address3, $phone, $cellphone, $zipcode, $city, $countryId, $email, $plainPassword = null, $lang = null, $reseller = 0, $sponsor = null, $discount = 0)
|
||||
public function createOrUpdate($titleId, $firstname, $lastname, $address1, $address2, $address3, $phone, $cellphone, $zipcode, $city, $countryId, $email = null, $plainPassword = null, $lang = null, $reseller = 0, $sponsor = null, $discount = 0)
|
||||
{
|
||||
$this
|
||||
->setTitleId($titleId)
|
||||
@@ -169,6 +169,21 @@ class Customer extends BaseCustomer implements UserInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setEmail($email, $force = false)
|
||||
{
|
||||
$email = trim($email);
|
||||
|
||||
if ($this->isNew() && ($email === null || $email == "")) {
|
||||
throw new InvalidArgumentException("customer email is mandatory on creation");
|
||||
}
|
||||
|
||||
if (!$this->isNew() && $force === false) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
return parent::setEmail($email);
|
||||
}
|
||||
|
||||
public function setDispatcher(EventDispatcherInterface $dispatcher)
|
||||
{
|
||||
$this->dispatcher = $dispatcher;
|
||||
|
||||
27
core/lib/Thelia/Model/Exception/InvalidArgumentException.php
Normal file
27
core/lib/Thelia/Model/Exception/InvalidArgumentException.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
namespace Thelia\Model\Exception;
|
||||
|
||||
|
||||
class InvalidArgumentException extends ModelException
|
||||
{}
|
||||
28
core/lib/Thelia/Model/Exception/ModelException.php
Normal file
28
core/lib/Thelia/Model/Exception/ModelException.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
namespace Thelia\Model\Exception;
|
||||
|
||||
|
||||
class ModelException extends \RuntimeException
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user