apply php-cs-fiwer on Action directory

This commit is contained in:
Manuel Raynaud
2013-08-09 10:38:58 +02:00
parent 7071ffc686
commit 31743fd987
7 changed files with 125 additions and 157 deletions

View File

@@ -23,43 +23,38 @@
namespace Thelia\Action; namespace Thelia\Action;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Thelia\Form\CategoryDeletionForm;
use Thelia\Form\BaseForm; use Thelia\Form\BaseForm;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Action\Exception\FormValidationException; use Thelia\Action\Exception\FormValidationException;
use Thelia\Core\Event\ActionEvent; use Thelia\Core\Event\ActionEvent;
abstract class BaseAction abstract class BaseAction
{ {
protected function validateForm(BaseForm $aBaseForm, $expectedMethod = null) protected function validateForm(BaseForm $aBaseForm, $expectedMethod = null)
{ {
$form = $aBaseForm->getForm(); $form = $aBaseForm->getForm();
if ($aBaseForm->getRequest()->isMethod($expectedMethod)) { if ($aBaseForm->getRequest()->isMethod($expectedMethod)) {
$form->bind($aBaseForm->getRequest()); $form->bind($aBaseForm->getRequest());
if ($form->isValid()) { if ($form->isValid()) {
return $form;
return $form; } else {
} throw new FormValidationException("Missing or invalid data");
else {
throw new FormValidationException("Missing or invalid data");
} }
} else {
throw new FormValidationException(sprintf("Wrong form method, %s expected.", $expectedMethod));
} }
else { }
throw new FormValidationException(sprintf("Wrong form method, %s expected.", $expectedMethod));
}
}
/**
*
* @param BaseForm $aBaseForm
* @param string $error_message
* @param ActionEvent $event
*/
protected function propagateFormError(BaseForm $aBaseForm, $error_message, ActionEvent $event) {
/**
*
* @param BaseForm $aBaseForm
* @param string $error_message
* @param ActionEvent $event
*/
protected function propagateFormError(BaseForm $aBaseForm, $error_message, ActionEvent $event)
{
// The form has an error // The form has an error
$aBaseForm->setError(true); $aBaseForm->setError(true);
$aBaseForm->setErrorMessage($error_message); $aBaseForm->setErrorMessage($error_message);
@@ -69,7 +64,7 @@ abstract class BaseAction
// Stop event propagation // Stop event propagation
$event->stopPropagation(); $event->stopPropagation();
} }
protected function redirect($url, $status = 302) protected function redirect($url, $status = 302)
{ {
@@ -79,4 +74,4 @@ abstract class BaseAction
exit; exit;
} }
} }

View File

@@ -40,26 +40,26 @@ class Category extends BaseAction implements EventSubscriberInterface
{ {
public function create(ActionEvent $event) public function create(ActionEvent $event)
{ {
$request = $event->getRequest(); $request = $event->getRequest();
try { try {
$categoryCreationForm = new CategoryCreationForm($request); $categoryCreationForm = new CategoryCreationForm($request);
$form = $this->validateForm($categoryCreationForm, "POST"); $form = $this->validateForm($categoryCreationForm, "POST");
$data = $form->getData(); $data = $form->getData();
$category = new CategoryModel(); $category = new CategoryModel();
$event->getDispatcher()->dispatch(TheliaEvents::BEFORE_CREATECATEGORY, $event); $event->getDispatcher()->dispatch(TheliaEvents::BEFORE_CREATECATEGORY, $event);
$category->create( $category->create(
$data["title"], $data["title"],
$data["parent"], $data["parent"],
$data["locale"] $data["locale"]
); );
AdminLog::append(sprintf("Category %s (ID %s) created", $category->getTitle(), $category->getId()), $request, $request->getSession()->getAdminUser()); AdminLog::append(sprintf("Category %s (ID %s) created", $category->getTitle(), $category->getId()), $request, $request->getSession()->getAdminUser());
$categoryEvent = new CategoryEvent($category); $categoryEvent = new CategoryEvent($category);
@@ -68,8 +68,8 @@ class Category extends BaseAction implements EventSubscriberInterface
// Substitute _ID_ in the URL with the ID of the created category // Substitute _ID_ in the URL with the ID of the created category
$successUrl = str_replace('_ID_', $category->getId(), $categoryCreationForm->getSuccessUrl()); $successUrl = str_replace('_ID_', $category->getId(), $categoryCreationForm->getSuccessUrl());
// Redirect to the success URL // Redirect to the success URL
$this->redirect($successUrl); $this->redirect($successUrl);
} catch (PropelException $e) { } catch (PropelException $e) {
Tlog::getInstance()->error(sprintf('error during creating category with message "%s"', $e->getMessage())); Tlog::getInstance()->error(sprintf('error during creating category with message "%s"', $e->getMessage()));
@@ -83,7 +83,7 @@ class Category extends BaseAction implements EventSubscriberInterface
public function modify(ActionEvent $event) public function modify(ActionEvent $event)
{ {
/* /*
$request = $event->getRequest(); $request = $event->getRequest();
$customerModification = new CustomerModification($request); $customerModification = new CustomerModification($request);
@@ -100,9 +100,9 @@ class Category extends BaseAction implements EventSubscriberInterface
$customer = CustomerQuery::create()->findPk(1); $customer = CustomerQuery::create()->findPk(1);
try { try {
$customerEvent = new CustomerEvent($customer); $customerEvent = new CustomerEvent($customer);
$event->getDispatcher()->dispatch(TheliaEvents::BEFORE_CHANGECUSTOMER, $customerEvent); $event->getDispatcher()->dispatch(TheliaEvents::BEFORE_CHANGECUSTOMER, $customerEvent);
$data = $form->getData(); $data = $form->getData();
$customer->createOrUpdate( $customer->createOrUpdate(
$data["title"], $data["title"],
@@ -123,20 +123,17 @@ class Category extends BaseAction implements EventSubscriberInterface
// Update the logged-in user, and redirect to the success URL (exits) // 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. // We don-t send the login event, as the customer si already logged.
$this->processSuccessfullLogin($event, $customer, $customerModification); $this->processSuccessfullLogin($event, $customer, $customerModification);
} } catch (PropelException $e) {
catch(PropelException $e) {
Tlog::getInstance()->error(sprintf('error during modifying customer on action/modifyCustomer with message "%s"', $e->getMessage())); 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."; $message = "Failed to change your account, please try again.";
} }
} else {
$message = "Missing or invalid data";
} }
else { } else {
$message = "Missing or invalid data"; $message = "Wrong form method !";
}
}
else {
$message = "Wrong form method !";
} }
// The form has an error // The form has an error
@@ -155,44 +152,42 @@ class Category extends BaseAction implements EventSubscriberInterface
*/ */
public function delete(ActionEvent $event) public function delete(ActionEvent $event)
{ {
$request = $event->getRequest(); $request = $event->getRequest();
try { try {
$categoryDeletionForm = new CategoryDeletionForm($request); $categoryDeletionForm = new CategoryDeletionForm($request);
$form = $this->validateForm($categoryDeletionForm, "POST"); $form = $this->validateForm($categoryDeletionForm, "POST");
$data = $form->getData(); $data = $form->getData();
$category = CategoryQuery::create()->findPk($data['id']); $category = CategoryQuery::create()->findPk($data['id']);
$categoryEvent = new CategoryEvent($category); $categoryEvent = new CategoryEvent($category);
$event->getDispatcher()->dispatch(TheliaEvents::BEFORE_DELETECATEGORY, $categoryEvent); $event->getDispatcher()->dispatch(TheliaEvents::BEFORE_DELETECATEGORY, $categoryEvent);
$category->delete(); $category->delete();
AdminLog::append(sprintf("Category %s (ID %s) deleted", $category->getTitle(), $category->getId()), $request, $request->getSession()->getAdminUser()); AdminLog::append(sprintf("Category %s (ID %s) deleted", $category->getTitle(), $category->getId()), $request, $request->getSession()->getAdminUser());
$categoryEvent->category = $category; $categoryEvent->category = $category;
$event->getDispatcher()->dispatch(TheliaEvents::AFTER_DELETECATEGORY, $categoryEvent); $event->getDispatcher()->dispatch(TheliaEvents::AFTER_DELETECATEGORY, $categoryEvent);
// Substitute _ID_ in the URL with the ID of the created category // Substitute _ID_ in the URL with the ID of the created category
$successUrl = str_replace('_ID_', $category->getParent(), $categoryDeletionForm->getSuccessUrl()); $successUrl = str_replace('_ID_', $category->getParent(), $categoryDeletionForm->getSuccessUrl());
// Redirect to the success URL // Redirect to the success URL
Redirect::exec($successUrl); Redirect::exec($successUrl);
} } catch (PropelException $e) {
catch(PropelException $e) {
\Thelia\Log\Tlog::getInstance()->error(sprintf('error during deleting category ID=%s on action/modifyCustomer with message "%s"', $data['id'], $e->getMessage())); \Thelia\Log\Tlog::getInstance()->error(sprintf('error during deleting category ID=%s on action/modifyCustomer with message "%s"', $data['id'], $e->getMessage()));
$message = "Failed to change your account, please try again."; $message = "Failed to change your account, please try again.";
} } catch (FormValidationException $e) {
catch(FormValidationException $e) {
$message = $e->getMessage(); $message = $e->getMessage();
} }
$this->propagateFormError($categoryDeletionForm, $message, $event); $this->propagateFormError($categoryDeletionForm, $message, $event);
@@ -205,20 +200,20 @@ class Category extends BaseAction implements EventSubscriberInterface
*/ */
public function toggleVisibility(ActionEvent $event) public function toggleVisibility(ActionEvent $event)
{ {
$request = $event->getRequest(); $request = $event->getRequest();
$category = CategoryQuery::create()->findPk($request->get('id', 0)); $category = CategoryQuery::create()->findPk($request->get('id', 0));
if ($category !== null) { if ($category !== null) {
$category->setVisible($category->getVisible() ? false : true); $category->setVisible($category->getVisible() ? false : true);
$category->save(); $category->save();
$categoryEvent = new CategoryEvent($category); $categoryEvent = new CategoryEvent($category);
$event->getDispatcher()->dispatch(TheliaEvents::AFTER_CHANGECATEGORY, $categoryEvent); $event->getDispatcher()->dispatch(TheliaEvents::AFTER_CHANGECATEGORY, $categoryEvent);
} }
} }
/** /**

View File

@@ -25,7 +25,6 @@ namespace Thelia\Action;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\ActionEvent; use Thelia\Core\Event\ActionEvent;
use Thelia\Core\Event\CustomerEvent;
use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\TheliaEvents;
use Thelia\Form\BaseForm; use Thelia\Form\BaseForm;
use Thelia\Form\CustomerCreation; use Thelia\Form\CustomerCreation;
@@ -36,14 +35,12 @@ use Thelia\Model\CustomerQuery;
use Thelia\Form\CustomerLogin; use Thelia\Form\CustomerLogin;
use Thelia\Core\Security\Authentication\CustomerUsernamePasswordFormAuthenticator; use Thelia\Core\Security\Authentication\CustomerUsernamePasswordFormAuthenticator;
use Thelia\Core\Security\SecurityContext; use Thelia\Core\Security\SecurityContext;
use Thelia\Model\ConfigQuery;
use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Component\Validator\Exception\ValidatorException;
use Thelia\Core\Security\Exception\AuthenticationException; use Thelia\Core\Security\Exception\AuthenticationException;
use Thelia\Core\Security\Exception\UsernameNotFoundException; use Thelia\Core\Security\Exception\UsernameNotFoundException;
use Propel\Runtime\Exception\PropelException; use Propel\Runtime\Exception\PropelException;
use Thelia\Action\Exception\FormValidationException; use Thelia\Action\Exception\FormValidationException;
class Customer extends BaseAction implements EventSubscriberInterface class Customer extends BaseAction implements EventSubscriberInterface
{ {
/** /**
@@ -51,25 +48,26 @@ class Customer extends BaseAction implements EventSubscriberInterface
*/ */
protected $securityContext; protected $securityContext;
public function __construct(SecurityContext $securityContext) { public function __construct(SecurityContext $securityContext)
{
$this->securityContext = $securityContext; $this->securityContext = $securityContext;
} }
public function create(ActionEvent $event) public function create(ActionEvent $event)
{ {
$request = $event->getRequest(); $request = $event->getRequest();
try { try {
$customerCreationForm = new CustomerCreation($request); $customerCreationForm = new CustomerCreation($request);
$form = $this->validateForm($customerCreationForm, "POST"); $form = $this->validateForm($customerCreationForm, "POST");
$data = $form->getData(); $data = $form->getData();
$customer = new CustomerModel(); $customer = new CustomerModel();
$customer->setDispatcher($event->getDispatcher()); $customer->setDispatcher($event->getDispatcher());
$customer->createOrUpdate( $customer->createOrUpdate(
$data["title"], $data["title"],
$data["firstname"], $data["firstname"],
$data["lastname"], $data["lastname"],
$data["address1"], $data["address1"],
@@ -84,40 +82,34 @@ class Customer extends BaseAction implements EventSubscriberInterface
$request->getSession()->getLang() $request->getSession()->getLang()
); );
// Connect the newly created user,and redirect to the success URL // Connect the newly created user,and redirect to the success URL
$this->processSuccessfullLogin($event, $customer, $customerCreationForm, true); $this->processSuccessfullLogin($event, $customer, $customerCreationForm, true);
} } catch (PropelException $e) {
catch (PropelException $e) {
Tlog::getInstance()->error(sprintf('error during creating customer on action/createCustomer with message "%s"', $e->getMessage())); Tlog::getInstance()->error(sprintf('error during creating customer on action/createCustomer with message "%s"', $e->getMessage()));
$message = "Failed to create your account, please try again."; $message = "Failed to create your account, please try again.";
} } catch (FormValidationException $e) {
catch(FormValidationException $e) {
$message = $e->getMessage(); $message = $e->getMessage();
} }
// The form has errors, propagate it. // The form has errors, propagate it.
$this->propagateFormError($customerCreationForm, $message, $event); $this->propagateFormError($customerCreationForm, $message, $event);
} }
public function modify(ActionEvent $event) public function modify(ActionEvent $event)
{ {
$request = $event->getRequest(); $request = $event->getRequest();
try { try {
$customerModification = new CustomerModification($request); $customerModification = new CustomerModification($request);
$form = $this->validateForm($customerModification, "POST"); $form = $this->validateForm($customerModification, "POST");
$data = $form->getData(); $data = $form->getData();
$customer = CustomerQuery::create()->findPk(1); $customer = CustomerQuery::create()->findPk(1);
$data = $form->getData(); $data = $form->getData();
$customer->createOrUpdate( $customer->createOrUpdate(
@@ -131,29 +123,24 @@ class Customer extends BaseAction implements EventSubscriberInterface
$data["cellphone"], $data["cellphone"],
$data["zipcode"], $data["zipcode"],
$data["country"] $data["country"]
); );
// Update the logged-in user, and redirect to the success URL (exits) // 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. // We don-t send the login event, as the customer si already logged.
$this->processSuccessfullLogin($event, $customer, $customerModification); $this->processSuccessfullLogin($event, $customer, $customerModification);
} } catch (PropelException $e) {
catch(PropelException $e) {
Tlog::getInstance()->error(sprintf('error during modifying customer on action/modifyCustomer with message "%s"', $e->getMessage())); 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."; $message = "Failed to change your account, please try again.";
} } catch (FormValidationException $e) {
catch(FormValidationException $e) {
$message = $e->getMessage(); $message = $e->getMessage();
} }
// The form has errors, propagate it. // The form has errors, propagate it.
$this->propagateFormError($customerModification, $message, $event); $this->propagateFormError($customerModification, $message, $event);
} }
/** /**
* Perform user logout. The user is redirected to the provided view, if any. * Perform user logout. The user is redirected to the provided view, if any.
@@ -162,9 +149,9 @@ class Customer extends BaseAction implements EventSubscriberInterface
*/ */
public function logout(ActionEvent $event) public function logout(ActionEvent $event)
{ {
$event->getDispatcher()->dispatch(TheliaEvents::CUSTOMER_LOGOUT, $event); $event->getDispatcher()->dispatch(TheliaEvents::CUSTOMER_LOGOUT, $event);
$this->getSecurityContext()->clear(); $this->getSecurityContext()->clear();
} }
/** /**
@@ -177,36 +164,32 @@ class Customer extends BaseAction implements EventSubscriberInterface
*/ */
public function login(ActionEvent $event) public function login(ActionEvent $event)
{ {
$request = $event->getRequest(); $request = $event->getRequest();
$customerLoginForm = new CustomerLogin($request); $customerLoginForm = new CustomerLogin($request);
$authenticator = new CustomerUsernamePasswordFormAuthenticator($request, $customerLoginForm); $authenticator = new CustomerUsernamePasswordFormAuthenticator($request, $customerLoginForm);
try { try {
$user = $authenticator->getAuthentifiedUser(); $user = $authenticator->getAuthentifiedUser();
$this->processSuccessfullLogin($event, $user, $customerLoginForm); $this->processSuccessfullLogin($event, $user, $customerLoginForm);
} } catch (ValidatorException $ex) {
catch (ValidatorException $ex) { $message = "Missing or invalid information. Please check your input.";
$message = "Missing or invalid information. Please check your input."; } catch (UsernameNotFoundException $ex) {
} $message = "This email address was not found.";
catch (UsernameNotFoundException $ex) { } catch (AuthenticationException $ex) {
$message = "This email address was not found."; $message = "Login failed. Please check your username and password.";
} } catch (\Exception $ex) {
catch (AuthenticationException $ex) { $message = sprintf("Unable to process your request. Please try again (%s in %s).", $ex->getMessage(), $ex->getFile());
$message = "Login failed. Please check your username and password."; }
}
catch (\Exception $ex) {
$message = sprintf("Unable to process your request. Please try again (%s in %s).", $ex->getMessage(), $ex->getFile());
}
// The for has an error // The for has an error
$customerLoginForm->setError(true); $customerLoginForm->setError(true);
$customerLoginForm->setErrorMessage($message); $customerLoginForm->setErrorMessage($message);
// Dispatch the errored form // Dispatch the errored form
$event->setErrorForm($customerLoginForm); $event->setErrorForm($customerLoginForm);
// A this point, the same view is displayed again. // A this point, the same view is displayed again.
} }
@@ -250,10 +233,10 @@ class Customer extends BaseAction implements EventSubscriberInterface
* *
* Stores the current user in the security context, and redirect to the * Stores the current user in the security context, and redirect to the
* success_url. * success_url.
* @param ActionEvent $event * @param ActionEvent $event
* @param CustomerModel $user * @param CustomerModel $user
* @param BaseForm $form * @param BaseForm $form
* @param bool $sendLoginEvent * @param bool $sendLoginEvent
*/ */
protected function processSuccessfullLogin(ActionEvent $event, CustomerModel $user, BaseForm $form, $sendLoginEvent = false) protected function processSuccessfullLogin(ActionEvent $event, CustomerModel $user, BaseForm $form, $sendLoginEvent = false)
{ {
@@ -275,7 +258,6 @@ class Customer extends BaseAction implements EventSubscriberInterface
if ($sendLoginEvent) $event->getDispatcher()->dispatch(TheliaEvents::CUSTOMER_LOGIN, $event); if ($sendLoginEvent) $event->getDispatcher()->dispatch(TheliaEvents::CUSTOMER_LOGIN, $event);
} }
/** /**
@@ -283,9 +265,9 @@ class Customer extends BaseAction implements EventSubscriberInterface
* *
* @return SecurityContext the security context * @return SecurityContext the security context
*/ */
protected function getSecurityContext() { protected function getSecurityContext()
//$this->securityContext->setContext(SecurityContext::CONTEXT_FRONT_OFFICE); {
//$this->securityContext->setContext(SecurityContext::CONTEXT_FRONT_OFFICE);
return $this->securityContext; return $this->securityContext;
} }
} }

View File

@@ -22,8 +22,7 @@
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\Action\Exception; namespace Thelia\Action\Exception;
class ActionException extends \RuntimeException class ActionException extends \RuntimeException
{ {
} }

View File

@@ -23,7 +23,6 @@
namespace Thelia\Action\Exception; namespace Thelia\Action\Exception;
class FormValidationException extends ActionException class FormValidationException extends ActionException
{ {
} }

View File

@@ -23,7 +23,6 @@
namespace Thelia\Action\Exception; namespace Thelia\Action\Exception;
class ProductNotFoundException extends ActionException
class ProductNotFoundException extends ActionException { {
}
}

View File

@@ -23,8 +23,7 @@
namespace Thelia\Action\Exception; namespace Thelia\Action\Exception;
class StockNotFoundException extends ActionException class StockNotFoundException extends ActionException
{ {
} }