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
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Daniel West <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class UploadAction |
29
|
|
|
{ |
30
|
|
|
public function __invoke(?object $data, Request $request, UploadableFileManager $uploadableFileManager, PublishableStatusChecker $publishableStatusChecker, PublishableNormalizer $publishableNormalizer) |
31
|
|
|
{ |
32
|
|
|
$contentType = $request->headers->get('CONTENT_TYPE'); |
33
|
|
|
if (null === $contentType) { |
|
|
|
|
34
|
|
|
throw new UnsupportedMediaTypeHttpException('The "Content-Type" header must exist.'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$contentType = explode(';', $contentType)[0]; |
38
|
|
|
$formats = ['multipart/form-data']; |
39
|
|
|
if (!\in_array(strtolower($contentType), $formats, true)) { |
40
|
|
|
throw new UnsupportedMediaTypeHttpException(sprintf('The content-type "%s" is not supported. Supported MIME type is "%s".', $contentType, implode('", "', $formats))); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$resourceClass = $request->attributes->get('_api_resource_class'); |
44
|
|
|
$resource = $data ?? new $resourceClass(); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* if it IS publishable |
48
|
|
|
* if NOT asking to update published ?published=true |
49
|
|
|
* if it IS currently published |
50
|
|
|
* if the user DOES have permission. |
51
|
|
|
*/ |
52
|
|
|
$publishableAnnotationReader = $publishableStatusChecker->getAnnotationReader(); |
53
|
|
|
if ($publishableAnnotationReader->isConfigured($resource)) { |
54
|
|
|
$configuration = $publishableAnnotationReader->getConfiguration($resource); |
55
|
|
|
$isGranted = $publishableStatusChecker->isGranted($resource); |
56
|
|
|
if (!$data) { |
57
|
|
|
if (!$isGranted) { |
58
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
59
|
|
|
$accessor->setValue($resource, $configuration->fieldName, date('Y-m-d H:i:s')); |
60
|
|
|
} |
61
|
|
|
} elseif ( |
62
|
|
|
$isGranted && |
63
|
|
|
!$publishableStatusChecker->isPublishedRequest($request) && |
64
|
|
|
$publishableStatusChecker->isActivePublishedAt($resource) |
65
|
|
|
) { |
66
|
|
|
$resource = $publishableNormalizer->createDraft($resource, $configuration, $resourceClass); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
$uploadableFileManager->setUploadedFilesFromFileBag($resource, $request->files); |
72
|
|
|
} catch (InvalidArgumentException $exception) { |
73
|
|
|
throw new UnprocessableEntityHttpException($exception->getMessage()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$request->attributes->set('data', $resource); |
77
|
|
|
|
78
|
|
|
return $resource; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|