68 lines
2.8KB

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