src/Service/AppointmentService.php line 371

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Throwable;
  4. use JsonException;
  5. use App\Adapter\AppointmentAdapter;
  6. use App\Mapper\AppointmentMapper;
  7. use App\Model\Appointment;
  8. use App\Model\Garage;
  9. use App\Model\Vehicle;
  10. use App\Utils\Date;
  11. use DateTime;
  12. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  13. use Symfony\Component\Serializer\SerializerInterface;
  14. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  15. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  16. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  17. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  18. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  19. class AppointmentService
  20. {
  21.     public const PREFIX_NEW 'new';
  22.     public const APPOINTMENT_SLUG 'appointment';
  23.     protected AppointmentAdapter $adapter;
  24.     protected AppointmentMapper $mapper;
  25.     protected ClientService $clientService;
  26.     protected SerializerInterface $serializer;
  27.     public function __construct(
  28.         AppointmentAdapter $adapter,
  29.         AppointmentMapper $mapper,
  30.         ClientService $clientService,
  31.         SerializerInterface $serializer)
  32.     {
  33.         $this->adapter $adapter;
  34.         $this->mapper $mapper;
  35.         $this->clientService $clientService;
  36.         $this->serializer $serializer;
  37.     }
  38.     /**
  39.      * @throws Throwable
  40.      * @throws TransportExceptionInterface
  41.      * @throws ServerExceptionInterface
  42.      * @throws RedirectionExceptionInterface
  43.      * @throws DecodingExceptionInterface
  44.      * @throws ClientExceptionInterface
  45.      */
  46.     public function getGarages(): ?array
  47.     {
  48.         return $this->adapter->getGarages();
  49.     }
  50.     public function getService(array $data): ?array
  51.     {
  52.         if (isset($data['brand']) && isset($data['model']) && isset($data['submodel'])) {
  53.             $services array_unique($this->adapter->getServices($data), SORT_REGULAR);
  54.             // convert cents to euro
  55.             return array_map(function ($value) {
  56.                 $value['price'] = $value['price'] / 100;
  57.                 $value['description'] = html_entity_decode($value['description'], ENT_QUOTES|ENT_HTML5);
  58.                 return $value;
  59.             }, $services);
  60.         }
  61.         return [];
  62.     }
  63.     public function getServiceIds(Appointment $appointment): array
  64.     {
  65.         $serviceIDs = [];
  66.         foreach ($appointment->getServices() as $service) {
  67.             $serviceIDs[] = $service['id'];
  68.         }
  69.         return $serviceIDs;
  70.     }
  71.     /**
  72.      * @throws RedirectionExceptionInterface
  73.      * @throws DecodingExceptionInterface
  74.      * @throws ClientExceptionInterface
  75.      * @throws JsonException
  76.      * @throws TransportExceptionInterface
  77.      * @throws Throwable
  78.      * @throws ServerExceptionInterface
  79.      */
  80.     public function sendAppointment(Appointment $appointmentbool $isUserConnected false): bool
  81.     {
  82.         $dto $this->mapper->getAppointmentDto($appointment$isUserConnected);
  83.         return $this->adapter->sendAppointment($dto$isUserConnected);
  84.     }
  85.     /**
  86.      * @return void
  87.      */
  88.     public function addToVehicles(array &$vehiclesVehicle $vehicle)
  89.     {
  90.         // for update, remove element and add a new one later
  91.         if ($id $vehicle->getId()) {
  92.             $this->deleteVehicle($id$vehicles);
  93.         }
  94.         // add the encoded vehicle on the top, so it will be focused on front
  95.         array_unshift($vehicles, [
  96.             'selected' => true,
  97.             'id' => $vehicle->getId() ?? self::PREFIX_NEW.'_'.uniqid(),
  98.             'brand' => $vehicle->getBrand(),
  99.             'model' => $vehicle->getModel(),
  100.             'submodel' => $vehicle->getSubModel(),
  101.             'canUpdate' => $vehicle->canUpdate(),
  102.             'attributes' => [
  103.                 [
  104.                     'type' => 'select',
  105.                     'label' => 'account.cars.brand',
  106.                     'value' => $vehicle->getBrand(),
  107.                     'field' => 'brand',
  108.                 ],
  109.                 [
  110.                     'type' => 'select',
  111.                     'label' => 'account.cars.model',
  112.                     'value' => $vehicle->getModel(),
  113.                     'field' => 'model',
  114.                 ],
  115.                 [
  116.                     'type' => 'select',
  117.                     'label' => 'account.cars.submodel',
  118.                     'value' => $vehicle->getSubModel(),
  119.                     'field' => 'submodel',
  120.                 ],
  121.                 [
  122.                     'type' => 'text',
  123.                     'label' => 'account.cars.plate',
  124.                     'value' => $vehicle->getPlate(),
  125.                     'field' => 'plate',
  126.                 ],
  127.                 [
  128.                     'type' => 'text',
  129.                     'label' => 'account.cars.vin',
  130.                     'value' => $vehicle->getVin(),
  131.                     'field' => 'vin',
  132.                 ],
  133.                 [
  134.                     'type' => 'link',
  135.                     'label' => 'account.cars.doclinkreg',
  136.                     'files' => $vehicle->getRegistrationFiles(),
  137.                     'field' => 'registrationFiles',
  138.                 ],
  139.                 [
  140.                     'type' => 'link',
  141.                     'label' => 'account.cars.doclinkconv',
  142.                     'files' => $vehicle->getConversionFiles(),
  143.                     'field' => 'conversionFiles',
  144.                 ],
  145.             ],
  146.         ]);
  147.     }
  148.     public function deleteVehicle(string $id, array &$vehicles): array
  149.     {
  150.         $found array_search($idarray_column($vehicles'id'), true);
  151.         if (false !== $found && isset($vehicles[$found])) {
  152.             $temp = [];
  153.             foreach ($vehicles as $car) {
  154.                 if ($car['id'] !== $id) {
  155.                     $temp[] = $car;
  156.                 }
  157.             }
  158.             $vehicles $temp;
  159.         }
  160.         return $vehicles;
  161.     }
  162.     public function deleteAppointmentVehicle(string $idbool $isUserConnectedSessionInterface $session): array
  163.     {
  164.         if ($isUserConnected) {
  165.             $this->clientService->deleteVehicle($id);
  166.             $userVehicles $this->clientService->getClientVehicles();
  167.             $vehicles = [];
  168.             foreach ($userVehicles as $vehicle) {
  169.                 $vehicle $this->serializer->deserialize(json_encode($vehicleJSON_THROW_ON_ERROR), Vehicle::class, 'json');
  170.                 $this->addToVehicles($vehicles$vehicle);
  171.             }
  172.         } else {
  173.             $vehicles $session->get('vehicles');
  174.             $this->deleteVehicle($id$vehicles);
  175.             $session->set('vehicles'$vehicles);
  176.         }
  177.         return $vehicles;
  178.     }
  179.     /**
  180.      * @return void
  181.      */
  182.     public function removeNextStep(Appointment $appointmentstring $currentStep)
  183.     {
  184.         switch ($currentStep) {
  185.             case 'garage':
  186.                 $appointment->setVehicle(null);
  187.                 $appointment->setServices(null);
  188.                 $appointment->setSlot(null);
  189.                 $appointment->setReceptionSlot(null);
  190.                 $appointment->setDeliverySlot(null);
  191.                 break;
  192.             case 'vehicle':
  193.                 $appointment->setServices(null);
  194.                 $appointment->setSlot(null);
  195.                 $appointment->setReceptionSlot(null);
  196.                 $appointment->setDeliverySlot(null);
  197.                 break;
  198.             case 'service':
  199.                 $appointment->setSlot(null);
  200.                 $appointment->setReceptionSlot(null);
  201.                 $appointment->setDeliverySlot(null);
  202.                 break;
  203.         }
  204.     }
  205.     /**
  206.      * @return void
  207.      */
  208.     public function removeAllSession(SessionInterface $session)
  209.     {
  210.         $session->remove('appointment');
  211.         $session->remove('vehicles');
  212.         $session->remove('gon');
  213.         $session->remove('maxStep');
  214.     }
  215.     public function needReinitVehicleStep(SessionInterface $session, ?string $id): bool
  216.     {
  217.         /** @var Appointment $appointment */
  218.         $appointment $session->get('appointment');
  219.         if ($appointment && $appointment->getVehicle() && $appointment->getVehicle()->getId() == $id) {
  220.             $appointment->setVehicle(null);
  221.             $this->removeNextStep($appointment'vehicle');
  222.             $session->set('reinit'true);
  223.             return true;
  224.         }
  225.         return false;
  226.     }
  227.     public function changeGarage($garageIDSessionInterface $session, array &$responseDatasSlotService $slotService): array
  228.     {
  229.         $garages $session->get('garages');
  230.         /** @var Appointment $appointment */
  231.         $appointment $session->get('appointment');
  232.         $hours = [];
  233.         $garage $this->getSelectedGarage($garages$garageID);
  234.         $day $responseDatas['day'];
  235.         $datestart = new DateTime("first day of $day");
  236.         $dateend = new DateTime("last day of $day");
  237.         $data = [
  238.             'serviceIDs' => implode(','$slotService->getServiceIds($appointment)),
  239.             'garageID' => $garage->getId(),
  240.             'from' => $datestart->getTimestamp(),
  241.             'to' => $dateend->getTimestamp()
  242.         ];
  243.         $slots $slotService->getAvailability($data$appointment);
  244.         $garage->setAvailabilities($slots);
  245.         $appointment->setGarage($garage);
  246.         if (isset($slots[$responseDatas['day']])) {
  247.             $hours $slots[$responseDatas['day']];
  248.         }
  249.         $dropTime = [
  250.             'day' => $responseDatas['day'],
  251.             'hour' => $responseDatas['time'],
  252.             'from' => $datestart->getTimestamp(),
  253.             'to' => $dateend->getTimestamp()
  254.         ];
  255.         $receptionSlots $slotService->getDropPickUpSlot($appointment$dropTime);
  256.         $deliverySlots $slotService->getDropPickUpSlot($appointment$dropTimefalse);
  257.         $responseDatas array_merge($responseDatas, [
  258.             'receptionTime' => $receptionSlots,
  259.             'deliveryTime' => $deliverySlots,
  260.             'hours' => $hours,
  261.         ]);
  262.         return $responseDatas;
  263.     }
  264.     /**
  265.      * Get datas from Service such as duration and list services
  266.      *
  267.      * @param Appointment $appointment
  268.      * @param array $responseDatas
  269.      * @return void
  270.      */
  271.     public function getDatasFromServices(Appointment $appointment, array &$responseDatas = [])
  272.     {
  273.         $services $appointment->getServices();
  274.         $listServices = [];
  275.         foreach ($services as $service) {
  276.             $listServices[] = $service['name'];
  277.         }
  278.         $responseDatas['duration'] = Date::getElementsTime($this->getTotalServiceDuration($services));
  279.         $responseDatas['list_services'] =  implode(', '$listServices);
  280.     }
  281.     /**
  282.      * @param array $services
  283.      * @return int
  284.      */
  285.     public function getTotalServiceDuration(array $services): int
  286.     {
  287.         $totalDuration 0;
  288.         foreach ($services as $service) {
  289.             $totalDuration += $service['time'];
  290.         }
  291.         return  $totalDuration;
  292.     }
  293.     /**
  294.      * @param $garages
  295.      * @param $garageID
  296.      * @return Garage
  297.      * @throws JsonException
  298.      */
  299.     public function getSelectedGarage($garages$garageID): ?Garage
  300.     {
  301.         $garage null;
  302.         foreach ($garages as $center) {
  303.             if ($garageID == $center['id']) {
  304.                 /** @var Garage $garage */
  305.                 $garage $this->serializer->deserialize(json_encode($centerJSON_THROW_ON_ERROR), Garage::class, 'json');
  306.                 break;
  307.             }
  308.         }
  309.         return $garage;
  310.     }
  311.     /**
  312.      * @param Appointment $appointment
  313.      * @return array
  314.      */
  315.     public function getServiceHeaderInfo(Appointment $appointment): array
  316.     {
  317.         // services calcul
  318.         $serviceHeaderInfo = [];
  319.         $pipe '';
  320.         $serviceHeaderInfo['total'] = 0;
  321.         $serviceHeaderInfo['name'] = '';
  322.         foreach ($appointment->getServices() as $key => $service) {
  323.             if ($key 0) {
  324.                 $pipe ' | ';
  325.             }
  326.             $serviceHeaderInfo['name'] .= $pipe $service['name'];
  327.             $serviceHeaderInfo['total'] += $service['price'];
  328.         }
  329.         $appointment->setServiceHeaderInfo($serviceHeaderInfo);
  330.         return $serviceHeaderInfo;
  331.     }
  332. }