· 5 years ago · Mar 01, 2021, 02:20 PM
1<?php
2
3
4namespace App\EventSubscriber;
5
6
7use Doctrine\ORM\EntityManagerInterface;
8use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
9use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10use Symfony\Component\HttpKernel\Event\ExceptionEvent;
11use Symfony\Component\HttpKernel\KernelEvents;
12use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
13use Symfony\Component\Security\Core\Security;
14
15class TicketCreationSubscriber implements EventSubscriberInterface
16{
17 private $tokenStorage;
18 private $em;
19 private $params;
20 private $security;
21
22 public function __construct(
23 TokenStorageInterface $tokenStorage,
24 EntityManagerInterface $em,
25 ParameterBagInterface $params,
26 Security $security
27 ) {
28 $this->tokenStorage = $tokenStorage;
29 $this->em = $em;
30 $this->params = $params;
31 $this->security = $security;
32 }
33
34 public static function getSubscribedEvents(): array
35 {
36 return [
37 KernelEvents::EXCEPTION => 'creationTicket',
38 ];
39 }
40
41 public function creationTicket(ExceptionEvent $event)
42 {
43 $request = $event->getRequest();
44 $user = $this->security->getUser();
45
46 // If user isn't fully authenticated.
47 $name = !$user ? $this->params->get('mailer_name') : $user->getUsername();
48 $email = !$user ? $this->params->get('mailer_email') : $user->getEmail();
49
50 $config = [
51 'url' => $this->params->get('url_api_osticket').'api/tickets.json', // URL to site.tld/api/tickets.json
52 'key' => $this->params->get('token_api_osticket') // API Key goes here
53 ];
54
55 $data = [
56 'email' => $email, // from email aka User/Client Email.
57 'name' => $name, // from name aka User/Client Name.
58 'subject' => 'Remonté de bug automatique', // test subject, aka Issue Summary.
59 'message' => $this->buildContentMessage($event), // test ticket body, aka Issue Details.
60 'priority' => '3', // Custom field.
61 'project' => 'Conservatoire de Menucourt (module adhérent)', // Custom field.
62 'ip' => $_SERVER['SERVER_NAME'], // Should be IP address of the machine thats trying to open the ticket.
63 ];
64
65 // Pre-checks.
66 function_exists('curl_version') or die('CURL support required');
67 function_exists('json_encode') or die('JSON support required');
68
69 // Set timeout.
70 set_time_limit(30);
71
72 // Curl post.
73 $ch = curl_init();
74 curl_setopt($ch, CURLOPT_URL, $config['url']);
75 curl_setopt($ch, CURLOPT_POST, 1);
76 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
77 curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.7');
78 curl_setopt($ch, CURLOPT_HEADER, true);
79 curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-API-Key: '.$config['key']));
80 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
81 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
82 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
83 $result=curl_exec($ch);
84 $code = curl_getinfo($ch);
85 curl_close($ch);
86
87 if ($code != 201)
88 dd('Unable to create ticket: '.$result);
89
90 $ticket_id = (int) $result;
91
92 if(isset($ticket_id) && $ticket_id!='')
93 {
94 echo "Ticket Created Sucessfully with number: " . $ticket_id;
95 }else{
96 echo "Ticket not created. Try again later.";
97
98 }
99
100 // If request is an AJAX call.
101 if ($request->isXmlHttpRequest()) {
102 return $event->setResponse('lol');
103 }
104
105 $event->setResponse('lol');
106 }
107
108 /**
109 * Construct the message to send at osTicket's API.
110 *
111 * @param ExceptionEvent $event The exception error object.
112 *
113 * @return string
114 */
115 private function buildContentMessage(ExceptionEvent $event): string
116 {
117 $str = 'data:text/html,';
118 $str .= '<p>'.$event->getThrowable()->getMessage().'</p>';
119 $str .= '<p>'.$event->getThrowable()->getFile().', line '.$event->getThrowable()->getLine().'</p>';
120 $str .= '<hr />';
121 $str .= $event->getThrowable()->getTraceAsString();
122
123 return $str;
124 }
125}