You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.6KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Solver\Price;
  3. use Lc\CaracoleBundle\Doctrine\Extension\ProductPropertyInterface;
  4. use Lc\CaracoleBundle\Model\Order\OrderProductInterface;
  5. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  6. class PriceSolver
  7. {
  8. protected ProductPriceSolver $productPriceSolver;
  9. protected OrderProductPriceSolver $orderProductPriceSolver;
  10. protected OrderShopPriceSolver $orderShopPriceSolver;
  11. public function __construct(
  12. ProductPriceSolver $productPriceSolver,
  13. OrderProductPriceSolver $orderProductPriceSolver,
  14. OrderShopPriceSolver $orderShopPriceSolver
  15. ) {
  16. $this->productPriceSolver = $productPriceSolver;
  17. $this->orderProductPriceSolver = $orderProductPriceSolver;
  18. $this->orderShopPriceSolver = $orderShopPriceSolver;
  19. }
  20. public function __call($name, $arguments)
  21. {
  22. if (strpos($name, 'apply') === false) {
  23. $entity = $arguments[0];
  24. $service = '';
  25. if ($entity instanceof ProductPropertyInterface) {
  26. $service = 'productPriceSolver';
  27. }
  28. if ($entity instanceof OrderProductInterface) {
  29. $service = 'orderProductPriceSolver';
  30. }
  31. if ($entity instanceof OrderShopInterface || is_iterable($entity) || is_array($entity)) {
  32. $service = 'orderShopPriceSolver';
  33. }
  34. if (strlen($service) && $entity && method_exists($this->$service, $name)) {
  35. if (isset($arguments[1]) && isset($arguments[2]) && isset($arguments[3])) {
  36. return $this->$service->$name($entity, $arguments[1], $arguments[2], $arguments[3]);
  37. } elseif (isset($arguments[1]) && isset($arguments[2])) {
  38. return $this->$service->$name($entity, $arguments[1], $arguments[2]);
  39. } elseif (isset($arguments[1])) {
  40. return $this->$service->$name($entity, $arguments[1]);
  41. } else {
  42. return $this->$service->$name($entity);
  43. }
  44. } else {
  45. if (!strlen($service)) {
  46. throw new \ErrorException("PriceSolver : le type d'entité n'est pas géré");
  47. } else {
  48. if (!method_exists($this->$service, $name)) {
  49. throw new \ErrorException(
  50. "PriceSolver : la méthode " . $name . " du service " . $service . " n'existe pas."
  51. );
  52. }
  53. }
  54. }
  55. return false;
  56. }
  57. }
  58. }