Browse Source

Factory

develop
Guillaume 3 years ago
parent
commit
a61a70b67f
14 changed files with 91 additions and 363 deletions
  1. +34
    -15
      Controller/Reminder/ReminderAdminController.php
  2. +7
    -1
      Controller/Ticket/TicketAdminController.php
  3. +0
    -254
      Controller/Ticket/_TicketAdminController.php
  4. +1
    -41
      Factory/AbstractFactory.php
  5. +8
    -0
      Factory/FactoryInterface.php
  6. +8
    -2
      Factory/Reminder/ReminderFactory.php
  7. +5
    -3
      Factory/Setting/SiteSettingFactory.php
  8. +5
    -4
      Factory/Site/SiteFactory.php
  9. +6
    -20
      Factory/Ticket/TicketFactory.php
  10. +5
    -10
      Factory/Ticket/TicketMessageFactory.php
  11. +4
    -11
      Factory/User/UserFactory.php
  12. +1
    -1
      Model/Ticket/TicketModel.php
  13. +1
    -1
      Resources/views/adminlte/block/navbar_header.html.twig
  14. +6
    -0
      Twig/TwigExtension.php

+ 34
- 15
Controller/Reminder/ReminderAdminController.php View File

@@ -3,9 +3,12 @@
namespace Lc\SovBundle\Controller\Reminder;

use Doctrine\ORM\EntityManagerInterface;
use Lc\CaracoleBundle\Resolver\MerchantResolver;
use Lc\CaracoleBundle\Resolver\SectionResolver;
use Lc\SovBundle\Factory\Reminder\ReminderFactoryInterface;
use Lc\SovBundle\Form\Reminder\ReminderAdminFormType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@@ -14,37 +17,47 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class ReminderAdminController extends AbstractController
{

protected $entityManager;
protected $reminderFactory;
protected $formFactory;
protected $urlGenerator;
protected EntityManagerInterface $entityManager;
protected ReminderFactoryInterface $reminderFactory;
protected FormFactoryInterface $formFactory;
protected UrlGeneratorInterface $urlGenerator;
protected MerchantResolver $merchantResolver;
protected SectionResolver $sectionResolver;
protected ParameterBagInterface $parameterBag;

public function __construct(
EntityManagerInterface $entityManager,
ReminderFactoryInterface $reminderFactory,
FormFactoryInterface $formFactory,
UrlGeneratorInterface $urlGenerator
UrlGeneratorInterface $urlGenerator,
MerchantResolver $merchantResolver,
SectionResolver $sectionResolver,
ParameterBagInterface $parameterBag
) {
$this->entityManager = $entityManager;
$this->reminderFactory = $reminderFactory;
$this->formFactory = $formFactory;
$this->urlGenerator = $urlGenerator;
$this->merchantResolver = $merchantResolver;
$this->sectionResolver = $sectionResolver;
$this->parameterBag = $parameterBag;
}

public function renderModal(Request $request): Response
{
$reminder = $this->reminderFactory->create(
[
'crudAction' => $request->get('crudAction'),
'crudControllerFqcn' => $request->get('crudControllerFqcn'),
'entityId' => $request->get('entityId'),
]
);
$crudAction = $request->get('crudAction');
$crudControllerFqcn = $request->get('crudControllerFqcn');
$entityId = $request->get('entityId');

$reminder = $this->createEntity($crudAction, $crudControllerFqcn, $entityId);

$form = $this->formFactory->create(
ReminderAdminFormType::class,
$reminder,
[
'action' => $this->urlGenerator->generate('sov_admin_reminder_new')
'action' => $this->urlGenerator->generate(
$this->parameterBag->get('app.reminder.route_new')
)
]
);

@@ -58,9 +71,9 @@ class ReminderAdminController extends AbstractController

public function new(Request $request)
{
$reminder = $this->reminderFactory->create();
$form = $this->formFactory->create(ReminderAdminFormType::class, $reminder);
$reminder = $this->createEntity();

$form = $this->formFactory->create(ReminderAdminFormType::class, $reminder);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
@@ -74,6 +87,12 @@ class ReminderAdminController extends AbstractController
return $this->redirect($request->headers->get('referer'));
}

public function createEntity(string $crudAction = null, string $crudControllerFqcn = null, int $entityId = null)
{
return $this->reminderFactory->create($crudAction, $crudControllerFqcn, $entityId);
}


/*
public function renderTemplate($actionName, $templatePath, array $parameters = [])
{

+ 7
- 1
Controller/Ticket/TicketAdminController.php View File

@@ -98,9 +98,15 @@ class TicketAdminController extends AbstractAdminController
return parent::configureActions($actions);
}

public function createEntityFromFactory()
{
return $this->ticketFactory->create();
}

public function new(AdminContext $context)
{
$ticket = $this->ticketFactory->create();
$ticket = $this->createEntityFromFactory();

$form = $this->createForm(TicketFormType::class, $ticket);

$form->handleRequest($context->getRequest());

+ 0
- 254
Controller/Ticket/_TicketAdminController.php View File

@@ -1,254 +0,0 @@
<?php

namespace Lc\SovBundle\Controller\Ticket;

use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
use FOS\UserBundle\Model\UserManagerInterface;
use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Controller\AbstractAdminController;
use Lc\SovBundle\Form\Ticket\TicketMessageType;
use Lc\SovBundle\Form\Ticket\TicketStatusType;
use Lc\SovBundle\Services\UtilsManager;
use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
use Lc\SovBundle\Model\Ticket\TicketModel;

abstract class _TicketAdminController extends AbstractAdminController
{
protected $ticketUtils;
protected $userUtils;

public function __construct(
Security $security,
UserManagerInterface $userManager,
EntityManagerInterface $em,
MailjetTransport $mailjetTransport,
UtilsManager $utilsManager,
TranslatorInterface $translator,
SessionInterface $session
) {
parent::__construct($security, $userManager, $em, $mailjetTransport, $utilsManager, $translator, $session);
$this->ticketUtils = $utilsManager->getTicketUtils();
$this->userUtils = $utilsManager->getUserUtils();
}

public function persistTicketEntity($entity, $form)
{
$entity->setStatus(TicketModel::TICKET_STATUS_OPEN);

$ticket = $this->ticketUtils->createTicket(
[
'user' => $entity->getUser(),
'type' => $entity->getType(),
'orderShop' => $entity->getOrderShop(),
'subject' => $entity->getSubject(),
'message' => $form->get('message')->getData(),
'createByAdmin' => true,
]
);
}

protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
{
$queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);

if ($this->filtersForm->get('status')->getData() === null) {
$queryBuilder->andWhere('entity.status LIKE :open OR entity.status LIKE :beingprocessed');
$queryBuilder->setParameter('open', TicketModel::TICKET_STATUS_OPEN);
$queryBuilder->setParameter('beingprocessed', TicketModel::TICKET_STATUS_BEING_PROCESSED);
}

return $queryBuilder;
}

public function showAction()
{
$this->dispatch(EasyAdminEvents::PRE_SHOW);

$id = $this->request->query->get('id');
$easyadmin = $this->request->attributes->get('easyadmin');
$entity = $easyadmin['item'];

$fields = $this->entity['show']['fields'];
$deleteForm = $this->createDeleteForm($this->entity['name'], $id);

$this->dispatch(
EasyAdminEvents::POST_SHOW,
[
'deleteForm' => $deleteForm,
'fields' => $fields,
'entity' => $entity,
]
);

$parameters = [
'entity' => $entity,
'fields' => $fields,
'delete_form' => $deleteForm->createView(),
];

return $this->executeDynamicMethod(
'render<EntityName>Template',
['show', $this->entity['templates']['show'], $parameters]
);
}

/**
* The method that is executed when the user performs a 'show' action on an entity.
*
* @return Response
*/
public function renderTicketTemplate($actionName, $templatePath, array $parameters = [])
{
if ($actionName == 'show') {
$parameters['form_add_ticket_message'] = $this->createCustomForm(
TicketMessageType::class,
'newMessageTicket',
$parameters,
false
)->createView();
$parameters['form_ticket_status'] = $this->createCustomForm(
TicketStatusType::class,
'ticketStatus',
$parameters
)->createView();
}
return parent::renderTemplate($actionName, $templatePath, $parameters);
}


public function ticketStatusAction()
{
$easyadmin = $this->request->attributes->get('easyadmin');
$ticket = $easyadmin['item'];

$formTicketStatusForm = $this->createForm(TicketStatusType::class, $ticket);
$formTicketStatusForm->handleRequest($this->request);

if ($formTicketStatusForm->isSubmitted() && $formTicketStatusForm->isValid()) {
$this->em->persist($ticket);
$this->em->flush();

$this->utils->addFlash('success', 'success.ticket.statusChange');
} else {
$this->utils->addFlash('error', $formTicketStatusForm->getErrors());
}
$response['flashMessages'] = $this->utils->getFlashMessages();

return new Response(json_encode($response));
}

public function newMessageTicketAction()
{
$easyadmin = $this->request->attributes->get('easyadmin');
$ticket = $easyadmin['item'];

$formAddTicketMessage = $this->createForm(TicketMessageType::class);
$formAddTicketMessage->handleRequest($this->request);

if ($formAddTicketMessage->isSubmitted() && $formAddTicketMessage->isValid()) {
$this->ticketUtils->createTicketMessage(
array(
'ticket' => $ticket,
'message' => $formAddTicketMessage->get('message')->getData(),
'answerByAdmin' => true
)
);

$this->utils->addFlash('success', 'success.ticket.addMessage');
} else {
$this->utils->addFlash('error', $formAddTicketMessage->getErrors());
}

return $this->redirectToRoute(
'easyadmin',
[
'action' => 'show',
'entity' => 'Ticket',
'id' => $ticket->getId()
]
);
}

public function newAction()
{
$this->dispatch(EasyAdminEvents::PRE_NEW);

$entity = $this->executeDynamicMethod('createNew<EntityName>Entity');

$easyadmin = $this->request->attributes->get('easyadmin');
$easyadmin['item'] = $entity;
$this->request->attributes->set('easyadmin', $easyadmin);

$fields = $this->entity['new']['fields'];

$newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);

if ($this->request->request->get('user')) {
$userRepo = $this->em->getRepository(UserInterface::class);
$newForm->get('user')->setData($userRepo->find($this->request->request->get('user')));
}

if ($this->request->request->get('orderId')) {
$newForm->get('orderId')->setData($this->request->request->get('orderId'));
}


$newForm->handleRequest($this->request);

if ($newForm->isSubmitted() && $newForm->isValid()) {
if ($newForm->get('orderId')->getData()) {
$orderShopRepo = $this->em->getRepository(OrderShopInterface::class);
$orderShop = $orderShopRepo->find($newForm->get('orderId')->getData());
if ($orderShop) {
$entity->setOrderShop($orderShop);
}
}


$this->processUploadedFiles($newForm);

$this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]);
$this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
$this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);

$this->utils->addFlash('success', 'success.ticket.addMessage');

if ($this->request->isXmlHttpRequest()) {
$response['type'] = 'success';
$response['flashMessages'] = $this->utils->getFlashMessages();
return new Response(json_encode($response));
} else {
return $this->redirectToReferrer();
}
}
$this->dispatch(
EasyAdminEvents::POST_NEW,
[
'entity_fields' => $fields,
'form' => $newForm,
'entity' => $entity,
]
);

$parameters = [
'form' => $newForm->createView(),
'entity_fields' => $fields,
'entity' => $entity,
];
if ($this->request->isXmlHttpRequest()) {
$response['modal'] = $this->renderView('@LcShop/backend/ticket/modal/create.html.twig', $parameters);
return new Response(json_encode($response));
} else {
return $this->executeDynamicMethod(
'render<EntityName>Template',
['new', $this->entity['templates']['new'], $parameters]
);
}
}
}

+ 1
- 41
Factory/AbstractFactory.php View File

@@ -2,47 +2,7 @@

namespace Lc\SovBundle\Factory;

use Doctrine\ORM\EntityManagerInterface;

abstract class AbstractFactory
abstract class AbstractFactory implements FactoryInterface
{
protected $em ;

public function __construct(EntityManagerInterface $em)
{
$this->em = $em ;
}

public function create($params = array())
{
$entityClass = $this->em->getEntityName($this->getEntityClass());

if ($entityClass && strlen($entityClass) > 0) {
$entity = new $entityClass();
$this->populate($entity, $params);

return $entity;
}
else {
throw new \ErrorException('La classe de l\'entité n\'est pas définie.');
}
}

public function populate($entity, $params = array())
{
$entityArray = (array)$entity;

foreach ($entityArray as $name => $value) {
$nameArray = explode("\x00", $name);
$name = $nameArray[2];

if ($name != 'id' && isset($params[$name])) {
$setter = 'set' . ucfirst($name);

if (method_exists($entity, $setter)) {
$entity->$setter($params[$name]);
}
}
}
}
}

+ 8
- 0
Factory/FactoryInterface.php View File

@@ -0,0 +1,8 @@
<?php

namespace Lc\SovBundle\Factory;

interface FactoryInterface
{

}

+ 8
- 2
Factory/Reminder/ReminderFactory.php View File

@@ -8,9 +8,15 @@ use Lc\SovBundle\Model\Reminder\ReminderInterface;

class ReminderFactory extends AbstractFactory implements ReminderFactoryInterface
{
public function getEntityClass()
public function create(string $crudAction = null, string $crudControllerFqcn = null, int $entityId = null): ReminderInterface
{
return ReminderInterface::class;
$reminder = new Reminder();

$reminder->setCrudAction($crudAction);
$reminder->setCrudControllerFqcn($crudControllerFqcn);
$reminder->setEntityId($entityId);

return $reminder;
}

}

+ 5
- 3
Factory/Setting/SiteSettingFactory.php View File

@@ -2,13 +2,15 @@

namespace Lc\SovBundle\Factory\Setting;

use App\Entity\Setting\SiteSetting;
use Lc\SovBundle\Factory\AbstractFactory;
use Lc\SovBundle\Model\Setting\SiteSettingInterface;

class SiteSettingFactory extends AbstractFactory
{
public function getEntityClass()
public function create()
{
return SiteSettingInterface::class;
$siteSetting = new SiteSetting();

return $siteSetting;
}
}

+ 5
- 4
Factory/Site/SiteFactory.php View File

@@ -2,14 +2,15 @@

namespace Lc\SovBundle\Factory\Site;

use App\Entity\Site\Site;
use Lc\SovBundle\Factory\AbstractFactory;
use Lc\SovBundle\Model\Setting\SiteSettingInterface;
use Lc\SovBundle\Model\Site\SiteInterface;

class SiteFactory extends AbstractFactory
{
public function getEntityClass()
public function create()
{
return SiteInterface::class;
$site = new Site();

return $site;
}
}

+ 6
- 20
Factory/Ticket/TicketFactory.php View File

@@ -3,37 +3,23 @@
namespace Lc\SovBundle\Factory\Ticket;

use App\Entity\Ticket\Ticket;
use Doctrine\ORM\EntityManagerInterface;
use Lc\SovBundle\Factory\AbstractFactory;
use Lc\SovBundle\Model\Ticket\TicketInterface;

class TicketFactory extends AbstractFactory implements TicketFactoryInterface
{
protected $ticketMessageFactory;
protected TicketMessageFactory $ticketMessageFactory;

public function __construct(EntityManagerInterface $em, TicketMessageFactory $ticketMessageFactory)
public function __construct(TicketMessageFactory $ticketMessageFactory)
{
$this->ticketMessageFactory = $ticketMessageFactory;

parent::__construct($em);
}

public function getEntityClass()
public function create()
{
return TicketInterface::class;
}

public function create($params = array())
{
if (!isset($params['status'])) {
$params['status'] = Ticket::TICKET_STATUS_OPEN;
}

$ticket = parent::create($params);
$ticket = new Ticket();

if (!isset($params['ticketMessages'])) {
$ticket->addTicketMessage($this->ticketMessageFactory->create());
}
$ticketMessage = $this->ticketMessageFactory->create() ;
$ticket->addTicketMessage($ticketMessage) ;

return $ticket;
}

+ 5
- 10
Factory/Ticket/TicketMessageFactory.php View File

@@ -2,22 +2,17 @@

namespace Lc\SovBundle\Factory\Ticket;

use App\Entity\Ticket\TicketMessage;
use Lc\SovBundle\Factory\AbstractFactory;
use Lc\SovBundle\Model\Ticket\TicketMessageInterface;

class TicketMessageFactory extends AbstractFactory
{
public function getEntityClass()
public function create()
{
return TicketMessageInterface::class;
}
$ticketMessage = new TicketMessage();

public function create($params = array())
{
if (!isset($params['status'])) {
$params['status'] = 1;
}
$ticketMessage->setStatus(1);

return parent::create($params);
return $ticketMessage;
}
}

+ 4
- 11
Factory/User/UserFactory.php View File

@@ -2,22 +2,15 @@

namespace Lc\SovBundle\Factory\User;

use Doctrine\ORM\EntityManagerInterface;
use Lc\CaracoleBundle\Resolver\MerchantResolver;
use App\Entity\User\User;
use Lc\SovBundle\Factory\AbstractFactory;
use Lc\SovBundle\Model\User\UserInterface;

class UserFactory extends AbstractFactory
{
protected $merchantResolver;

public function __construct(EntityManagerInterface $em)
public function create()
{
parent::__construct($em);
}
$user = new User();

public function getEntityClass()
{
return UserInterface::class;
return $user;
}
}

+ 1
- 1
Model/Ticket/TicketModel.php View File

@@ -44,7 +44,7 @@ abstract class TicketModel extends AbstractLightEntity implements TicketInterfac
/**
* @ORM\Column(type="string", length=32)
*/
protected $status;
protected $status = self::TICKET_STATUS_OPEN;

/**
* @ORM\Column(type="string", length=255)

+ 1
- 1
Resources/views/adminlte/block/navbar_header.html.twig View File

@@ -39,7 +39,7 @@
</li>

<li class="nav-item">
<a href="{{ path('sov_admin_reminder_render_modal') }}" class="btn btn-sm btn-outline-info btn-reminder" data-crud-action="{{ ea.request.query.get('crudAction') }}" data-crud-controller-fqcn="{{ ea.request.query.get('crudControllerFqcn') }}">
<a href="{{ path(sov_get_parameter('app.reminder.route_render_modal')) }}" class="btn btn-sm btn-outline-info btn-reminder" data-crud-action="{{ ea.request.query.get('crudAction') }}" data-crud-controller-fqcn="{{ ea.request.query.get('crudControllerFqcn') }}">
<i class="action-icon fa fa-plus"></i>
Pense-bête
</a>

+ 6
- 0
Twig/TwigExtension.php View File

@@ -55,6 +55,7 @@ class TwigExtension extends AbstractExtension
new TwigFunction('page_by_dev_alias', [$this, 'getPageByDevAlias']),
new TwigFunction('translated_urls', [$this, 'getTranslatedUrls']),
new TwigFunction('sov_get_reminders', [$this, 'getReminders']),
new TwigFunction('sov_get_parameter', [$this, 'getParameter']),
);
}

@@ -160,6 +161,11 @@ class TwigExtension extends AbstractExtension
return $this->parameterBag->get('lc_sov.homepage_route');
}

public function getParameter($name)
{
return $this->parameterBag->get($name) ;
}

public function getReminders()
{
$reminders = $this->em->getRepository(ReminderInterface::class)->findAll() ;

Loading…
Cancel
Save