<?php

namespace App\MessageHandler;

use App\Message\DomainIpCheckedMessage;
use App\Service\DomainService;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class DomainIpMessageHandler implements MessageHandlerInterface
{
    /**
     * @var DomainService
     */
    private $domainService;

    public function __construct(DomainService $domainService)
    {
        $this->domainService = $domainService;
    }

    public function __invoke(DomainIpCheckedMessage $message)
    {
        try {
            $domain = $this->domainService->getDomain($message->getDomainId());
        } catch (\Exception $e) {
            return null;
        }

        $domain->setIp($message->getIp());
        $domain->setCheckedAt($message->getWhen());
        $this->domainService->saveDomain($domain);
    }
}
