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.

127 lines
4.7KB

  1. <?php
  2. namespace domain\Product\Rotating;
  3. use domain\_\AbstractResolver;
  4. use domain\Distribution\Distribution\Distribution;
  5. use domain\Distribution\Distribution\DistributionRepository;
  6. use domain\Distribution\DistributionRotating\DistributionRotatingRepository;
  7. use domain\Product\Product\Product;
  8. use domain\Product\RotatingProduct\RotatingProduct;
  9. use domain\Product\RotatingProduct\RotatingProductRepository;
  10. use yii\base\ErrorException;
  11. class RotatingResolver extends AbstractResolver
  12. {
  13. protected DistributionRepository $distributionRepository;
  14. protected DistributionRotatingRepository $distributionRotatingRepository;
  15. protected RotatingProductRepository $rotatingProductRepository;
  16. public function loadDependencies(): void
  17. {
  18. $this->distributionRepository = $this->loadService(DistributionRepository::class);
  19. $this->distributionRotatingRepository = $this->loadService(DistributionRotatingRepository::class);
  20. $this->rotatingProductRepository = $this->loadService(RotatingProductRepository::class);
  21. }
  22. public function getPreviousDistribution(Rotating $rotating, \DateTime $date): ?Distribution
  23. {
  24. $date = $date->modify('previous '.$rotating->getDayAsString('en'));
  25. return $this->distributionRepository->findOneDistributionByDateTime($date);
  26. }
  27. public function getNextDistribution(Rotating $rotating, \DateTime $date): ?Distribution
  28. {
  29. $date = $date->modify('next '.$rotating->getDayAsString('en'));
  30. return $this->distributionRepository->findOneDistributionByDateTime($date);
  31. }
  32. /**
  33. * @throws ErrorException
  34. */
  35. public function deductRotatingProductOfDistribution(Rotating $rotating, Distribution $distribution): RotatingProduct
  36. {
  37. $date = clone $distribution->getDate();
  38. $previousDistribution = $this->getPreviousDistribution($rotating, $date);
  39. if($previousDistribution) {
  40. $previousDistributionRotating = $this->distributionRotatingRepository->findOneDistributionRotating($previousDistribution, $rotating);
  41. if($previousDistributionRotating) {
  42. $rotatingProductPreviousDistribution = $previousDistributionRotating->getRotatingProduct();
  43. $nextRotatingProduct = $this->getNextRotatingProduct($rotating, $rotatingProductPreviousDistribution);
  44. return $nextRotatingProduct;
  45. }
  46. }
  47. return $this->getFirstRotatingProduct($rotating);
  48. }
  49. /**
  50. * @throws ErrorException
  51. */
  52. public function getNextRotatingProduct(Rotating $rotating, RotatingProduct $currentRotatingProduct = null): RotatingProduct
  53. {
  54. if(is_null($currentRotatingProduct)) {
  55. return $this->getFirstRotatingProduct($rotating);
  56. }
  57. $rotatingProductReturn = null;
  58. $nextIsTheOne = false;
  59. foreach($rotating->getRotatingProducts() as $rotatingProduct) {
  60. if($nextIsTheOne) {
  61. return $rotatingProduct;
  62. }
  63. if($rotatingProduct->getId() == $currentRotatingProduct->getId()) {
  64. $nextIsTheOne = true;
  65. }
  66. }
  67. if($nextIsTheOne) {
  68. $rotatingProductReturn = $this->getFirstRotatingProduct($rotating);
  69. }
  70. if($rotatingProductReturn) {
  71. return $rotatingProductReturn;
  72. }
  73. else {
  74. throw new ErrorException("Le produit tournant suivant n'a pas été trouvé");
  75. }
  76. }
  77. /**
  78. * @throws ErrorException
  79. */
  80. public function getFirstRotatingProduct(Rotating $rotating): RotatingProduct
  81. {
  82. $rotatingProductReturn = null;
  83. foreach($rotating->getRotatingProducts() as $rotatingProduct) {
  84. $rotatingProductReturn = $rotatingProduct;
  85. break;
  86. }
  87. if($rotatingProductReturn) {
  88. return $rotatingProductReturn;
  89. }
  90. else {
  91. throw new ErrorException("Le produit tournant doit contenir au moins un produit");
  92. }
  93. }
  94. public function getRotatingOfProductInDistribution(Product $product, Distribution $distribution): ?Rotating
  95. {
  96. $distributionRotatingsArray = $this->distributionRotatingRepository->findDistributionRotatingsByDistribution($distribution);
  97. foreach($distributionRotatingsArray as $distributionRotating) {
  98. if($distributionRotating->getRotatingProduct()->getProduct()->equal($product)) {
  99. return $distributionRotating->getRotating();
  100. }
  101. }
  102. return null;
  103. }
  104. public function isRotatingOfProductInDistribution(Product $product, Distribution $distribution): bool
  105. {
  106. return (bool) $this->getRotatingOfProductInDistribution($product, $distribution);
  107. }
  108. }