Completed
Push — master ( 29b346...1faa7f )
by Amrouche
19s
created

Factory/AnnotationSubresourceMetadataFactory.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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)) {
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) {
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
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