vendor/uvdesk/api-bundle/API/Tickets.php line 27

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\ApiBundle\API;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. use Symfony\Component\Serializer\Serializer;
  10. use Symfony\Component\Serializer\Encoder\XmlEncoder;
  11. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  12. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Webkul\UVDesk\CoreFrameworkBundle\Entity as CoreFrameworkBundleEntity;
  15. use Webkul\UVDesk\CoreFrameworkBundle\Workflow\Events as CoreWorkflowEvents;
  16. use Webkul\UVDesk\CoreFrameworkBundle\Services\UVDeskService;
  17. class Tickets extends AbstractController
  18. {
  19.     /**
  20.      * Return support tickets.
  21.      *
  22.      * @param Request $request
  23.      */
  24.     public function fetchTickets(Request $requestContainerInterface $containerUVDeskService $uvdeskEntityManagerInterface $entityManager)
  25.     {
  26.         $json = [];
  27.         $ticketRepository $entityManager->getRepository(CoreFrameworkBundleEntity\Ticket::class);
  28.         $userRepository $entityManager->getRepository(CoreFrameworkBundleEntity\User::class);
  29.         if ($request->query->get('actAsType')) {
  30.             switch ($request->query->get('actAsType')) {
  31.                 case 'customer':
  32.                     $email $request->query->get('actAsEmail');
  33.                     $customer $entityManager->getRepository(CoreFrameworkBundleEntity\User::class)->findOneByEmail($email);
  34.                     if ($customer) {
  35.                         $json $ticketRepository->getAllCustomerTickets($request->query$container$customer);
  36.                     } else {
  37.                         $json['error'] = $container->get('translator')->trans('Error! Resource not found.');
  38.                         return new JsonResponse($jsonResponse::HTTP_NOT_FOUND);
  39.                     }
  40.                     return new JsonResponse($json);
  41.                 case 'agent':
  42.                     $email $request->query->get('actAsEmail');
  43.                     $user $entityManager->getRepository(CoreFrameworkBundleEntity\User::class)->findOneByEmail($email);
  44.                     if ($user) {
  45.                         $request->query->set('agent'$user->getId());
  46.                     } else {
  47.                         $json['error'] = $container->get('translator')->trans('Error! Resource not found.');
  48.                         return new JsonResponse($jsonResponse::HTTP_NOT_FOUND);
  49.                     }
  50.                     break;
  51.                 default:
  52.                     $json['error'] = $container->get('translator')->trans('Error! invalid actAs details.');
  53.                     return new JsonResponse($jsonResponse::HTTP_BAD_REQUEST);
  54.             }
  55.         }
  56.         $json $ticketRepository->getAllTickets($request->query$container);
  57.         $collection $json['tickets'];
  58.         $pagination $json['pagination'];
  59.         // Resolve asset paths
  60.         $defaultAgentProfileImagePath $this->getParameter('assets_default_agent_profile_image_path');
  61.         $defaultCustomerProfileImagePath $this->getParameter('assets_default_customer_profile_image_path');
  62.         $user $this->getUser();
  63.         $userInstance $user->getCurrentInstance();
  64.         $currentUserDetails = [
  65.             'id'               => $user->getId(),
  66.             'email'            => $user->getEmail(),
  67.             'name'             => $user->getFirstName() . ' ' $user->getLastname(),
  68.             'profileImagePath' => $uvdesk->generateCompleteLocalResourcePathUri($userInstance->getProfileImagePath() ?? $defaultAgentProfileImagePath)
  69.         ];
  70.         foreach ($collection as $index => $ticket) {
  71.             // Resolve assets: Assigned agent
  72.             if (! empty($ticket['agent'])) {
  73.                 $profileImagePath $uvdesk->generateCompleteLocalResourcePathUri($ticket['agent']['profileImagePath'] ?? $defaultAgentProfileImagePath);
  74.                 $smallThumbnailPath $uvdesk->generateCompleteLocalResourcePathUri($ticket['agent']['smallThumbnail'] ?? $defaultAgentProfileImagePath);
  75.                 $collection[$index]['agent']['profileImagePath'] = $profileImagePath;
  76.                 $collection[$index]['agent']['smallThumbnail'] = $smallThumbnailPath;
  77.             }
  78.             // Resolve assets: Customer
  79.             if (! empty($ticket['customer'])) {
  80.                 $profileImagePath $uvdesk->generateCompleteLocalResourcePathUri($ticket['customer']['profileImagePath'] ?? $defaultCustomerProfileImagePath);
  81.                 $smallThumbnailPath $uvdesk->generateCompleteLocalResourcePathUri($ticket['customer']['smallThumbnail'] ?? $defaultCustomerProfileImagePath);
  82.                 $collection[$index]['customer']['profileImagePath'] = $profileImagePath;
  83.                 $collection[$index]['customer']['smallThumbnail'] = $smallThumbnailPath;
  84.             }
  85.         }
  86.         // Available helpdesk agents collection
  87.         $agents $container->get('user.service')->getAgentsPartialDetails();
  88.         return new JsonResponse([
  89.             'tickets'     => $collection,
  90.             'pagination'  => $pagination,
  91.             'userDetails' => $currentUserDetails,
  92.             'agents'      => $agents,
  93.             'status'      => $container->get('ticket.service')->getStatus(),
  94.             'group'       => $userRepository->getSupportGroups(),
  95.             'team'        => $userRepository->getSupportTeams(),
  96.             'priority'    => $container->get('ticket.service')->getPriorities(),
  97.             'type'        => $container->get('ticket.service')->getTypes(),
  98.             'source'      => $container->get('ticket.service')->getAllSources(),
  99.         ]);
  100.         return new JsonResponse($json);
  101.     }
  102.     /**
  103.      * Trash support tickets.
  104.      *
  105.      * @param Request $request
  106.      * @return void
  107.      */
  108.     public function trashTicket(Request $requestContainerInterface $containerEntityManagerInterface $entityManager)
  109.     {
  110.         $ticketId $request->attributes->get('ticketId');
  111.         $ticket $entityManager->getRepository(CoreFrameworkBundleEntity\Ticket::class)->find($ticketId);
  112.         if (! $ticket) {
  113.             throw new NotFoundHttpException('Ticket Not Found');
  114.         }
  115.         if (!$ticket->getIsTrashed()) {
  116.             $ticket->setIsTrashed(1);
  117.             $entityManager->persist($ticket);
  118.             $entityManager->flush();
  119.             $json['success'] = $container->get('translator')->trans('Success ! Ticket moved to trash successfully.');
  120.             $statusCode Response::HTTP_OK;
  121.             // Trigger ticket delete event
  122.             $event = new CoreWorkflowEvents\Ticket\Delete();
  123.             $event
  124.                 ->setTicket($ticket);
  125.             $container->get('event_dispatcher')->dispatch($event'uvdesk.automation.workflow.execute');
  126.         } else {
  127.             $json['error'] = $container->get('translator')->trans('Warning ! Ticket is already in trash.');
  128.             $statusCode Response::HTTP_BAD_REQUEST;
  129.         }
  130.         return new JsonResponse($json$statusCode);
  131.     }
  132.     /**
  133.      * Create support tickets.
  134.      * customFields data passed like :
  135.      * {
  136.      *   customFields[key] : value,
  137.      *   customFields[key] : value,
  138.      * }
  139.      *
  140.      * @param Request $request
  141.      * @return void
  142.      */
  143.     public function createTicket(Request $requestContainerInterface $containerEntityManagerInterface $entityManager)
  144.     {
  145.         $data $request->request->all() ?: json_decode($request->getContent(), true);
  146.         foreach ($data as $key => $value) {
  147.             if (!in_array($key, ['subject''group''type''status''locale''domain''priority''agent''replies''createdAt''updatedAt''customFields''files''from''name''message''tags''actAsType''actAsEmail'])) {
  148.                 unset($data[$key]);
  149.             }
  150.         }
  151.         if (
  152.             !(isset($data['from'])
  153.                 && isset($data['name'])
  154.                 && isset($data['subject'])
  155.                 && isset($data['message'])
  156.                 &&  isset($data['actAsType'])
  157.                 || isset($data['actAsEmail']))
  158.         ) {
  159.             $json['error'] = $container->get('translator')->trans('required fields: name, from, subject, message, actAsType or actAsEmail');
  160.             return new JsonResponse($jsonResponse::HTTP_BAD_REQUEST);
  161.         }
  162.         if ($data) {
  163.             $error false;
  164.             if ($data['subject'] == '') {
  165.                 $statusCode Response::HTTP_BAD_REQUEST;
  166.             } elseif ($data['message'] == '') {
  167.                 $json['message'] = $container->get('translator')->trans("Warning! Please complete message field value!");
  168.                 $statusCode Response::HTTP_BAD_REQUEST;
  169.             } elseif (filter_var($data['from'], FILTER_VALIDATE_EMAIL) === false) {
  170.                 $json['message'] = $container->get('translator')->trans("Warning! Invalid from Email Address!");
  171.                 $statusCode Response::HTTP_BAD_REQUEST;
  172.             } elseif ($data['actAsType'] == ''  &&  $data['actAsEmail'] == '') {
  173.                 $json['message'] = $container->get('translator')->trans("Warning! Provide atleast one parameter actAsType(agent or customer) or actAsEmail");
  174.                 $statusCode Response::HTTP_BAD_REQUEST;
  175.             }
  176.             if (! $error) {
  177.                 $name explode(' 'trim($data['name']));
  178.                 $ticketData['firstName'] = $name[0];
  179.                 $ticketData['lastName']  = isset($name[1]) ? $name[1] : '';
  180.                 $ticketData['role']      = 4;
  181.                 if (
  182.                     (array_key_exists('actAsType'$data))
  183.                     && (strtolower($data['actAsType']) == 'customer')
  184.                 ) {
  185.                     $actAsType strtolower(trim($data['actAsType']));
  186.                 } else if (
  187.                     (array_key_exists('actAsEmail'$data))
  188.                     && (strtolower(trim($data['actAsType'])) == 'agent')
  189.                 ) {
  190.                     $user $entityManager->getRepository(CoreFrameworkBundleEntity\User::class)->findOneByEmail(trim($data['actAsEmail']));
  191.                     if ($user) {
  192.                         $actAsType 'agent';
  193.                     } else {
  194.                         $json['error'] = $container->get('translator')->trans("Error ! actAsEmail is not valid");
  195.                         return new JsonResponse($jsonResponse::HTTP_BAD_REQUEST);
  196.                     }
  197.                 } else {
  198.                     $json['warning'] = $container->get('translator')->trans('Warning ! For Customer specify actAsType as customer and for Agent specify both parameter actASType  as agent and actAsEmail as agent email');
  199.                     $statusCode Response::HTTP_BAD_REQUEST;
  200.                     return new JsonResponse($json$statusCode);
  201.                 }
  202.                 // Create customer if account does not exists
  203.                 $customer $entityManager->getRepository(CoreFrameworkBundleEntity\User::class)->findOneByEmail($data['from']);
  204.                 if (
  205.                     empty($customer)
  206.                     || null == $customer->getCustomerInstance()
  207.                 ) {
  208.                     $role $entityManager->getRepository(CoreFrameworkBundleEntity\SupportRole::class)->findOneByCode('ROLE_CUSTOMER');
  209.                     // Create User Instance
  210.                     $customer $container->get('user.service')->createUserInstance($data['from'], $data['name'], $role, [
  211.                         'source' => 'api',
  212.                         'active' => true
  213.                     ]);
  214.                 }
  215.                 if ($actAsType == 'agent') {
  216.                     $data['user'] = isset($user) && $user $user $container->get('user.service')->getCurrentUser();
  217.                 } else {
  218.                     $data['user'] = $customer;
  219.                 }
  220.                 $attachments $request->files->get('attachments');
  221.                 if (! empty($attachments)) {
  222.                     $attachments is_array($attachments) ? $attachments : [$attachments];
  223.                 }
  224.                 $ticketData['user']        = $data['user'];
  225.                 $ticketData['subject']     = trim($data['subject']);
  226.                 $ticketData['message']     = trim($data['message']);
  227.                 $ticketData['customer']    = $customer;
  228.                 $ticketData['source']      = 'api';
  229.                 $ticketData['threadType']  = 'create';
  230.                 $ticketData['createdBy']   = $actAsType;
  231.                 $ticketData['attachments'] = $attachments;
  232.                 $extraKeys = ['tags''group''priority''status''agent''createdAt''updatedAt'];
  233.                 if (array_key_exists('type'$data)) {
  234.                     $ticketType $entityManager->getRepository(CoreFrameworkBundleEntity\TicketType::class)->findOneByCode($data['type']);
  235.                     $ticketData['type'] = $ticketType;
  236.                 }
  237.                 foreach ($extraKeys as $key) {
  238.                     if (isset($ticketData[$key])) {
  239.                         unset($ticketData[$key]);
  240.                     }
  241.                 }
  242.                 $thread $container->get('ticket.service')->createTicketBase($ticketData);
  243.                 $customFields $request->request->get('customFields');
  244.                 if ($customFields) {
  245.                     $container->get('ticket.service')->addTicketCustomFields($thread$customFields$customFields);
  246.                 }
  247.                 // Trigger ticket created event
  248.                 try {
  249.                     $event = new CoreWorkflowEvents\Ticket\Create();
  250.                     $event
  251.                         ->setTicket($thread->getTicket());
  252.                     $container->get('event_dispatcher')->dispatch($event'uvdesk.automation.workflow.execute');
  253.                 } catch (\Exception $e) {
  254.                 }
  255.                 $user $this->getUser();
  256.                 $userService $container->get('user.service');
  257.                 $json['message'] = $container->get('translator')->trans('Success ! Ticket has been created successfully.');
  258.                 $json['ticket'] = [
  259.                     'id'        => $thread->getTicket()->getId(),
  260.                     'status'    => $thread->getTicket()->getStatus()->getId(),
  261.                     'createdAt' => $userService->getLocalizedFormattedTime($thread->getTicket()->getCreatedAt(), $user),
  262.                     'updatedAt' => $userService->getLocalizedFormattedTime($thread->getTicket()->getUpdatedAt(), $user),
  263.                 ];
  264.                 $statusCode Response::HTTP_OK;
  265.             } else {
  266.                 $json['message'] = $container->get('translator')->trans('Warning ! Required parameters should not be blank');
  267.                 $statusCode Response::HTTP_BAD_REQUEST;
  268.             }
  269.         } else {
  270.             $json['error'] = $container->get('translator')->trans('invalid/empty size of Request');
  271.             $json['message'] = $container->get('translator')->trans('Warning ! Post size can not exceed 25MB');
  272.             $statusCode Response::HTTP_BAD_REQUEST;
  273.         }
  274.         return new JsonResponse($json$statusCode);
  275.     }
  276.     /**
  277.      * View support tickets.
  278.      *
  279.      * @param Request $request
  280.      * @return void
  281.      */
  282.     public function viewTicket($ticketIdContainerInterface $containerUVDeskService $uvdeskEntityManagerInterface $entityManager)
  283.     {
  284.         $userRepository $entityManager->getRepository(CoreFrameworkBundleEntity\User::class);
  285.         $ticketRepository $entityManager->getRepository(CoreFrameworkBundleEntity\Ticket::class);
  286.         $ticket $ticketRepository->findOneById($ticketId);
  287.         if (empty($ticket)) {
  288.             throw new \Exception('No ticket found');
  289.         }
  290.         $user $this->getUser();
  291.         $agent $ticket->getAgent();
  292.         $customer $ticket->getCustomer();
  293.         $defaultAgentProfileImagePath $this->getParameter('assets_default_agent_profile_image_path');
  294.         $defaultCustomerProfileImagePath $this->getParameter('assets_default_customer_profile_image_path');
  295.         $agentDetails = !empty($agent) ? $agent->getAgentInstance()->getPartialDetails() : null;
  296.         $customerDetails $customer->getCustomerInstance()->getPartialDetails();
  297.         if (! empty($agentDetails)) {
  298.             $agentDetails['thumbnail'] = $uvdesk->generateCompleteLocalResourcePathUri($agentDetails['thumbnail'] ?? $defaultAgentProfileImagePath);
  299.         }
  300.         if (! empty($agentDetails)) {
  301.             $customerDetails['thumbnail'] = $uvdesk->generateCompleteLocalResourcePathUri($customerDetails['thumbnail'] ?? $defaultCustomerProfileImagePath);
  302.         }
  303.         // Mark as viewed by agents
  304.         if (false == $ticket->getIsAgentViewed()) {
  305.             $ticket
  306.                 ->setIsAgentViewed(true);
  307.             $entityManager->persist($ticket);
  308.             $entityManager->flush();
  309.         }
  310.         // Ticket status Collection
  311.         $status array_map(function ($statusCollection) {
  312.             return [
  313.                 'id'          => $statusCollection->getId(),
  314.                 'code'        => $statusCollection->getCode(),
  315.                 'colorCode'   => $statusCollection->getColorCode(),
  316.                 'description' => $statusCollection->getDescription(),
  317.             ];
  318.         }, $entityManager->getRepository(CoreFrameworkBundleEntity\TicketStatus::class)->findAll());
  319.         // Ticket Type Collection
  320.         $type array_map(function ($ticketTypeCollection) {
  321.             return [
  322.                 'id'          => $ticketTypeCollection->getId(),
  323.                 'code'        => $ticketTypeCollection->getCode(),
  324.                 'isActive'    => $ticketTypeCollection->getIsActive(),
  325.                 'description' => $ticketTypeCollection->getDescription(),
  326.             ];
  327.         }, $entityManager->getRepository(CoreFrameworkBundleEntity\TicketType::class)->findByIsActive(true));
  328.         // Priority Collection
  329.         $priority array_map(function ($ticketPriorityCollection) {
  330.             return [
  331.                 'id'          => $ticketPriorityCollection->getId(),
  332.                 'code'        => $ticketPriorityCollection->getCode(),
  333.                 'colorCode'   => $ticketPriorityCollection->getColorCode(),
  334.                 'description' => $ticketPriorityCollection->getDescription(),
  335.             ];
  336.         }, $entityManager->getRepository(CoreFrameworkBundleEntity\TicketPriority::class)->findAll());
  337.         $userService $container->get('user.service');
  338.         $fileSystemService =  $container->get('uvdesk.core.file_system.service');
  339.         $supportGroup $ticket->getSupportGroup();
  340.         if (! empty($supportGroup)) {
  341.             $supportGroup = [
  342.                 'id'   => $supportGroup->getId(),
  343.                 'name' => $supportGroup->getName(),
  344.             ];
  345.         }
  346.         $supportTeam $ticket->getSupportTeam();
  347.         if (! empty($supportTeam)) {
  348.             $supportTeam = [
  349.                 'id'   => $supportTeam->getId(),
  350.                 'name' => $supportTeam->getName(),
  351.             ];
  352.         }
  353.         $ticketDetails = [
  354.             'id'               => $ticket->getId(),
  355.             'source'           => $ticket->getSource(),
  356.             'priority'         => $ticket->getPriority()->getId(),
  357.             'status'           => $ticket->getStatus()->getId(),
  358.             'subject'          => $ticket->getSubject(),
  359.             'isNew'            => $ticket->getIsNew(),
  360.             'isReplied'        => $ticket->getIsReplied(),
  361.             'isReplyEnabled'   => $ticket->getIsReplyEnabled(),
  362.             'isStarred'        => $ticket->getIsStarred(),
  363.             'isTrashed'        => $ticket->getIsTrashed(),
  364.             'isAgentViewed'    => $ticket->getIsAgentViewed(),
  365.             'isCustomerViewed' => $ticket->getIsCustomerViewed(),
  366.             'createdAt'        => $userService->getLocalizedFormattedTime($ticket->getCreatedAt(), $user),
  367.             'updatedAt'        => $userService->getLocalizedFormattedTime($ticket->getUpdatedAt(), $user),
  368.             'group'            => $supportGroup,
  369.             'team'             => $supportTeam,
  370.         ];
  371.         $threads array_map(function ($thread) use ($uvdesk$userService$fileSystemService$defaultAgentProfileImagePath$defaultCustomerProfileImagePath) {
  372.             $user $thread->getUser();
  373.             $userInstance $thread->getCreatedBy() == 'agent' $user->getAgentInstance() : $user->getCustomerInstance();
  374.             $attachments array_map(function ($attachment) use ($fileSystemService) {
  375.                 return $fileSystemService->getFileTypeAssociations($attachment);
  376.             }, $thread->getAttachments()->getValues());
  377.             $thumbnail $uvdesk->generateCompleteLocalResourcePathUri($userInstance->getProfileImagePath() ?? ($thread->getCreatedBy() == 'agent' $defaultAgentProfileImagePath $defaultCustomerProfileImagePath));
  378.             return [
  379.                 'id'           => $thread->getId(),
  380.                 'source'       => $thread->getSource(),
  381.                 'threadType'   => $thread->getThreadType(),
  382.                 'createdBy'    => $thread->getCreatedBy(),
  383.                 'cc'           => $thread->getCc(),
  384.                 'bcc'          => $thread->getBcc(),
  385.                 'isLocked'     => $thread->getIsLocked(),
  386.                 'isBookmarked' => $thread->getIsBookmarked(),
  387.                 'message'      => $thread->getMessage(),
  388.                 'source'       => $thread->getSource(),
  389.                 'createdAt'    => $userService->getLocalizedFormattedTime($thread->getCreatedAt(), $user),
  390.                 'updatedAt'    => $userService->getLocalizedFormattedTime($thread->getUpdatedAt(), $user),
  391.                 'user' => [
  392.                     'id'        => $user->getId(),
  393.                     'name'      => $user->getFullName(),
  394.                     'email'     => $user->getEmail(),
  395.                     'thumbnail' => $thumbnail,
  396.                 ],
  397.                 'attachments' => $attachments,
  398.             ];
  399.         }, $ticket->getThreads()->getValues());
  400.         $ticketDetails['threads'] = $threads;
  401.         $ticketDetails['agent'] = $agentDetails;
  402.         $ticketDetails['customer'] = $customerDetails;
  403.         $ticketDetails['totalThreads'] = count($threads);
  404.         $customFields = [];
  405.         if ($userService->isFileExists('apps/uvdesk/custom-fields')) {
  406.             $customFieldsService $this->container->get('uvdesk_package_custom_fields.service');
  407.             $customFields =  $customFieldsService->getTicketCustomFieldDetails($ticket->getId());
  408.         }
  409.         return new JsonResponse([
  410.             'ticket'                => $ticketDetails,
  411.             'totalCustomerTickets'  => ($ticketRepository->countCustomerTotalTickets($customer$container)),
  412.             'supportGroups'         => $userRepository->getSupportGroups(),
  413.             'supportTeams'          => $userRepository->getSupportTeams(),
  414.             'ticketStatuses'        => $status,
  415.             'ticketPriorities'      => $priority,
  416.             'ticketTypes'           => $type,
  417.             'customFields'          => $customFields
  418.         ]);
  419.     }
  420.     /**
  421.      * delete support tickets.
  422.      *
  423.      * @param Request $request
  424.      * @return void
  425.      */
  426.     public function deleteTicketForever(Request $requestContainerInterface $containerEntityManagerInterface $entityManager)
  427.     {
  428.         $ticketId $request->attributes->get('ticketId');
  429.         $ticket $entityManager->getRepository(CoreFrameworkBundleEntity\Ticket::class)->find($ticketId);
  430.         if (! $ticket) {
  431.             throw new NotFoundHttpException('No ticket Found');
  432.         }
  433.         if ($ticket->getIsTrashed()) {
  434.             $entityManager->remove($ticket);
  435.             $entityManager->flush();
  436.             $json['success'] = $container->get('translator')->trans('Success ! Ticket removed successfully.');
  437.             $statusCode Response::HTTP_OK;
  438.             // Trigger ticket delete event
  439.             $event = new CoreWorkflowEvents\Ticket\Delete();
  440.             $event
  441.                 ->setTicket($ticket);
  442.             $container->get('event_dispatcher')->dispatch($event'uvdesk.automation.workflow.execute');
  443.         } else {
  444.             $json['error'] = $container->get('translator')->trans('Warning ! something went wrong.');
  445.             $statusCode Response::HTTP_BAD_REQUEST;
  446.         }
  447.         return new JsonResponse($json$statusCode);
  448.     }
  449.     /**
  450.      * Assign Ticket to a agent
  451.      *
  452.      * @param Request $request
  453.      * @return void
  454.      */
  455.     public function assignAgent(Request $requestContainerInterface $containerEntityManagerInterface $entityManager)
  456.     {
  457.         $json = [];
  458.         $data $request->request->all() ?: json_decode($request->getContent(), true);
  459.         $ticketId $request->attributes->get('ticketId');
  460.         $ticket $entityManager->getRepository(CoreFrameworkBundleEntity\Ticket::class)->findOneBy(array('id' => $ticketId));
  461.         if ($ticket) {
  462.             if (isset($data['id'])) {
  463.                 $agent $entityManager->getRepository(CoreFrameworkBundleEntity\User::class)->find($data['id']);
  464.             } else {
  465.                 $json['error'] = $container->get('translator')->trans('missing fields');
  466.                 $json['description'] = $container->get('translator')->trans('required: id ');
  467.                 return new JsonResponse($jsonResponse::HTTP_BAD_REQUEST);
  468.             }
  469.             if ($agent) {
  470.                 if ($ticket->getAgent() != $agent) {
  471.                     if ($ticket->getIsTrashed()) {
  472.                         $json['status'] = false;
  473.                         $json['error'] = $container->get('translator')->trans('Tickets is in trashed can not assign to agent.');
  474.                         return new JsonResponse($jsonResponse::HTTP_BAD_REQUEST);
  475.                     }
  476.                     $ticket->setAgent($agent);
  477.                     $entityManager->persist($ticket);
  478.                     $entityManager->flush();
  479.                     $json['success'] = $container->get('translator')->trans('Success ! Ticket assigned to agent successfully.');
  480.                     $statusCode Response::HTTP_OK;
  481.                     // Trigger ticket delete event
  482.                     $event = new CoreWorkflowEvents\Ticket\Agent();
  483.                     $event
  484.                         ->setTicket($ticket);
  485.                     $container->get('event_dispatcher')->dispatch($event'uvdesk.automation.workflow.execute');
  486.                 } else {
  487.                     $json['error'] = $container->get('translator')->trans('invalid resource');
  488.                     $json['description'] = $container->get('translator')->trans('Error ! Invalid agent or already assigned for this ticket');
  489.                     $statusCode Response::HTTP_NOT_FOUND;
  490.                 }
  491.             }
  492.         } else {
  493.             $json['error'] = $container->get('translator')->trans('invalid ticket');
  494.             $statusCode Response::HTTP_NOT_FOUND;
  495.         }
  496.         return new JsonResponse($json$statusCode);
  497.     }
  498.     /**
  499.      * adding  or removing collaborator to a Ticket
  500.      *
  501.      * @param Request $request
  502.      * @return void
  503.      */
  504.     public function addRemoveTicketCollaborator(Request $requestContainerInterface $containerEntityManagerInterface $entityManager)
  505.     {
  506.         $json = [];
  507.         $statusCode Response::HTTP_OK;
  508.         $content $request->request->all() ?: json_decode($request->getContent(), true);
  509.         $ticket $entityManager->getRepository(CoreFrameworkBundleEntity\Ticket::class)->find($request->attributes->get('ticketId'));
  510.         if (! $ticket) {
  511.             $json['error'] =  $container->get('translator')->trans('resource not found');
  512.             return new JsonResponse($jsonResponse::HTTP_NOT_FOUND);
  513.         }
  514.         if (
  515.             $request->getMethod() == "POST"
  516.             && ! (isset($content['id']))
  517.         ) {
  518.             if (
  519.                 ! isset($content['email'])
  520.                 || ! filter_var($content['email'], FILTER_VALIDATE_EMAIL)
  521.             ) {
  522.                 $json['error'] = $container->get('translator')->trans('missing/invalid field');
  523.                 $json['message'] = $container->get('translator')->trans('required: email');
  524.                 return new JsonResponse($jsonResponse::HTTP_BAD_REQUEST);
  525.             }
  526.             if ($content['email'] == $ticket->getCustomer()->getEmail()) {
  527.                 $json['error'] = $container->get('translator')->trans('Error ! Can not add customer as a collaborator.');
  528.                 $statusCode Response::HTTP_BAD_REQUEST;
  529.             } else {
  530.                 $data = array(
  531.                     'from'      => $content['email'],
  532.                     'firstName' => ($firstName ucfirst(current(explode('@'$content['email'])))),
  533.                     'lastName'  => '',
  534.                     'role'      => 4,
  535.                 );
  536.                 $supportRole $entityManager->getRepository(CoreFrameworkBundleEntity\SupportRole::class)->findOneByCode('ROLE_CUSTOMER');
  537.                 $collaborator $container->get('user.service')->createUserInstance($data['from'], $data['firstName'], $supportRole$extras = ["active" => true]);
  538.                 $checkTicket $entityManager->getRepository(CoreFrameworkBundleEntity\Ticket::class)->isTicketCollaborator($ticket$content['email']);
  539.                 if (! $checkTicket) {
  540.                     $ticket->addCollaborator($collaborator);
  541.                     $entityManager->persist($ticket);
  542.                     $entityManager->flush();
  543.                     $ticket->lastCollaborator $collaborator;
  544.                     $json['collaborator'] = $collaborator->getCustomerInstance() ? $collaborator->getCustomerInstance()->getPartialDetails() : $collaborator->getAgentInstance()->getPartialDetails();
  545.                     $event = new CoreWorkflowEvents\Ticket\Collaborator();
  546.                     $event
  547.                         ->setTicket($ticket);
  548.                     $container->get('event_dispatcher')->dispatch($event'uvdesk.automation.workflow.execute');
  549.                     $json['success'] =  $container->get('translator')->trans('Success ! Collaborator added successfully.');
  550.                     $statusCode Response::HTTP_OK;
  551.                 } else {
  552.                     $json['warning'] =  $container->get('translator')->trans('Collaborator is already added.');
  553.                     $statusCode Response::HTTP_BAD_REQUEST;
  554.                 }
  555.             }
  556.         } elseif ($request->getMethod() == "POST"  &&  isset($content['id'])) {
  557.             $collaborator $entityManager->getRepository(CoreFrameworkBundleEntity\User::class)->findOneBy(array('id' => $content['id']));
  558.             if ($collaborator) {
  559.                 $ticket->removeCollaborator($collaborator);
  560.                 $entityManager->persist($ticket);
  561.                 $entityManager->flush();
  562.                 $json['success'] =  $container->get('translator')->trans('Success ! Collaborator removed successfully.');
  563.                 $statusCode Response::HTTP_OK;
  564.             } else {
  565.                 $json['error'] =  $container->get('translator')->trans('Error ! Invalid Collaborator.');
  566.                 $statusCode Response::HTTP_BAD_REQUEST;
  567.             }
  568.         }
  569.         return new JsonResponse($json$statusCode);
  570.     }
  571.     /**
  572.      * Download ticket attachment
  573.      *
  574.      * @param Request $request
  575.      * @return void
  576.      */
  577.     public function downloadAttachment(Request $requestContainerInterface $containerEntityManagerInterface $entityManager)
  578.     {
  579.         $attachmentId $request->attributes->get('attachmentId');
  580.         $attachmentRepository $entityManager->getRepository(CoreFrameworkBundleEntity\Attachment::class);
  581.         $attachment $attachmentRepository->findOneById($attachmentId);
  582.         if (! $attachment) {
  583.             throw new NotFoundHttpException('Attachment Not Found');
  584.         }
  585.         $path $container->get('kernel')->getProjectDir() . "/public/" $attachment->getPath();
  586.         $response = new Response();
  587.         $response->setStatusCode(200);
  588.         $response->headers->set('Content-type'$attachment->getContentType());
  589.         $response->headers->set('Content-Disposition''attachment; filename=' $attachment->getName());
  590.         $response->sendHeaders();
  591.         $response->setContent(readfile($path));
  592.         return $response;
  593.     }
  594.     /**
  595.      * Download Zip attachment
  596.      *
  597.      * @param Request $request
  598.      * @return void
  599.      */
  600.     public function downloadZipAttachment(Request $requestEntityManagerInterface $entityManager)
  601.     {
  602.         $threadId $request->attributes->get('threadId');
  603.         $attachmentRepository $entityManager->getRepository(CoreFrameworkBundleEntity\Attachment::class);
  604.         $attachment $attachmentRepository->findByThread($threadId);
  605.         if (! $attachment) {
  606.             throw new NotFoundHttpException('Page Not Found');
  607.         }
  608.         $zipname 'attachments/' $threadId '.zip';
  609.         $zip = new \ZipArchive;
  610.         $zip->open($zipname\ZipArchive::CREATE);
  611.         if (count($attachment)) {
  612.             foreach ($attachment as $attach) {
  613.                 $zip->addFile(substr($attach->getPath(), 1));
  614.             }
  615.         }
  616.         $zip->close();
  617.         $response = new Response();
  618.         $response->setStatusCode(200);
  619.         $response->headers->set('Content-type''application/zip');
  620.         $response->headers->set('Content-Disposition''attachment; filename=' $threadId '.zip');
  621.         $response->sendHeaders();
  622.         $response->setContent(readfile($zipname));
  623.         return $response;
  624.     }
  625.     /**
  626.      * Edit Ticket properties
  627.      *
  628.      * @param Request $request
  629.      * @return void
  630.      */
  631.     public function editTicketProperties(Request $requestContainerInterface $containerEntityManagerInterface $entityManager)
  632.     {
  633.         $json = [];
  634.         $statusCode Response::HTTP_OK;
  635.         $requestContent $request->request->all() ?: json_decode($request->getContent(), true);
  636.         $ticketId =  $request->attributes->get('ticketId');
  637.         $ticket $entityManager->getRepository(CoreFrameworkBundleEntity\Ticket::class)->findOneById($ticketId);
  638.         // Validate request integrity
  639.         if (empty($ticket)) {
  640.             $json['error']  = 'invalid resource';
  641.             $json['description'] =  $container->get('translator')->trans('Unable to retrieve details for ticket #%ticketId%.', [
  642.                 '%ticketId%' => $ticketId,
  643.             ]);
  644.             $statusCode Response::HTTP_NOT_FOUND;
  645.             return new JsonResponse($json$statusCode);
  646.         } else if (!isset($requestContent['property'])) {
  647.             $json['error']  =  $container->get('translator')->trans('missing resource');
  648.             $json['description'] = $container->get('translator')->trans('Insufficient details provided.');
  649.             $statusCode Response::HTTP_BAD_REQUEST;
  650.             return new JsonResponse($json$statusCode);
  651.         }
  652.         // Update property
  653.         switch ($requestContent['property']) {
  654.             case 'agent':
  655.                 $agent $entityManager->getRepository(CoreFrameworkBundleEntity\User::class)->findOneById($requestContent['value']);
  656.                 if (empty($agent)) {
  657.                     // User does not exist
  658.                     $json['error']  = $container->get('translator')->trans('No such user exist');
  659.                     $json['description'] = $container->get('translator')->trans('Unable to retrieve agent details');
  660.                     $statusCode Response::HTTP_BAD_REQUEST;
  661.                     return new JsonResponse($json$statusCode);
  662.                 } else {
  663.                     // Check if an agent instance exists for the user
  664.                     $agentInstance $agent->getAgentInstance();
  665.                     if (empty($agentInstance)) {
  666.                         // Agent does not exist
  667.                         $json['error']  = $container->get('translator')->trans('No such user exist');
  668.                         $json['description'] = $container->get('translator')->trans('Unable to retrieve agent details');
  669.                         $statusCode Response::HTTP_BAD_REQUEST;
  670.                         return new JsonResponse($json$statusCode);
  671.                     }
  672.                 }
  673.                 $agentDetails $agentInstance->getPartialDetails();
  674.                 // Check if ticket is already assigned to the agent
  675.                 if (
  676.                     $ticket->getAgent()
  677.                     && $agent->getId() === $ticket->getAgent()->getId()
  678.                 ) {
  679.                     $json['success']  = $container->get('translator')->trans('Already assigned');
  680.                     $json['description'] = $container->get('translator')->trans('Ticket already assigned to %agent%', [
  681.                         '%agent%' => $agentDetails['name']
  682.                     ]);
  683.                     $statusCode Response::HTTP_OK;
  684.                     return new JsonResponse($json$statusCode);
  685.                 } else {
  686.                     $ticket->setAgent($agent);
  687.                     $entityManager->persist($ticket);
  688.                     $entityManager->flush();
  689.                     // Trigger Agent Assign event
  690.                     $event = new CoreWorkflowEvents\Ticket\Agent();
  691.                     $event
  692.                         ->setTicket($ticket);
  693.                     $container->get('event_dispatcher')->dispatch($event'uvdesk.automation.workflow.execute');
  694.                     $json['success']  = $container->get('translator')->trans('Success');
  695.                     $json['description'] = $container->get('translator')->trans('Ticket successfully assigned to %agent%', [
  696.                         '%agent%' => $agentDetails['name'],
  697.                     ]);
  698.                     $statusCode Response::HTTP_OK;
  699.                     return new JsonResponse($json$statusCode);
  700.                 }
  701.                 break;
  702.             case 'status':
  703.                 $ticketStatus $entityManager->getRepository(CoreFrameworkBundleEntity\TicketStatus::class)->findOneById((int) $requestContent['value']);
  704.                 if (empty($ticketStatus)) {
  705.                     // Selected ticket status does not exist
  706.                     $json['error']  = $container->get('translator')->trans('Error');
  707.                     $json['description'] = $container->get('translator')->trans('Unable to retrieve status details');
  708.                     $statusCode Response::HTTP_BAD_REQUEST;
  709.                     return new JsonResponse($json$statusCode);
  710.                 }
  711.                 if ($ticketStatus->getId() === $ticket->getStatus()->getId()) {
  712.                     $json['success']  = $container->get('translator')->trans('Success');
  713.                     $json['description'] = $container->get('translator')->trans('Ticket status already set to %status%', [
  714.                         '%status%' => $ticketStatus->getDescription()
  715.                     ]);
  716.                     $statusCode Response::HTTP_OK;
  717.                     return new JsonResponse($json$statusCode);
  718.                 } else {
  719.                     $ticket->setStatus($ticketStatus);
  720.                     $entityManager->persist($ticket);
  721.                     $entityManager->flush();
  722.                     // Trigger ticket status event
  723.                     $event = new CoreWorkflowEvents\Ticket\Status();
  724.                     $event
  725.                         ->setTicket($ticket);
  726.                     $container->get('event_dispatcher')->dispatch($event'uvdesk.automation.workflow.execute');
  727.                     $json['success']  =  $container->get('translator')->trans('Success');
  728.                     $json['description'] =  $container->get('translator')->trans('Ticket status update to %status%', [
  729.                         '%status%' => $ticketStatus->getDescription()
  730.                     ]);
  731.                     $statusCode Response::HTTP_OK;
  732.                     return new JsonResponse($json$statusCode);
  733.                 }
  734.                 break;
  735.             case 'priority':
  736.                 $ticketPriority $entityManager->getRepository(CoreFrameworkBundleEntity\TicketPriority::class)->findOneById($requestContent['value']);
  737.                 if (empty($ticketPriority)) {
  738.                     // Selected ticket priority does not exist
  739.                     $json['error']  = $container->get('translator')->trans('Error');
  740.                     $json['description'] =  $container->get('translator')->trans('Unable to retrieve priority details');
  741.                     $statusCode Response::HTTP_BAD_REQUEST;
  742.                     return new JsonResponse($json$statusCode);
  743.                 }
  744.                 if ($ticketPriority->getId() === $ticket->getPriority()->getId()) {
  745.                     $json['success']  = $container->get('translator')->trans('Success');
  746.                     $json['description'] =  $container->get('translator')->trans('Ticket priority already set to %priority%', [
  747.                         '%priority%' => $ticketPriority->getDescription()
  748.                     ]);
  749.                     $statusCode Response::HTTP_OK;
  750.                     return new JsonResponse($json$statusCode);
  751.                 } else {
  752.                     $ticket->setPriority($ticketPriority);
  753.                     $entityManager->persist($ticket);
  754.                     $entityManager->flush();
  755.                     // Trigger ticket Priority event
  756.                     $event = new CoreWorkflowEvents\Ticket\Priority();
  757.                     $event
  758.                         ->setTicket($ticket);
  759.                     $container->get('event_dispatcher')->dispatch($event'uvdesk.automation.workflow.execute');
  760.                     $json['success']  = $container->get('translator')->trans('Success');
  761.                     $json['description'] =  $container->get('translator')->trans('Ticket priority updated to %priority%', [
  762.                         '%priority%' => $ticketPriority->getDescription()
  763.                     ]);
  764.                     $statusCode Response::HTTP_OK;
  765.                     return new JsonResponse($json$statusCode);
  766.                 }
  767.                 break;
  768.             case 'group':
  769.                 $supportGroup $entityManager->getRepository(CoreFrameworkBundleEntity\SupportGroup::class)->findOneById($requestContent['value']);
  770.                 if (empty($supportGroup)) {
  771.                     if ($requestContent['value'] == "") {
  772.                         if ($ticket->getSupportGroup() != null) {
  773.                             $ticket->setSupportGroup(null);
  774.                             $entityManager->persist($ticket);
  775.                             $entityManager->flush();
  776.                         }
  777.                         $json['success']  = $container->get('translator')->trans('Success');
  778.                         $json['description'] =   $container->get('translator')->trans('Ticket support group updated successfully');
  779.                         $statusCode Response::HTTP_OK;
  780.                     } else {
  781.                         $json['error']  = $container->get('translator')->trans('Error');
  782.                         $json['description'] = $container->get('translator')->trans('Unable to retrieve support group details');
  783.                         $statusCode Response::HTTP_BAD_REQUEST;
  784.                     }
  785.                     return new JsonResponse($json$statusCode);
  786.                 }
  787.                 if (
  788.                     ! empty($ticket->getSupportGroup())
  789.                     && $supportGroup->getId() === $ticket->getSupportGroup()->getId()
  790.                 ) {
  791.                     $json['success']  = $container->get('translator')->trans('Success');
  792.                     $json['description'] = $container->get('translator')->trans('Ticket already assigned to support group');
  793.                     $statusCode Response::HTTP_OK;
  794.                     return new JsonResponse($json$statusCode);
  795.                 } else {
  796.                     $ticket->setSupportGroup($supportGroup);
  797.                     $entityManager->persist($ticket);
  798.                     $entityManager->flush();
  799.                     // Trigger Support group event
  800.                     $event = new CoreWorkflowEvents\Ticket\Group();
  801.                     $event
  802.                         ->setTicket($ticket);
  803.                     $container->get('event_dispatcher')->dispatch($event'uvdesk.automation.workflow.execute');
  804.                     $json['success']  = $container->get('translator')->trans('Success');
  805.                     $json['description'] = $container->get('translator')->trans('Ticket assigned to support group successfully');
  806.                     $json['description'] = $container->get('translator')->trans('Ticket assigned to support group %group%', [
  807.                         '%group%' => $supportGroup->getDescription()
  808.                     ]);
  809.                     $statusCode Response::HTTP_OK;
  810.                     return new JsonResponse($json$statusCode);
  811.                 }
  812.                 break;
  813.             case 'team':
  814.                 $supportTeam $entityManager->getRepository(CoreFrameworkBundleEntity\SupportTeam::class)->findOneById($requestContent['value']);
  815.                 if (empty($supportTeam)) {
  816.                     if ($requestContent['value'] == "") {
  817.                         if ($ticket->getSupportTeam() != null) {
  818.                             $ticket->setSupportTeam(null);
  819.                             $entityManager->persist($ticket);
  820.                             $entityManager->flush();
  821.                         }
  822.                         $json['success']  = $container->get('translator')->trans('Success');
  823.                         $json['description'] = $container->get('translator')->trans('Ticket support team updated successfully');
  824.                         $statusCode Response::HTTP_OK;
  825.                         return new JsonResponse($json$statusCode);
  826.                     } else {
  827.                         $json['error']  = $container->get('translator')->trans('Error');
  828.                         $json['description'] = $container->get('translator')->trans('Unable to retrieve support team details');
  829.                         $statusCode Response::HTTP_BAD_REQUEST;
  830.                         return new JsonResponse($json$statusCode);
  831.                     }
  832.                 }
  833.                 if (
  834.                     ! empty($ticket->getSupportTeam())
  835.                     && $supportTeam->getId() === $ticket->getSupportTeam()->getId()
  836.                 ) {
  837.                     $json['success']  = $container->get('translator')->trans('Success');
  838.                     $json['description'] = $container->get('translator')->trans('Ticket already assigned to support team');
  839.                     $statusCode Response::HTTP_OK;
  840.                     return new JsonResponse($json$statusCode);
  841.                 } else {
  842.                     $ticket->setSupportTeam($supportTeam);
  843.                     $entityManager->persist($ticket);
  844.                     $entityManager->flush();
  845.                     // Trigger ticket delete event
  846.                     $event = new CoreWorkflowEvents\Ticket\Team();
  847.                     $event
  848.                         ->setTicket($ticket);
  849.                     $container->get('event_dispatcher')->dispatch($event'uvdesk.automation.workflow.execute');
  850.                     $json['success']  = $container->get('translator')->trans('Success');
  851.                     $json['description'] = $container->get('translator')->trans('Ticket assigned to support team successfully');
  852.                     $json['description'] = $container->get('translator')->trans('Ticket assigned to support team %team%', [
  853.                         '%team%' => $supportTeam->getDescription()
  854.                     ]);
  855.                     $statusCode Response::HTTP_OK;
  856.                     return new JsonResponse($json$statusCode);
  857.                 }
  858.                 break;
  859.             case 'type':
  860.                 $ticketType $entityManager->getRepository(CoreFrameworkBundleEntity\TicketType::class)->findOneById($requestContent['value']);
  861.                 if (empty($ticketType)) {
  862.                     // Selected ticket priority does not exist
  863.                     $json['error']  = $container->get('translator')->trans('Error');
  864.                     $json['description'] = $container->get('translator')->trans('Unable to retrieve ticket type details');
  865.                     $statusCode Response::HTTP_BAD_REQUEST;
  866.                     return new JsonResponse($json$statusCode);
  867.                 }
  868.                 if (
  869.                     ! empty($ticket->getType())
  870.                     && $ticketType->getId() === $ticket->getType()->getId()
  871.                 ) {
  872.                     $json['success']  = $container->get('translator')->trans('Success');
  873.                     $json['description'] = $container->get('translator')->trans('Ticket type already set to ' $ticketType->getDescription());
  874.                     $statusCode Response::HTTP_OK;
  875.                     return new JsonResponse($json$statusCode);
  876.                 } else {
  877.                     $ticket->setType($ticketType);
  878.                     $entityManager->persist($ticket);
  879.                     $entityManager->flush();
  880.                     // Trigger ticket delete event
  881.                     $event = new CoreWorkflowEvents\Ticket\Type();
  882.                     $event
  883.                         ->setTicket($ticket);
  884.                     $container->get('event_dispatcher')->dispatch($event'uvdesk.automation.workflow.execute');
  885.                     $json['success']  = $container->get('translator')->trans('Success');
  886.                     $json['description'] = $container->get('translator')->trans('Ticket type updated to ' $ticketType->getDescription());
  887.                     $statusCode Response::HTTP_OK;
  888.                     return new JsonResponse($json$statusCode);
  889.                 }
  890.                 break;
  891.             case 'label':
  892.                 $label $entityManager->getRepository(CoreFrameworkBundleEntity\SupportLabel::class)->find($requestContent['value']);
  893.                 if ($label) {
  894.                     $ticket->removeSupportLabel($label);
  895.                     $entityManager->persist($ticket);
  896.                     $entityManager->flush();
  897.                     $json['success']  = $container->get('translator')->trans('Success');
  898.                     $json['description'] = $container->get('translator')->trans('Success ! Ticket to label removed successfully');
  899.                     $statusCode Response::HTTP_OK;
  900.                     return new JsonResponse($json$statusCode);
  901.                 } else {
  902.                     $json['error']  = $container->get('translator')->trans('Error');
  903.                     $json['description'] = $container->get('translator')->trans('No support level exist for this ticket with this id');
  904.                     $statusCode Response::HTTP_BAD_REQUEST;
  905.                     return new JsonResponse($json$statusCode);
  906.                 }
  907.                 break;
  908.             default:
  909.                 break;
  910.         }
  911.         return new JsonResponse($json$statusCode);
  912.     }
  913.     /**
  914.      * objectSerializer This function convert Entity object into json contenxt
  915.      * @param Object $object Customer Entity object
  916.      * @return JSON  JSON context
  917.      */
  918.     public function objectSerializer($object)
  919.     {
  920.         $object->formatedCreatedAt = new \Datetime;
  921.         $encoders = array(new XmlEncoder(), new JsonEncoder());
  922.         $normalizer = new ObjectNormalizer();
  923.         $normalizers = array($normalizer);
  924.         $serializer = new Serializer($normalizers$encoders);
  925.         $jsonContent $serializer->serialize($object'json', [
  926.             'circular_reference_handler' => function ($object) {
  927.                 return $object->getId();
  928.             }
  929.         ]);
  930.         return $jsonContent;
  931.     }
  932. }