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

TimestampedContextBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 10
c 1
b 0
f 0
dl 0
loc 22
ccs 0
cts 11
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A createFromRequest() 0 13 4
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 TimestampedContextBuilder 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