<?php
/**
 * @author Daniel Knudsen <d.knudsen@spawntree.de>
 * @date 25.01.24
 */


namespace App\Controller;


use App\Entity\DocumentObject;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

#[AsController]
final class CreateDocumentObjectAction extends AbstractController
{
    public function __invoke(Request $request): DocumentObject
    {
        $uploadedFile = $request->files->get('file');
        if (!$uploadedFile) {
            throw new BadRequestHttpException('"file" is required');
        }

        $user = $this->getUser();
        assert($user instanceof User);
        $mediaObject = new DocumentObject($user);
        $mediaObject->file = $uploadedFile;

        return $mediaObject;
    }
}