src/Helpers/Services/ServiceHelper.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Helpers\Services;
  3. use App\Entity\Animals\MixerCategory;
  4. use App\Entity\Animals\Size;
  5. use App\Entity\Animals\Type;
  6. use App\Entity\Services\PriceGroup;
  7. use App\Entity\Services\Service;
  8. use Symfony\Component\DependencyInjection\ContainerInterface as Container;
  9. use Symfony\Component\Yaml\Parser;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use \App\Helpers\System\ConfigHelper;
  12. class ServiceHelper
  13. {
  14. protected $container;
  15. protected $em;
  16. protected $config;
  17. public function __construct(Container $container, EntityManagerInterface $em, ConfigHelper $configHelper)
  18. {
  19. $this->container = $container;
  20. $this->em = $em;
  21. $this->configHelper = $configHelper;
  22. }
  23. public function getPrice(Service $service, Type $type, Size $size, ?MixerCategory $mixerCategory = null, ?PriceGroup $priceGroup = null, ?\DateTime $lookupDate = null, $method = null)
  24. {
  25. // Price lookup
  26. $lookup = array(
  27. 'size' => $size,
  28. 'animalType' => $type,
  29. 'service' => $service
  30. );
  31. // Got a mixer category
  32. if($mixerCategory)
  33. $lookup["mixerCategory"] = $mixerCategory;
  34. // Got a price group?
  35. if($priceGroup)
  36. $lookup['priceGroup'] = $priceGroup;
  37. // Got a specific lookup date?
  38. if(is_object($lookupDate))
  39. $lookup['enabledForDate'] = $lookupDate;
  40. // Find the price
  41. $price = $this->em->getRepository(\App\Entity\Services\Price::class)->findOneFiltered($lookup, [
  42. "pg.startDate" => "DESC"
  43. ]);
  44. // Price not found?
  45. if(!$price)
  46. return null;
  47. if($method)
  48. {
  49. $method = "get" . ucFirst($method) . "()";
  50. // Method does not exist?
  51. if(!method_exists($price, $method))
  52. throw new \Exception("Method: " . $method . ' does not exists on price.');
  53. return $price->$method;
  54. }
  55. // Return the whole price
  56. return $price;
  57. }
  58. }