src/Application/UseCase/Product/GetRecommendedProductsForDistributionNetworkHandler.php line 32

Open in your IDE?
  1. <?php
  2. namespace Whater\Application\UseCase\Product;
  3. use Whater\Application\UseCase\AbstractCommandHandler;
  4. use Whater\Application\UseCase\Product\CommandRequest\GetRecommendedProductsForDistributionNetworkCommand;
  5. use Whater\Domain\Product\Repository\ProductRepositoryInterface;
  6. /**
  7. * Class GetRecommendedProductsForDistributionNetworkHandler
  8. * @package Whater\Application\UseCase\Whater
  9. */
  10. class GetRecommendedProductsForDistributionNetworkHandler extends AbstractCommandHandler
  11. {
  12. /**
  13. * @var ProductRepositoryInterface
  14. */
  15. private $repositoryProduct;
  16. /**
  17. * @param ProductRepositoryInterface $repositoryProduct
  18. */
  19. public function __construct(ProductRepositoryInterface $repositoryProduct)
  20. {
  21. $this->repositoryProduct = $repositoryProduct;
  22. }
  23. /**
  24. * @param GetRecommendedProductsForDistributionNetworkCommand $getRecommendedProductsForDistributionNetworkCommand
  25. */
  26. public function handle(GetRecommendedProductsForDistributionNetworkCommand $getRecommendedProductsForDistributionNetworkCommand): ?array
  27. {
  28. $recommendedProducts = [];
  29. $distributionNetwork = $getRecommendedProductsForDistributionNetworkCommand->distributionNetwork();
  30. $countryId = $distributionNetwork->country()->id();
  31. $countryAreaId = null;
  32. try {
  33. $countryArea = $distributionNetwork->towns()->first()->countryArea();
  34. $countryAreaId = $countryArea->id();
  35. } catch (\Exception $e) {
  36. }
  37. $recommendedProducts = $this->repositoryProduct->findRecommendedProducts(
  38. $countryId,
  39. $countryAreaId,
  40. null,
  41. $distributionNetwork->whaterStatus()
  42. );
  43. return $recommendedProducts;
  44. }
  45. }