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

getOperationConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
nc 1
nop 2
dl 0
loc 14
ccs 0
cts 6
cp 0
crap 2
rs 9.8666
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A UploadableResourceMetadataFactory::getDownloadOperationConfiguration() 0 7 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\DownloadAction;
20
use Silverback\ApiComponentsBundle\Action\Uploadable\UploadAction;
21
use Silverback\ApiComponentsBundle\AnnotationReader\UploadableAnnotationReaderInterface;
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 $annotationReader, PathSegmentNameGeneratorInterface $pathSegmentNameGenerator)
35
    {
36 2
        $this->decorated = $decorated;
37 2
        $this->uploadableHelper = $annotationReader;
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);
49 1
        $properties = [];
50 1
        foreach ($fields as $field => $configuration) {
51 1
            $properties[$field] = [
52
                'type' => 'string',
53
                'format' => 'binary',
54
            ];
55
        }
56 1
        $resourceShortName = $resourceMetadata->getShortName();
57 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

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