Passed
Pull Request — main (#155)
by Daniel
05:12
created

MetadataContextBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 25
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromRequest() 0 16 5
A __construct() 0 3 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\Serializer\ContextBuilder;
15
16
use ApiPlatform\Serializer\SerializerContextBuilderInterface;
17
use Silverback\ApiComponentsBundle\Serializer\MappingLoader\UploadableLoader;
18
use Symfony\Component\HttpFoundation\Request;
19
20
/**
21
 * @author Daniel West <[email protected]>
22
 */
23
final class MetadataContextBuilder implements SerializerContextBuilderInterface
24
{
25
    private SerializerContextBuilderInterface $decorated;
26
27
    public function __construct(SerializerContextBuilderInterface $decorated)
28
    {
29
        $this->decorated = $decorated;
30
    }
31
32
    public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null): array
33
    {
34
        $context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);
35
36
        if (empty($resourceClass = $context['resource_class']) || empty($context['groups']) || \in_array('Route:manifest:read', $context['groups'], true)) {
37
            return $context;
38
        }
39
40
        $reflectionClass = new \ReflectionClass($resourceClass);
41
        if ($normalization) {
42
            $context['groups'][] = sprintf('%s:%s:read', $reflectionClass->getShortName(), UploadableLoader::GROUP_NAME);
43
        } else {
44
            $context['groups'][] = sprintf('%s:%s:write', $reflectionClass->getShortName(), UploadableLoader::GROUP_NAME);
45
        }
46
47
        return $context;
48
    }
49
}
50