Passed
Pull Request — master (#58)
by Daniel
06:59
created

UploadableResourceMetadataFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 51
dl 0
loc 87
ccs 44
cts 44
cp 1
rs 10
c 1
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getUploadOperationConfiguration() 0 13 1
A create() 0 23 3
A getCollectionPostResourceMetadata() 0 8 1
A getItemPutResourceMetadata() 0 16 2
A getDownloadOperationConfiguration() 0 5 1
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\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\ApiComponentsBundle\Action\Uploadable\UploadableUploadAction;
20
use Silverback\ApiComponentsBundle\AnnotationReader\UploadableAnnotationReaderInterface;
21
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
22
23
/**
24
 * Configures API Platform metadata for file resources.
25
 *
26
 * @author Daniel West <[email protected]>
27
 */
28
class UploadableResourceMetadataFactory implements ResourceMetadataFactoryInterface
29
{
30
    private ResourceMetadataFactoryInterface $decorated;
31
    private UploadableAnnotationReaderInterface $uploadableHelper;
32
    private PathSegmentNameGeneratorInterface $pathSegmentNameGenerator;
33
34 2
    public function __construct(ResourceMetadataFactoryInterface $decorated, UploadableAnnotationReaderInterface $uploadableHelper, PathSegmentNameGeneratorInterface $pathSegmentNameGenerator)
35
    {
36 2
        $this->decorated = $decorated;
37 2
        $this->uploadableHelper = $uploadableHelper;
38 2
        $this->pathSegmentNameGenerator = $pathSegmentNameGenerator;
39 2
    }
40
41 2
    public function create(string $resourceClass): ResourceMetadata
42
    {
43 2
        $resourceMetadata = $this->decorated->create($resourceClass);
44 2
        if (!$this->uploadableHelper->isConfigured($resourceClass)) {
45 1
            return $resourceMetadata;
46
        }
47
48 1
        $fields = $this->uploadableHelper->getConfiguredProperties($resourceClass, false, false);
49 1
        $properties = [];
50 1
        $fieldsAsSnakeCase = [];
51 1
        $camelCaseToSnakeCaseConverter = new CamelCaseToSnakeCaseNameConverter();
52 1
        foreach ($fields as $field) {
53 1
            $properties[$field] = [
54
                'type' => 'string',
55
                'format' => 'binary',
56
            ];
57 1
            $fieldsAsSnakeCase[] = $camelCaseToSnakeCaseConverter->normalize($field);
58
        }
59 1
        $resourceShortName = $resourceMetadata->getShortName();
60 1
        $pathSegmentName = $this->pathSegmentNameGenerator->getSegmentName($resourceShortName);
0 ignored issues
show
Bug introduced by
It seems like $resourceShortName can also be of type null; however, parameter $name of ApiPlatform\Core\Operati...rface::getSegmentName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        $pathSegmentName = $this->pathSegmentNameGenerator->getSegmentName(/** @scrutinizer ignore-type */ $resourceShortName);
Loading history...
61 1
        $resourceMetadata = $this->getCollectionPostResourceMetadata($resourceMetadata, $properties, $pathSegmentName);
62
63 1
        return $this->getItemPutResourceMetadata($resourceMetadata, $properties, $pathSegmentName, $fieldsAsSnakeCase);
64
    }
65
66 1
    private function getCollectionPostResourceMetadata(ResourceMetadata $resourceMetadata, array $properties, string $pathSegmentName): ResourceMetadata
67
    {
68 1
        $path = sprintf('/%s/upload', $pathSegmentName);
69
70 1
        $collectionOperations = $resourceMetadata->getCollectionOperations() ?? [];
71 1
        $collectionOperations['post_upload'] = array_merge(['method' => 'POST'], $this->getUploadOperationConfiguration($properties, $path));
72
73 1
        return $resourceMetadata->withCollectionOperations($collectionOperations);
74
    }
75
76 1
    private function getItemPutResourceMetadata(ResourceMetadata $resourceMetadata, array $properties, string $pathSegmentName, array $fieldsAsSnakeCase): ResourceMetadata
77
    {
78 1
        $uploadPath = sprintf('/%s/{id}/upload', $pathSegmentName);
79
80 1
        $itemOperations = $resourceMetadata->getItemOperations() ?? [];
81 1
        $putProperties = $this->getUploadOperationConfiguration($properties, $uploadPath);
82 1
        $itemOperations['put_upload'] = array_merge(['method' => 'PUT'], $putProperties);
83 1
        $itemOperations['patch_upload'] = array_merge(['method' => 'PATCH'], $putProperties);
84
85 1
        $downloadPath = sprintf('/%s/{id}/download/', $pathSegmentName);
86 1
        foreach ($fieldsAsSnakeCase as $fieldName) {
87 1
            $propertyDownloadPath = $downloadPath . $fieldName;
88 1
            $itemOperations['download_' . $fieldName] = array_merge(['method' => 'GET'], $this->getDownloadOperationConfiguration($propertyDownloadPath));
89
        }
90
91 1
        return $resourceMetadata->withItemOperations($itemOperations);
92
    }
93
94 1
    private function getDownloadOperationConfiguration(string $path): array
95
    {
96
        return [
97 1
            'controller' => UploadableUploadAction::class,
98 1
            'path' => $path,
99
        ];
100
    }
101
102 1
    private function getUploadOperationConfiguration(array $properties, string $path): array
103
    {
104
        return [
105 1
            'controller' => UploadableUploadAction::class,
106 1
            'path' => $path,
107
            'deserialize' => false,
108
            'openapi_context' => [
109
                'requestBody' => [
110
                    'content' => [
111
                        'multipart/form-data' => [
112
                            'schema' => [
113 1
                                'type' => 'object',
114 1
                                'properties' => $properties,
115
                            ],
116
                        ],
117
                    ],
118
                ],
119
            ],
120
        ];
121
    }
122
}
123