<?php
namespace App\Controller\Customer;
use App\Exception\InvalidPriceException;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Entity\CarrierPricePostman;
use App\Entity\Country;
use App\Entity\Configuration;
class PriceController extends AbstractController
{
/**
* Controller slouzi pro zjisteni ceny za poslani baliku podle parametru baliku
* @param EntityManagerInterface $em
* @param TranslatorInterface $translator
* @param string $iso dvoumistny iso kod zeme
* @param float|null $length delka baliku v centimetrech
* @param float|null $width sirka baliku v centimetrech
* @param float|null $height vyska baliku v centimetrech
* @param float $weight vaha baliku v kilogramech
* @param bool $json
* @return JsonResponse|Response
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function getDeliveryPrice(
EntityManagerInterface $em,
TranslatorInterface $translator,
string $iso,
?float $length = 0,
?float $width = 0,
?float $height = 0,
float $weight = 0,
bool $json = false
): JsonResponse|Response {
$ret = ['success' => false, 'price' => 0, 'message' => ''];
$countryRepo = $em->getRepository(Country::class);
/** @var Country|null Country */
$country = $countryRepo->findOneBy(['iso' => $iso]);
if (!is_null($country)) {
try {
$carrierPricePostmanRepo = $em->getRepository(CarrierPricePostman::class);
// metoda vyhodi vyjimku, pokud jsou parametry nevalidni
$carrierPricePostmanRepo->validateDimensions($translator, $length, $width, $height, $weight);
$carrierPrice = $carrierPricePostmanRepo->findLowestPriceByCountryAndWeight($translator, $country, $weight);
$ret['price'] = intval($carrierPrice->getPriceSell());
$ret['success'] = true;
} catch (InvalidPriceException $e) {
$configRepo = $em->getRepository(Configuration::class);
$ret['price'] = null;
$ret['success'] = false;
$ret['message'] = $e->getMessage();
}
}
if ($json) {
$jsonResponse = new JsonResponse($ret);
$jsonResponse->setEncodingOptions(\JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_LINE_TERMINATORS);
return $jsonResponse;
} else {
return new Response((string)$ret['price']);
}
}
}