<?php

namespace App\MessageHandler;

use App\Entity\File;
use App\Message\ReportGeneratedMessage;
use App\Service\Campaign\ReportService;
use App\Service\FileService;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

final class ReportGeneratedMessageHandler implements MessageHandlerInterface
{
    /**
     * @var FileService
     */
    private $fileService;
    /**
     * @var ReportService
     */
    private $reportService;

    public function __construct(FileService $fileService, ReportService $reportService)
    {
        $this->fileService = $fileService;
        $this->reportService = $reportService;
    }

    public function __invoke(ReportGeneratedMessage $message)
    {
        $file = new File();
        $file->setPath('pdf/');
        $file->setFilename($message->getPath());
        $file->setTitle($message->getReport()->getTitle());
        $file->setMimetype('application/pdf');
        $file->setSize(1);

        $file = $this->fileService->save($file);

        $report = $message->getReport()->setFile($file);
        $this->reportService->save($report);
    }
}
