<?php
namespace Whater\Application\UseCase\Product;
use Whater\Application\UseCase\AbstractCommandHandler;
use Whater\Application\UseCase\Product\CommandRequest\GetRecommendedProductsForDistributionNetworkCommand;
use Whater\Domain\Product\Repository\ProductRepositoryInterface;
/**
* Class GetRecommendedProductsForDistributionNetworkHandler
* @package Whater\Application\UseCase\Whater
*/
class GetRecommendedProductsForDistributionNetworkHandler extends AbstractCommandHandler
{
/**
* @var ProductRepositoryInterface
*/
private $repositoryProduct;
/**
* @param ProductRepositoryInterface $repositoryProduct
*/
public function __construct(ProductRepositoryInterface $repositoryProduct)
{
$this->repositoryProduct = $repositoryProduct;
}
/**
* @param GetRecommendedProductsForDistributionNetworkCommand $getRecommendedProductsForDistributionNetworkCommand
*/
public function handle(GetRecommendedProductsForDistributionNetworkCommand $getRecommendedProductsForDistributionNetworkCommand): ?array
{
$recommendedProducts = [];
$distributionNetwork = $getRecommendedProductsForDistributionNetworkCommand->distributionNetwork();
$countryId = $distributionNetwork->country()->id();
$countryAreaId = null;
try {
$countryArea = $distributionNetwork->towns()->first()->countryArea();
$countryAreaId = $countryArea->id();
} catch (\Exception $e) {
}
$recommendedProducts = $this->repositoryProduct->findRecommendedProducts(
$countryId,
$countryAreaId,
null,
$distributionNetwork->whaterStatus()
);
return $recommendedProducts;
}
}