1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Silverback API Components Bundle Project |
||
5 | * |
||
6 | * (c) Daniel West <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Silverback\ApiComponentsBundle\Action\Uploadable; |
||
15 | |||
16 | use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException; |
||
17 | use Silverback\ApiComponentsBundle\Helper\Publishable\PublishableStatusChecker; |
||
18 | use Silverback\ApiComponentsBundle\Helper\Uploadable\UploadableFileManager; |
||
19 | use Silverback\ApiComponentsBundle\Serializer\Normalizer\PublishableNormalizer; |
||
20 | use Symfony\Component\HttpFoundation\Request; |
||
21 | use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; |
||
22 | use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException; |
||
23 | use Symfony\Component\PropertyAccess\PropertyAccess; |
||
24 | use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
||
25 | |||
26 | /** |
||
27 | * @author Daniel West <[email protected]> |
||
28 | */ |
||
29 | class UploadAction |
||
30 | { |
||
31 | public function __construct(private NormalizerInterface|PublishableNormalizer $publishableNormalizer) |
||
32 | { |
||
33 | } |
||
34 | |||
35 | public function __invoke(?object $data, Request $request, UploadableFileManager $uploadableFileManager, PublishableStatusChecker $publishableStatusChecker) |
||
36 | { |
||
37 | $contentType = $request->headers->get('CONTENT_TYPE'); |
||
38 | if (null === $contentType) { |
||
39 | throw new UnsupportedMediaTypeHttpException('The "Content-Type" header must exist.'); |
||
40 | } |
||
41 | |||
42 | $contentType = explode(';', $contentType)[0]; |
||
43 | $formats = ['multipart/form-data']; |
||
44 | if (!\in_array(strtolower($contentType), $formats, true)) { |
||
45 | throw new UnsupportedMediaTypeHttpException(sprintf('The content-type "%s" is not supported. Supported MIME type is "%s".', $contentType, implode('", "', $formats))); |
||
46 | } |
||
47 | |||
48 | $resourceClass = $request->attributes->get('_api_resource_class'); |
||
49 | $resource = $data ?? new $resourceClass(); |
||
50 | |||
51 | /** |
||
52 | * if it IS publishable |
||
53 | * if NOT asking to update published ?published=true |
||
54 | * if it IS currently published |
||
55 | * if the user DOES have permission. |
||
56 | */ |
||
57 | $publishableAnnotationReader = $publishableStatusChecker->getAttributeReader(); |
||
58 | if ($publishableAnnotationReader->isConfigured($resource)) { |
||
59 | $configuration = $publishableAnnotationReader->getConfiguration($resource); |
||
60 | $isGranted = $publishableStatusChecker->isGranted($resource); |
||
61 | if (!$data) { |
||
62 | if (!$isGranted) { |
||
63 | $accessor = PropertyAccess::createPropertyAccessor(); |
||
64 | $accessor->setValue($resource, $configuration->fieldName, date('Y-m-d H:i:s')); |
||
65 | } |
||
66 | } elseif ( |
||
67 | $isGranted |
||
68 | && !$publishableStatusChecker->isRequestForPublished($request) |
||
69 | && $publishableStatusChecker->isActivePublishedAt($resource) |
||
70 | ) { |
||
71 | $resource = $this->publishableNormalizer->createDraft($resource, $configuration, $resourceClass); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
72 | } |
||
73 | } |
||
74 | |||
75 | try { |
||
76 | $uploadableFileManager->setUploadedFilesFromFileBag($resource, $request->files); |
||
77 | } catch (InvalidArgumentException $exception) { |
||
78 | throw new UnprocessableEntityHttpException($exception->getMessage()); |
||
79 | } |
||
80 | |||
81 | $request->attributes->set('data', $resource); |
||
82 | |||
83 | return $resource; |
||
84 | } |
||
85 | } |
||
86 |