Completed
Pull Request — master (#3367)
by Alan
05:13
created

SerializerContextBuilder::createFromRequest()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 3
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[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 ApiPlatform\Core\Serializer;
15
16
use ApiPlatform\Core\Exception\RuntimeException;
17
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
18
use ApiPlatform\Core\Util\RequestAttributesExtractor;
19
use Symfony\Component\HttpFoundation\Request;
20
21
/**
22
 * {@inheritdoc}
23
 *
24
 * @author Kévin Dunglas <[email protected]>
25
 *
26
 * @deprecated since API Platform 2.6, use {@see \ApiPlatform\Core\Serializer\SerializerContextProvider} class instead
27
 */
28
final class SerializerContextBuilder implements SerializerContextBuilderInterface
0 ignored issues
show
Deprecated Code introduced by
The interface ApiPlatform\Core\Seriali...ContextBuilderInterface has been deprecated: since API Platform 2.6, use {@see \ApiPlatform\Core\Serializer\SerializerContextProviderInterface} interface instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

28
final class SerializerContextBuilder implements /** @scrutinizer ignore-deprecated */ SerializerContextBuilderInterface

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
29
{
30
    use ContextTrait;
0 ignored issues
show
introduced by
The trait ApiPlatform\Core\Serializer\ContextTrait requires some properties which are not provided by ApiPlatform\Core\Seriali...erializerContextBuilder: $query, $attributes
Loading history...
31
32
    private $serializerContextProvider;
33
34
    public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, SerializerContextProviderInterface $serializerContextProvider = null)
35
    {
36
        trigger_deprecation('API Platform', '2.6', 'Using "%s" class is deprecated, use "%s" class instead.', __CLASS__, SerializerContextProvider::class);
37
38
        $this->serializerContextProvider = $serializerContextProvider ?? new SerializerContextProvider($resourceMetadataFactory);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function createFromRequest(Request $request, bool $normalization, array $attributes = null): array
45
    {
46
        trigger_deprecation('API Platform', '2.6', 'Using "%s()" method is deprecated, use "%s::create()" method instead.', __METHOD__, SerializerContextProvider::class);
47
48
        if (null === $attributes && !$attributes = RequestAttributesExtractor::extractAttributes($request)) {
49
            throw new RuntimeException('Request attributes are not valid.');
50
        }
51
52
        $context = $this->addRequestContext($request, $attributes);
53
        $operationName = $this->getOperationNameFromContext($context);
54
55
        return $this->serializerContextProvider->create($context['resource_class'], $operationName, $normalization, $context);
56
    }
57
}
58