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

MediaObjectContextBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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\Serializer\ContextBuilder;
15
16
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
17
use Silverback\ApiComponentBundle\Serializer\MappingLoader\TimestampedLoader;
18
use Symfony\Component\HttpFoundation\Request;
19
20
/**
21
 * @author Daniel West <[email protected]>
22
 */
23
final class MediaObjectContextBuilder 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
        if (empty($resourceClass = $context['resource_class']) || empty($context['groups'])) {
36
            return $context;
37
        }
38
39
        $reflectionClass = new \ReflectionClass($resourceClass);
40
        if ($normalization) {
41
            $context['groups'][] = sprintf('%s:%s:read', $reflectionClass->getShortName(), TimestampedLoader::GROUP_NAME);
42
        }
43
44
        return $context;
45
    }
46
}
47