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


namespace App\Controller;


use App\Entity\MediaObject;
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 CreateMediaObjectAction extends AbstractController
{
    public function __invoke(Request $request): MediaObject
    {
        $uploadedFile = $request->files->get('file');
        if (!$uploadedFile) {
            throw new BadRequestHttpException('"file" is required');
        }

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

        return $mediaObject;
    }
}