|
- <?php
-
- namespace Lc\ShopBundle\Services;
-
- use App\Entity\TicketMessage;
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\ShopBundle\Context\MerchantUtilsInterface;
- use Lc\ShopBundle\Context\OrderShopInterface;
- use Lc\ShopBundle\Context\TicketInterface;
- use Lc\ShopBundle\Context\TicketMessageInterface;
- use Lc\ShopBundle\Context\UserInterface;
- use Lc\ShopBundle\Model\Ticket;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Symfony\Component\HttpFoundation\File\Exception\FileException;
- use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
-
- class TicketUtils
- {
- protected $em;
- protected $merchantUtils;
- protected $mailUtils;
- protected $authorizationChecker;
- protected $parameterBag;
-
- public function __construct(
- EntityManagerInterface $em,
- MerchantUtilsInterface $merchantUtils,
- MailUtils $mailUtils,
- AuthorizationCheckerInterface $authorizationChecker,
- ParameterBagInterface $parameterBag
- ) {
- $this->em = $em;
- $this->merchantUtils = $merchantUtils;
- $this->mailUtils = $mailUtils;
- $this->authorizationChecker = $authorizationChecker;
- $this->parameterBag = $parameterBag;
- }
-
- public function getTicketsByUser($user)
- {
- $ticketRepo = $this->em->getRepository(TicketInterface::class);
-
- return $ticketRepo->findBy(array('user' => $user));
- }
-
- public function uploadImageTicketMessage($formTicket)
- {
- $image = $formTicket->get('image')->getData();
-
- if ($image) {
- $originalFilename = pathinfo($image->getClientOriginalName(), PATHINFO_FILENAME);
- $newFilename = uniqid().'.'.$image->guessExtension();
-
- try {
- $image->move(
- $this->parameterBag->get('app.ticket_images_directory'),
- $newFilename
- );
- } catch (FileException $e) {
- throw new \ErrorException("Une erreur est survenue lors de l'upload du fichier.");
- }
-
- return $this->parameterBag->get('app.ticket_images_subdirectory').$newFilename;
- }
-
- return false;
- }
-
- public function createTicket($params): TicketInterface
- {
- $classTicket = $this->em->getClassMetadata(TicketInterface::class)->getName();
- $ticket = new $classTicket;
- $ticket->setMerchant($this->merchantUtils->getMerchantCurrent());
- $ticket->setStatus(1);
-
- if (isset($params['user'])) {
- $ticket->setUser($params['user']);
-
- $email = $params['user']->getEmail();
- $firstname = $params['user']->getFirstname();
- } else {
- $ticket->setVisitorFirstname($params['visitorFirstname']);
- $ticket->setVisitorLastname($params['visitorLastname']);
- $ticket->setVisitorEmail($params['visitorEmail']);
- $ticket->setVisitorToken(uniqid());
-
- $email = $params['visitorEmail'];
- $firstname = $params['visitorFirstname'];
- }
-
- $ticket->setStatus(Ticket::TICKET_STATUS_OPEN);
- $ticket->setType($params['type']);
- if (isset($params['orderShop']) && $params['orderShop'] && $params['orderShop'] instanceof OrderShopInterface) {
- $ticket->setOrderShop($params['orderShop']);
- }
- $ticket->setSubject($params['subject']);
- $this->em->persist($ticket);
-
- $classTicketMessage = $this->em->getClassMetadata(TicketMessageInterface::class)->getName();
- $ticketMessage = new $classTicketMessage;
- $ticketMessage->setStatus(1);
- $ticketMessage->setTicket($ticket);
- $ticketMessage->setMessage($params['message']);
-
- if (isset($params['imageFilename']) && $params['imageFilename']) {
- $ticketMessage->setImageFilename($params['imageFilename']);
- }
-
- if (isset($params['createByAdmin']) && $params['createByAdmin']) {
- $ticketMessage->setAnswerByAdmin(true);
- }
- $this->em->persist($ticketMessage);
-
- $this->em->flush();
-
- if (isset($params['createByAdmin']) && $params['createByAdmin']) {
- // envoi email au client
- $this->mailUtils->send(
- [
- MailUtils::SUBJECT => 'Vous avez reçu un nouveau message',
- MailUtils::TO_EMAIL => $email,
- MailUtils::CONTENT_TEMPLATE => 'mail/ticket-new-by-admin',
- MailUtils::CONTENT_DATA => [
- 'firstname' => $firstname,
- 'ticket' => $ticket,
- ],
- ]
- );
- } else {
- $this->mailUtils->send(
- [
- MailUtils::SUBJECT => 'Nouvelle demande',
- MailUtils::TO_EMAIL => $email,
- MailUtils::CONTENT_TEMPLATE => 'mail/ticket-new',
- MailUtils::CONTENT_DATA => [
- 'firstname' => $firstname,
- 'ticket' => $ticket,
- ],
- ]
- );
- }
-
-
- $this->notifyAdmin($ticket, $ticketMessage);
-
- return $ticket;
- }
-
- public function createTicketMessage($params)
- {
- $classTicketMessage = $this->em->getClassMetadata(TicketMessageInterface::class)->getName();
- $ticketMessage = new $classTicketMessage;
-
- $ticket = $params['ticket'];
-
- $ticketMessage->setStatus(1);
- $ticketMessage->setTicket($ticket);
- $ticketMessage->setMessage($params['message']);
- if (isset($params['answerByAdmin']) && $params['answerByAdmin']) {
- $ticketMessage->setAnswerByAdmin($params['answerByAdmin']);
-
- // envoi email au client
- $this->mailUtils->send(
- [
- MailUtils::SUBJECT => 'Réponse à votre demande',
- MailUtils::TO_EMAIL => $ticket->getUser() ? $ticket->getUser()->getEmail(
- ) : $ticket->getVisitorEmail(),
- MailUtils::CONTENT_TEMPLATE => 'mail/ticket-response',
- MailUtils::CONTENT_DATA => [
- 'firstname' => $ticket->getUser() ? $ticket->getUser()->getFirstname(
- ) : $ticket->getVisitorFirstname(),
- 'ticket' => $ticket,
- ],
- ]
- );
- }
- $this->em->persist($ticketMessage);
-
- if (isset($params['imageFilename']) && $params['imageFilename']) {
- $ticketMessage->setImageFilename($params['imageFilename']);
- }
- if (isset($params['closeTicket']) && $params['closeTicket']) {
- $ticket->setStatus(Ticket::TICKET_STATUS_CLOSED);
- }
-
- $ticket->setUpdatedAt(new \DateTime());
- $this->em->persist($ticket);
-
- $this->em->flush();
-
- return $ticketMessage;
- }
-
- public function notifyAdmin($ticket, $ticketMessage)
- {
- $userRepo = $this->em->getRepository(UserInterface::class);
- $usersToNotify = $userRepo->findByTicketTypesNotification($ticket->getType());
-
-
- foreach ($usersToNotify as $userToNotify) {
- if ($this->authorizationChecker->isGranted('ROLE_ADMIN', $userToNotify)) {
- // envoi email au client
- $this->mailUtils->send(
- [
- MailUtils::SUBJECT => 'Nouveau ticket sur placedulocal',
- MailUtils::TO_EMAIL => $userToNotify->getEmail(),
- MailUtils::CONTENT_TEMPLATE => 'mail/ticket-notification',
- MailUtils::CONTENT_DATA => [
- 'firstname' => $userToNotify->getFirstname(),
- 'ticket' => $ticket,
- 'ticketMessage' => $ticketMessage,
- ],
- ]
- );
- }
- }
- }
- }
|