Je n'arrive pas à faire la liaison entre mon controller et mon listener
//le controller
<?php
namespace HookRetraction\Controller\Front;
use HookRetraction\Event\HookRetractionEvents;
use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Model\OrderQuery;
use Thelia\Model\OrderStatusQuery;
/**
* Class HookRetractionFrontController
* @package HookRetraction\Form
* @author Frederic Faou <fredodefrance@gmail.com>
*/
class HookRetractionFrontController extends BaseFrontController
{
const FORM_NAME = 'hookretraction.live.form';
protected $response;
public function CreateAction()
{
if (null != $response = $this->getSecurityContext()->getCustomerUser()) {
return $this->generateRedirectFromRoute("customer.login.view");
}
// Create the form from the request
$form = $this->createForm('hookretraction.live.form');
// Initialize the potential exception
$message = null;
try {
// Obtains current date
$today = new \DateTime('now');
// Check the form against constraints violations
$validateForm = $this->validateForm($form, "post");
//Get the id in the form value and search it in the order table
$data = $validateForm->getData();
$order = OrderQuery::create()->findPk($data['order_id']);
$status = OrderStatusQuery::create()->findPk($data['status_id']);
if (null === $order) {
throw new \InvalidArgumentException("The order you want to update status does not exist");
}
if (null === $status) {
throw new \InvalidArgumentException("The status you want to set to the order does not exist");
}
$event = new OrderEvent($order);
$event
->setOrder($data['order_id'])
->setStatus($data['status_id'])
;
} catch (FormValidationException $message) {
// Form cannot be validated. Create the error message using
$response = $message->getMessage();
}
catch (\Exception $message) {
// Any other error
$response = $message->getMessage();
}
if (null !== $message) {
\Thelia\Log\Tlog::getInstance()->error(
sprintf("Error validation : %s.", $message)
);
$form->setErrorMessage($message);
$this->getParserContext()
->addForm($form)
->setGeneralError($message)
;
return $this->render("account", $response);
}
return $response;
}
private function createEventInstance($data)
{
$HookRetractionEvent = new HookRetractionEvents(
$data['order_id'],
$data['status_id']
);
return $HookRetractionEvent;
}
public function getName()
{
return self::FORM_NAME;
}
}
//le listener
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
/*************************************************************************************/
namespace HookRetraction\EventListener;
use HookRetraction\HookRetraction;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Action\BaseAction;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Template\ParserInterface;
use Thelia\Mailer\MailerFactory;
use Thelia\Model\ConfigQuery;
use Thelia\Model\MessageQuery;
/**
* Class SendConfirmationEmail
*/
class HookRetractionUpdateFrontListener extends BaseAction implements EventSubscriberInterface
{
/**
* @var MailerFactory
*/
protected $mailer;
/**
* @var ParserInterface
*/
protected $parser;
public function __construct(ParserInterface $parser, MailerFactory $mailer)
{
$this->parser = $parser;
$this->mailer = $mailer;
}
/*
* @params OrderEvent $order
* Checks if order new status is sent, send an email to the customer.
*/
public function update_status(OrderEvent $event)
{
if ($event->getOrder()->isSent()) {
$contact_email = ConfigQuery::getStoreEmail();
if ($contact_email) {
$order = $event->getOrder();
$customer = $order->getCustomer();
$this->mailer->sendEmailToCustomer(
HookRetraction::MESSAGE_SEND_CONFIRMATION,
$order->getCustomer(),
[
'order_id' => $order->getId(),
'order_ref' => $order->getRef(),
'customer_id' => $customer->getId(),
'status_id' => $order->getStatusId(),
'order_date' => $order->getCreatedAt(),
'update_date' => $order->getUpdatedAt()
]
);
}
}
}
/**
*
* @inheritdoc
*/
public static function getSubscribedEvents()
{
return array(
TheliaEvents::ORDER_UPDATE_STATUS => array("update_status", 128)
);
}
}
Last edited by fredodefrance (02-05-2017 01:47:59)