Passed
Push — feature/uploadable ( 7c6d25...a7ed20 )
by Daniel
11:07
created

UploadableResourceMetadataFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
dl 0
loc 71
ccs 0
cts 35
cp 0
rs 10
c 1
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 18 3
A getCollectionPostResourceMetadata() 0 9 1
A getItemPutResourceMetadata() 0 11 1
A getOperationConfiguration() 0 14 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\UploadableAction;
20
use Silverback\ApiComponentsBundle\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));
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

63
        $path = sprintf('/%s/upload', $this->pathSegmentNameGenerator->getSegmentName(/** @scrutinizer ignore-type */ $resourceShortName));
Loading history...
64
65
        $collectionOperations = $resourceMetadata->getCollectionOperations() ?? [];
66
        $collectionOperations['post_upload'] = array_merge(['method' => 'POST'], $this->getOperationConfiguration($properties, $path));
67
68
        return $resourceMetadata->withCollectionOperations($collectionOperations);
69
    }
70
71
    private function getItemPutResourceMetadata(ResourceMetadata $resourceMetadata, array $properties): ResourceMetadata
72
    {
73
        $resourceShortName = $resourceMetadata->getShortName();
74
        $path = sprintf('/%s/{id}/upload', $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

74
        $path = sprintf('/%s/{id}/upload', $this->pathSegmentNameGenerator->getSegmentName(/** @scrutinizer ignore-type */ $resourceShortName));
Loading history...
75
76
        $itemOperations = $resourceMetadata->getItemOperations() ?? [];
77
        $putProperties = $this->getOperationConfiguration($properties, $path);
78
        $itemOperations['put_upload'] = array_merge(['method' => 'PUT'], $putProperties);
79
        $itemOperations['patch_upload'] = array_merge(['method' => 'PATCH'], $putProperties);
80
81
        return $resourceMetadata->withItemOperations($itemOperations);
82
    }
83
84
    private function getOperationConfiguration(array $properties, string $path): array
85
    {
86
        return [
87
            'controller' => UploadableAction::class,
88
            'path' => $path,
89
            'deserialize' => false,
90
            'read' => 'false',
91
            'openapi_context' => [
92
                'requestBody' => [
93
                    'content' => [
94
                        'multipart/form-data' => [
95
                            'schema' => [
96
                                'type' => 'object',
97
                                'properties' => $properties,
98
                            ],
99
                        ],
100
                    ],
101
                ],
102
            ],
103
        ];
104
    }
105
}
106