<?php
/*
* 2001-2019 Reflet Communication
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0) that is available
* through the world-wide-web at this URL: http://www.opensource.org/licenses/OSL-3.0
* If you are unable to obtain it through the world-wide-web, please send an email
* to agence@reflet-digital.com so we can send you a copy immediately.
*
* @author Reflet Communication
* @copyright 2001-2019 Reflet Communication
* @license http://www.opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
namespace App\Controller;
use App\Entity\Contact;
use App\Event\ContactEvent;
use App\Event\ContactFormEvent;
use App\Exception\BadResponseException;
use App\Filter\ProxyFilter;
use App\Form\Type\BasicContactType;
use App\Manager\Proxy\ProxyEntityManagerInterface;
use App\Manager\Proxy\SuluManager;
use App\Routing\CustomPageLoader;
use App\Service\MailChimpService;
use App\Service\ProxyService;
use Aws\Exception\AwsException;
use Psr\Log\LoggerInterface;
use Rc\MailChimp\Exception\MailChimpException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
/**
* Class SuluPageController
*
* @package App\Controller
*
* @Route(
* name = "sulu_page_"
* )
*/
class SuluPageController extends BaseController
{
const CUSTOM_PAGE_FORM_NAME = 'Opération spéciale';
/**
* @var ProxyEntityManagerInterface
*/
protected $proxyEntityManager;
/**
* @var ProxyFilter
*/
protected $proxyFilter;
/**
* SuluPageController constructor.
*
* @param RequestStack $stack
* @param ProxyService $proxyService
* @param ProxyFilter $proxyFilter
* @param TagAwareCacheInterface $cache
*/
public function __construct(
RequestStack $stack,
ProxyService $proxyService,
ProxyFilter $proxyFilter,
TagAwareCacheInterface $cache
) {
$request = $stack->getCurrentRequest();
$this->proxyEntityManager = new SuluManager($proxyService, $request->server, $cache);
$this->proxyFilter = $proxyFilter;
}
/**
* @Route(
* "/{path}",
* requirements = {
* "path" = "^(([a-z0-9]+(-[a-z0-9]+)*)(/([a-z0-9]+(-[a-z0-9]+)*))*)$"
* },
* name = "index"
* )
*
* @param Request $request
* @param LoggerInterface $logger
* @param string $path
*
* @return Response
*/
public function index(Request $request, LoggerInterface $logger, string $path): Response
{
$query = $request
->query
->all()
;
if (!empty($query)) {
$path .= '?'.http_build_query($query);
}
try {
$content = $this
->proxyEntityManager
->getContent('/' . $path)
;
} catch (BadResponseException $exception) {
$response = $exception->getResponse();
if (isset($response)) {
$body = strval($response->getBody());
$logger->error('Sulu call error: '.$path, [
$body,
]);
}
throw new NotFoundHttpException('Page not found');
}
$proxyFilter = $this->proxyFilter;
$content = $proxyFilter($request, $content);
$firstImage = $proxyFilter->getImage($content);
return $this->render('controller/sulu-page/index.html.twig', [
'proxyContent' => $content,
'firstImage' => $firstImage,
]);
}
/**
* @param Request $request
* @param LoggerInterface $logger
* @param MailChimpService $mailChimp
* @param EventDispatcherInterface $eventDispatcher
*
* @return Response
*/
public function custom(
Request $request,
LoggerInterface $logger,
MailChimpService $mailChimp,
EventDispatcherInterface $eventDispatcher
): Response
{
$path = $request->getRequestUri();
$path = CustomPageLoader::PATH_PREFIX.$path;
try {
$content = $this
->proxyEntityManager
->getContent($path)
;
} catch (BadResponseException $exception) {
$response = $exception->getResponse();
if (isset($response)) {
$body = strval($response->getBody());
$logger->error('Sulu call error: '.$path, [
$body,
]);
}
throw new NotFoundHttpException('Page not found');
}
$proxyFilter = $this->proxyFilter;
$content = $proxyFilter($request, $content);
$firstImage = $proxyFilter->getImage($content);
$contact = new Contact();
$form = $this->createForm(BasicContactType::class, $contact);
$route = $request
->attributes
->get('_route')
;
if ($proxyFilter->hasContactForm($content)) {
$form->handleRequest($request);
if (
$form->isSubmitted()
&& $form->isValid()
) {
$contact = $form->getData();
$customerEvent = new ContactFormEvent(
$contact,
'user_acknowledgement',
$proxyFilter->getMailMeta($content, 'user/subject'),
$proxyFilter->getMailMeta($content, 'user/content')
);
$salesEvent = new ContactFormEvent(
$contact,
'sales_notification',
$proxyFilter->getMailMeta($content, 'sales/subject'),
$proxyFilter->getMailMeta($content, 'sales/content')
);
try {
$eventDispatcher->dispatch($customerEvent, ContactFormEvent::NAME);
} catch (AwsException $e) {
// Ignore
}
try {
$eventDispatcher->dispatch($salesEvent, ContactFormEvent::NAME);
$this->addFlash(
'success',
'Votre demande a bien été envoyée.'
);
} catch (AwsException $e) {
$this->addFlash(
'error',
'Une erreur s\'est produite lors de l\'envoi de votre demande. Veuillez réessayer ultérieurement.'
);
}
if (!empty($contact->getNewsletterSubscribe())) {
$email = $contact->getEmail();
try {
try {
$result = $mailChimp->get($email);
if (
$result->status === MailChimpService::STATUS_ARCHIVED
|| $result->status === MailChimpService::STATUS_UNSUBSCRIBED
) {
$result = $mailChimp->put($email);
}
} catch (MailChimpException $e) {
$result = $mailChimp->put($email);
}
$this->addFlash(
'success',
$result->status === MailChimpService::STATUS_PENDING
? ContactController::NEWSLETTER_SUCCESS_NEW
: ContactController::NEWSLETTER_SUCCESS_OLD
);
} catch (MailChimpException $e) {
$this->addFlash(
'error',
'Une erreur s\'est produite lors de votre inscription à la newsletter. Veuillez réessayer ultérieurement.'
);
}
}
return $this->redirectToRoute($route);
}
$proxyFilter->setContactForm($content, $form, self::CUSTOM_PAGE_FORM_NAME, $route);
}
return $this->render('controller/sulu-page/index.html.twig', [
'proxyContent' => $content,
'firstImage' => $firstImage,
]);
}
}