<?php 
 
declare(strict_types=1); 
 
namespace Slivki\Services\City; 
 
use Slivki\Entity\City; 
use Slivki\Repository\City\CityRepositoryInterface; 
use Slivki\Util\SoftCache; 
 
final class CityCacheService 
{ 
    private const CACHE_NAME = 'city'; 
 
    private CityRepositoryInterface $cityRepository; 
 
    public function __construct(CityRepositoryInterface $cityRepository) 
    { 
        $this->cityRepository = $cityRepository; 
    } 
 
    public function getCityIdByDomain(string $domain): ?int 
    { 
        $softCache = new SoftCache(self::CACHE_NAME); 
 
        $cityId = $softCache->get($domain); 
 
        if (null === $cityId || false === $cityId) { 
            $city = $this->cityRepository->findActiveByDomain($domain); 
 
            if ($city !== null) { 
                $cityId = $city->getID(); 
                $softCache->set($domain, $cityId, 2 * 60 * 60); 
            } else { 
                $cityId = City::DEFAULT_CITY_ID; 
            } 
        } 
 
        return $cityId; 
    } 
}