Passed
Pull Request — master (#52)
by Daniel
06:09
created

getItemPutResourceMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 8
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
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 ApiPlatform\Core\Operation\PathSegmentNameGeneratorInterface;
19
use Silverback\ApiComponentBundle\Action\Uploadable\UploadableAction;
20
use Silverback\ApiComponentBundle\AnnotationReader\UploadableAnnotationReader;
21
22
/**
23
 * Configures API Platform metadata for file resources.
24
 *
25
 * @author Daniel West <[email protected]>
26
 */
27
class UploadableResourceMetadataFactory implements ResourceMetadataFactoryInterface
28
{
29
    private ResourceMetadataFactoryInterface $decorated;
30
    private UploadableAnnotationReader $uploadableHelper;
31
    private PathSegmentNameGeneratorInterface $pathSegmentNameGenerator;
32
33
    public function __construct(ResourceMetadataFactoryInterface $decorated, UploadableAnnotationReader $fileHelper, PathSegmentNameGeneratorInterface $pathSegmentNameGenerator)
34
    {
35
        $this->decorated = $decorated;
36
        $this->uploadableHelper = $fileHelper;
37
        $this->pathSegmentNameGenerator = $pathSegmentNameGenerator;
38
    }
39
40
    public function create(string $resourceClass): ResourceMetadata
41
    {
42
        $resourceMetadata = $this->decorated->create($resourceClass);
43
        if (!$this->uploadableHelper->isConfigured($resourceClass)) {
44
            return $resourceMetadata;
45
        }
46
47
        $fields = $this->uploadableHelper->getConfiguredProperties($resourceClass, false, false);
48
        $properties = [];
49
        foreach ($fields as $field) {
50
            $properties[$field] = [
51
                'type' => 'string',
52
                'format' => 'binary',
53
            ];
54
        }
55
        $resourceMetadata = $this->getCollectionPostResourceMetadata($resourceMetadata, $properties);
56
57
        return $this->getItemPutResourceMetadata($resourceMetadata, $properties);
58
    }
59
60
    private function getCollectionPostResourceMetadata(ResourceMetadata $resourceMetadata, array $properties): ResourceMetadata
61
    {
62
        $resourceShortName = $resourceMetadata->getShortName();
63
        $path = sprintf('/%s/upload', $this->pathSegmentNameGenerator->getSegmentName($resourceShortName));
64
65
        $collectionOperations = $resourceMetadata->getCollectionOperations() ?? [];
66
        $collectionOperations['post_upload'] = array_replace_recursive(
67
            $this->getOperationConfiguration($properties, $path),
68
            $collectionOperations['post'] ?? []
69
        );
70
71
        return $resourceMetadata->withCollectionOperations($collectionOperations);
72
    }
73
74
    private function getItemPutResourceMetadata(ResourceMetadata $resourceMetadata, array $properties): ResourceMetadata
75
    {
76
        $resourceShortName = $resourceMetadata->getShortName();
77
        $path = sprintf('/%s/{id}/upload', $this->pathSegmentNameGenerator->getSegmentName($resourceShortName));
78
79
        $itemOperations = $resourceMetadata->getItemOperations() ?? [];
80
        $itemOperations['put_upload'] = array_replace_recursive(
81
            $this->getOperationConfiguration($properties, $path),
82
            $itemOperations['put'] ?? []
83
        );
84
85
        return $resourceMetadata->withItemOperations($itemOperations);
86
    }
87
88
    private function getOperationConfiguration(array $properties, string $path): array
89
    {
90
        return [
91
            'controller' => UploadableAction::class,
92
            'deserialize' => false,
93
            'path' => $path,
94
            'openapi_context' => [
95
                'requestBody' => [
96
                    'content' => [
97
                        'multipart/form-data' => [
98
                            'schema' => [
99
                                'type' => 'object',
100
                                'properties' => $properties,
101
                            ],
102
                        ],
103
                    ],
104
                ],
105
            ],
106
        ];
107
    }
108
}
109