Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RotatingResolver.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\RotatingProduct\RotatingProduct;
  8. use domain\Product\RotatingProduct\RotatingProductRepository;
  9. use yii\base\ErrorException;
  10. class RotatingResolver extends AbstractResolver
  11. {
  12. protected DistributionRepository $distributionRepository;
  13. protected DistributionRotatingRepository $distributionRotatingRepository;
  14. public function loadDependencies(): void
  15. {
  16. $this->distributionRepository = $this->loadService(DistributionRepository::class);
  17. $this->distributionRotatingRepository = $this->loadService(DistributionRotatingRepository::class);
  18. }
  19. public function getPreviousDistribution(Rotating $rotating, \DateTime $date): ?Distribution
  20. {
  21. $date = $date->modify('previous '.$rotating->getDayAsString());
  22. return $this->distributionRepository->findOneDistributionByDateTime($date);
  23. }
  24. public function getNextDistribution(Rotating $rotating, \DateTime $date): ?Distribution
  25. {
  26. $date = $date->modify('next '.$rotating->getDayAsString());
  27. return $this->distributionRepository->findOneDistributionByDateTime($date);
  28. }
  29. /**
  30. * @throws ErrorException
  31. */
  32. public function deductNextRotatingProductOfDistribution(Rotating $rotating, Distribution $distribution): RotatingProduct
  33. {
  34. $date = clone $distribution->getDate();
  35. $previousDistribution = $this->getPreviousDistribution($rotating, $date);
  36. $previousDistributionRotating = $this->distributionRotatingRepository->findOneDistributionRotating($previousDistribution, $rotating);
  37. if($previousDistributionRotating) {
  38. $rotatingProductPreviousDistribution = $previousDistributionRotating->getRotatingProduct();
  39. return $this->getNextRotatingProduct($rotating, $rotatingProductPreviousDistribution);
  40. }
  41. return $this->getFirstRotatingProduct($rotating);
  42. }
  43. /**
  44. * @throws ErrorException
  45. */
  46. public function getNextRotatingProduct(Rotating $rotating, RotatingProduct $currentRotatingProduct = null): RotatingProduct
  47. {
  48. if(is_null($currentRotatingProduct)) {
  49. return $this->getFirstRotatingProduct($rotating);
  50. }
  51. $rotatingProductReturn = null;
  52. $nextIsTheOne = false;
  53. foreach($rotating->getRotatingProducts() as $rotatingProduct) {
  54. if($nextIsTheOne) {
  55. return $rotatingProduct;
  56. }
  57. if($rotatingProduct->getId() == $currentRotatingProduct->getId()) {
  58. $nextIsTheOne = true;
  59. }
  60. }
  61. if($nextIsTheOne) {
  62. $rotatingProductReturn = $this->getFirstRotatingProduct($rotating);
  63. }
  64. if($rotatingProductReturn) {
  65. return $rotatingProductReturn;
  66. }
  67. else {
  68. throw new ErrorException("Le produit tournant suivant n'a pas été trouvé");
  69. }
  70. }
  71. /**
  72. * @throws ErrorException
  73. */
  74. public function getFirstRotatingProduct(Rotating $rotating): RotatingProduct
  75. {
  76. $rotatingProductReturn = null;
  77. foreach($rotating->getRotatingProducts() as $rotatingProduct) {
  78. $rotatingProductReturn = $rotatingProduct;
  79. }
  80. if($rotatingProductReturn) {
  81. return $rotatingProductReturn;
  82. }
  83. else {
  84. throw new ErrorException("Le produit tournant doit contenir au moins un produit");
  85. }
  86. }
  87. }