<?php

declare(strict_types=1);

namespace App\MessageHandler;

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

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

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

    public function __invoke(StatUpdated $message)
    {
        try {
            $now = new \DateTime();
        } catch (\Exception $e) {
            return;
        }
        $domain = $message->getDomain()->setCheckedAt($now);
        $this->domainService->saveDomain($domain);
    }
}
