Passed
Push — feature/publishable ( c26268...7bd202 )
by Daniel
18:12 queued 11:31
created

FileResourceMetadataFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 34
c 1
b 0
f 0
dl 0
loc 61
ccs 0
cts 27
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 11 2
A __construct() 0 4 1
A getItemPutResourceMetadata() 0 9 1
A getOperationConfiguration() 0 15 1
A getCollectionPostResourceMetadata() 0 9 1
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