1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component 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\ApiComponentBundle\ApiPlatform\Metadata\Resource; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
17
|
|
|
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
18
|
|
|
use Silverback\ApiComponentBundle\Action\File\FileAction; |
19
|
|
|
use Silverback\ApiComponentBundle\Annotation\File; |
20
|
|
|
use Silverback\ApiComponentBundle\Helper\FileHelper; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Configures API Platform metadata for file resources. |
24
|
|
|
* |
25
|
|
|
* @author Daniel West <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class FileResourceMetadataFactory implements ResourceMetadataFactoryInterface |
28
|
|
|
{ |
29
|
|
|
private ResourceMetadataFactoryInterface $decorated; |
30
|
|
|
private FileHelper $fileHelper; |
31
|
|
|
|
32
|
|
|
public function __construct(ResourceMetadataFactoryInterface $decorated, FileHelper $fileHelper) |
33
|
|
|
{ |
34
|
|
|
$this->decorated = $decorated; |
35
|
|
|
$this->fileHelper = $fileHelper; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function create(string $resourceClass): ResourceMetadata |
39
|
|
|
{ |
40
|
|
|
$resourceMetadata = $this->decorated->create($resourceClass); |
41
|
|
|
if (!$this->fileHelper->isConfigured($resourceClass)) { |
42
|
|
|
return $resourceMetadata; |
43
|
|
|
} |
44
|
|
|
$fileConfiguration = $this->fileHelper->getConfiguration($resourceClass); |
45
|
|
|
|
46
|
|
|
$resourceMetadata = $this->getCollectionPostResourceMetadata($resourceMetadata, $fileConfiguration); |
47
|
|
|
|
48
|
|
|
return $this->getItemPutResourceMetadata($resourceMetadata, $fileConfiguration); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function getCollectionPostResourceMetadata(ResourceMetadata $resourceMetadata, File $fileConfiguration): ResourceMetadata |
52
|
|
|
{ |
53
|
|
|
$collectionOperations = $resourceMetadata->getCollectionOperations() ?? []; |
54
|
|
|
$collectionOperations['post'] = array_replace_recursive( |
55
|
|
|
$this->getOperationConfiguration($fileConfiguration), |
56
|
|
|
$collectionOperations['post'] ?? [] |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
return $resourceMetadata->withCollectionOperations($collectionOperations); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function getItemPutResourceMetadata(ResourceMetadata $resourceMetadata, File $fileConfiguration): ResourceMetadata |
63
|
|
|
{ |
64
|
|
|
$itemOperations = $resourceMetadata->getItemOperations() ?? []; |
65
|
|
|
$itemOperations['put'] = array_replace_recursive( |
66
|
|
|
$this->getOperationConfiguration($fileConfiguration), |
67
|
|
|
$itemOperations['put'] ?? [] |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
return $resourceMetadata->withItemOperations($itemOperations); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function getOperationConfiguration(File $fileConfiguration): array |
74
|
|
|
{ |
75
|
|
|
return [ |
76
|
|
|
'controller' => FileAction::class, |
77
|
|
|
'deserialize' => false, |
78
|
|
|
'openapi_context' => [ |
79
|
|
|
'requestBody' => [ |
80
|
|
|
'content' => [ |
81
|
|
|
'multipart/form-data' => [ |
82
|
|
|
'schema' => [ |
83
|
|
|
'type' => 'object', |
84
|
|
|
'properties' => [ |
85
|
|
|
$fileConfiguration->fileFieldName => [ |
86
|
|
|
'type' => 'string', |
87
|
|
|
'format' => 'binary', |
88
|
|
|
], |
89
|
|
|
], |
90
|
|
|
], |
91
|
|
|
], |
92
|
|
|
], |
93
|
|
|
], |
94
|
|
|
], |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|