Passed
Push — master ( 8bd912...d93388 )
by Alan
06:58 queued 02:20
created

AnnotationResourceFilterMetadataFactory.php (2 issues)

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\Resource\Factory;
15
16
use ApiPlatform\Core\Annotation\ApiFilter;
17
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
18
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
19
use ApiPlatform\Core\Util\AnnotationFilterExtractorTrait;
20
use Doctrine\Common\Annotations\Reader;
21
22
/**
23
 * Adds filters to the resource metadata {@see ApiFilter} annotation.
24
 *
25
 * @author Antoine Bluchet <[email protected]>
26
 */
27
final class AnnotationResourceFilterMetadataFactory implements ResourceMetadataFactoryInterface
28
{
29
    use AnnotationFilterExtractorTrait;
0 ignored issues
show
The trait ApiPlatform\Core\Util\An...ionFilterExtractorTrait requires some properties which are not provided by ApiPlatform\Core\Metadat...ceFilterMetadataFactory: $id, $properties, $strategy, $filterClass, $arguments
Loading history...
30
31
    private $reader;
32
    private $decorated;
33
34
    public function __construct(Reader $reader, ResourceMetadataFactoryInterface $decorated = null)
35
    {
36
        $this->reader = $reader;
37
        $this->decorated = $decorated;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function create(string $resourceClass): ResourceMetadata
44
    {
45
        $parentResourceMetadata = null;
46
        if ($this->decorated) {
47
            try {
48
                $parentResourceMetadata = $this->decorated->create($resourceClass);
49
            } catch (ResourceClassNotFoundException $resourceNotFoundException) {
50
                // Ignore not found exception from decorated factories
51
            }
52
        }
53
54
        if (null === $parentResourceMetadata) {
55
            return $this->handleNotFound($parentResourceMetadata, $resourceClass);
56
        }
57
58
        try {
59
            $reflectionClass = new \ReflectionClass($resourceClass);
60
        } catch (\ReflectionException $reflectionException) {
61
            return $this->handleNotFound($parentResourceMetadata, $resourceClass);
62
        }
63
64
        $filters = array_keys($this->readFilterAnnotations($reflectionClass, $this->reader));
65
66
        if (!$filters) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filters of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
67
            return $parentResourceMetadata;
68
        }
69
70
        $parentFilters = $parentResourceMetadata->getAttribute('filters', []);
71
72
        if ($parentFilters) {
73
            $filters = array_merge($parentFilters, $filters);
74
        }
75
76
        $attributes = $parentResourceMetadata->getAttributes();
77
78
        if (!$attributes) {
79
            $attributes = [];
80
        }
81
82
        return $parentResourceMetadata->withAttributes(array_merge($attributes, ['filters' => $filters]));
83
    }
84
85
    /**
86
     * Returns the metadata from the decorated factory if available or throws an exception.
87
     *
88
     * @throws ResourceClassNotFoundException
89
     */
90
    private function handleNotFound(?ResourceMetadata $parentPropertyMetadata, string $resourceClass): ResourceMetadata
91
    {
92
        if (null !== $parentPropertyMetadata) {
93
            return $parentPropertyMetadata;
94
        }
95
96
        throw new ResourceClassNotFoundException(sprintf('Resource "%s" not found.', $resourceClass));
97
    }
98
}
99