Completed
Push — master ( 7b8f41...eb47c5 )
by Kévin
02:52
created

AnnotationSubresourceMetadataFactory::create()   C

Complexity

Conditions 8
Paths 12

Size

Total Lines 38
Code Lines 21

Duplication

Lines 23
Ratio 60.53 %

Importance

Changes 0
Metric Value
dl 23
loc 38
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 21
nc 12
nop 3
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\Metadata\Property\Factory;
15
16
use ApiPlatform\Core\Annotation\ApiSubresource;
17
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
18
use ApiPlatform\Core\Metadata\Property\SubresourceMetadata;
19
use ApiPlatform\Core\Util\Reflection;
20
use Doctrine\Common\Annotations\Reader;
21
22
/**
23
 * Adds subresources to the properties metadata from {@see ApiResource} annotations.
24
 *
25
 * @author Antoine Bluchet <[email protected]>
26
 */
27
final class AnnotationSubresourceMetadataFactory implements PropertyMetadataFactoryInterface
28
{
29
    private $reader;
30
    private $decorated;
31
32
    public function __construct(Reader $reader, PropertyMetadataFactoryInterface $decorated)
33
    {
34
        $this->reader = $reader;
35
        $this->decorated = $decorated;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata
42
    {
43
        $propertyMetadata = $this->decorated->create($resourceClass, $property, $options);
44
45
        try {
46
            $reflectionClass = new \ReflectionClass($resourceClass);
47
        } catch (\ReflectionException $reflectionException) {
48
            return $propertyMetadata;
49
        }
50
51 View Code Duplication
        if ($reflectionClass->hasProperty($property)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
            $annotation = $this->reader->getPropertyAnnotation($reflectionClass->getProperty($property), ApiSubresource::class);
53
54
            if (null !== $annotation) {
55
                return $this->updateMetadata($annotation, $propertyMetadata);
56
            }
57
        }
58
59 View Code Duplication
        foreach (array_merge(Reflection::ACCESSOR_PREFIXES, Reflection::MUTATOR_PREFIXES) as $prefix) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
            $methodName = $prefix.ucfirst($property);
61
            if (!$reflectionClass->hasMethod($methodName)) {
62
                continue;
63
            }
64
65
            $reflectionMethod = $reflectionClass->getMethod($methodName);
66
            if (!$reflectionMethod->isPublic()) {
67
                continue;
68
            }
69
70
            $annotation = $this->reader->getMethodAnnotation($reflectionMethod, ApiSubresource::class);
71
72
            if (null !== $annotation) {
73
                return $this->updateMetadata($annotation, $propertyMetadata);
74
            }
75
        }
76
77
        return $propertyMetadata;
78
    }
79
80
    private function updateMetadata(ApiSubresource $annotation, PropertyMetadata $propertyMetadata): PropertyMetadata
0 ignored issues
show
Unused Code introduced by
The parameter $annotation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    {
82
        $type = $propertyMetadata->getType();
83
        $isCollection = $type->isCollection();
84
        $resourceClass = $isCollection ? $type->getCollectionValueType()->getClassName() : $type->getClassName();
85
86
        return $propertyMetadata->withSubresource(new SubresourceMetadata($resourceClass, $isCollection));
87
    }
88
}
89