src/Controller/Customer/PriceController.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Customer;
  3. use App\Exception\InvalidPriceException;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Contracts\Translation\TranslatorInterface;
  9. use App\Entity\CarrierPricePostman;
  10. use App\Entity\Country;
  11. use App\Entity\Configuration;
  12. class PriceController extends AbstractController
  13. {
  14.     /**
  15.      * Controller slouzi pro zjisteni ceny za poslani baliku podle parametru baliku
  16.      * @param EntityManagerInterface $em
  17.      * @param TranslatorInterface $translator
  18.      * @param string $iso dvoumistny iso kod zeme
  19.      * @param float|null $length delka baliku v centimetrech
  20.      * @param float|null $width sirka baliku v centimetrech
  21.      * @param float|null $height vyska baliku v centimetrech
  22.      * @param float $weight vaha baliku v kilogramech
  23.      * @param bool $json
  24.      * @return JsonResponse|Response
  25.      * @throws \Doctrine\ORM\NonUniqueResultException
  26.      */
  27.     public function getDeliveryPrice(
  28.         EntityManagerInterface $em,
  29.         TranslatorInterface $translator,
  30.         string $iso,
  31.         ?float $length 0,
  32.         ?float $width 0,
  33.         ?float $height 0,
  34.         float $weight 0,
  35.         bool $json false
  36.     ): JsonResponse|Response {
  37.         $ret = ['success' => false'price' => 0'message' => ''];
  38.         $countryRepo $em->getRepository(Country::class);
  39.         /** @var Country|null Country */
  40.         $country $countryRepo->findOneBy(['iso' => $iso]);
  41.         if (!is_null($country)) {
  42.             try {
  43.                 $carrierPricePostmanRepo $em->getRepository(CarrierPricePostman::class);
  44.                 // metoda vyhodi vyjimku, pokud jsou parametry nevalidni
  45.                 $carrierPricePostmanRepo->validateDimensions($translator$length$width$height$weight);
  46.                 $carrierPrice $carrierPricePostmanRepo->findLowestPriceByCountryAndWeight($translator$country$weight);
  47.                 $ret['price'] = intval($carrierPrice->getPriceSell());
  48.                 $ret['success'] = true;
  49.             } catch (InvalidPriceException $e) {
  50.                 $configRepo $em->getRepository(Configuration::class);
  51.                 $ret['price'] = null;
  52.                 $ret['success'] = false;
  53.                 $ret['message'] = $e->getMessage();
  54.             }
  55.         }
  56.         if ($json) {
  57.             $jsonResponse = new JsonResponse($ret);
  58.             $jsonResponse->setEncodingOptions(\JSON_UNESCAPED_SLASHES \JSON_UNESCAPED_UNICODE \JSON_UNESCAPED_LINE_TERMINATORS);
  59.             return $jsonResponse;
  60.         } else {
  61.             return new Response((string)$ret['price']);
  62.         }
  63.     }
  64. }