Passed
Push — main ( 30af20...9663ad )
by Daniel
05:47
created

UploadableAnnotationReader   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 73.68%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 35
c 2
b 0
f 0
dl 0
loc 84
ccs 28
cts 38
cp 0.7368
rs 10
wmc 23

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getConfiguration() 0 8 2
A getPropertyConfiguration() 0 13 5
A isConfigured() 0 7 5
B getConfiguredProperties() 0 18 8
A isFieldConfigured() 0 9 2
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components 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\ApiComponentsBundle\AnnotationReader;
15
16
use Doctrine\Common\Annotations\Reader;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Silverback\ApiComponentsBundle\Annotation\Uploadable;
19
use Silverback\ApiComponentsBundle\Annotation\UploadableField;
20
use Silverback\ApiComponentsBundle\Entity\Utility\ImagineFiltersInterface;
21
use Silverback\ApiComponentsBundle\Exception\BadMethodCallException;
22
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
23
use Silverback\ApiComponentsBundle\Exception\UnsupportedAnnotationException;
24
25
/**
26
 * @author Vincent Chalamon <[email protected]>
27
 */
28
final class UploadableAnnotationReader extends AnnotationReader implements UploadableAnnotationReaderInterface
29
{
30
    private bool $imagineBundleEnabled;
31
32 19
    public function __construct(Reader $reader, ManagerRegistry $managerRegistry, bool $imagineBundleEnabled)
33
    {
34 19
        $this->imagineBundleEnabled = $imagineBundleEnabled;
35 19
        parent::__construct($reader, $managerRegistry);
36 19
    }
37
38
    /**
39
     * @param object|string $class
40
     */
41 19
    public function isConfigured($class): bool
42
    {
43 19
        $isConfigured = parent::isConfigured($class);
44 19
        if (!$isConfigured || $this->imagineBundleEnabled || !is_a($class, ImagineFiltersInterface::class)) {
45 19
            return $isConfigured;
46
        }
47
        throw new BadMethodCallException(sprintf('LiipImagineBundle is not enabled/installed so you should not configure Imagine filters on %s', \is_string($class) ? $class : \get_class($class)));
0 ignored issues
show
introduced by
The condition is_string($class) is always false.
Loading history...
48
    }
49
50
    /**
51
     * @param object|string $class
52
     */
53 19
    public function getConfiguration($class): Uploadable
54
    {
55 19
        $uploadable = $this->getClassAnnotationConfiguration($class, Uploadable::class);
56 19
        if (!$uploadable instanceof Uploadable) {
57
            throw new \LogicException(sprintf('getClassAnnotationConfiguration should return the type %s', Uploadable::class));
58
        }
59
60 19
        return $uploadable;
61
    }
62
63
    public function isFieldConfigured(\ReflectionProperty $property): bool
64
    {
65
        try {
66
            $this->getPropertyConfiguration($property);
67
        } catch (InvalidArgumentException $e) {
68
            return false;
69
        }
70
71
        return true;
72
    }
73
74 19
    public function getPropertyConfiguration(\ReflectionProperty $property): UploadableField
75
    {
76
        /** @var UploadableField|null $annotation */
77 19
        $annotation = $this->reader->getPropertyAnnotation($property, UploadableField::class);
78 19
        if (!$annotation instanceof UploadableField) {
79 19
            throw new InvalidArgumentException(sprintf('%s::%s does not have %s annotation', $property->getDeclaringClass()->getName(), $property->getName(), UploadableField::class));
80
        }
81
82 19
        if (!$this->imagineBundleEnabled && null !== $annotation->imagineFilters && \count($annotation->imagineFilters)) {
83
            throw new BadMethodCallException(sprintf('LiipImagineBundle is not enabled/installed so you should not configure Imagine filters on %s::$%s', $property->class, $property->getName()));
84
        }
85
86 19
        return $annotation;
87
    }
88
89
    /**
90
     * @param object|string $data
91
     *
92
     * @return UploadableField[]|iterable
93
     */
94 19
    public function getConfiguredProperties($data, bool $skipUploadableCheck = false): iterable
95
    {
96 19
        if (!$skipUploadableCheck && !$this->isConfigured($data)) {
97
            throw new UnsupportedAnnotationException(sprintf('Cannot get field configuration for %s: is it not configured as Uploadable', \is_string($data) ? $data : \get_class($data)));
98
        }
99
100 19
        $found = false;
101 19
        $reflectionProperties = (new \ReflectionClass($data))->getProperties();
102 19
        foreach ($reflectionProperties as $reflectionProperty) {
103
            try {
104 19
                $config = $this->getPropertyConfiguration($reflectionProperty);
105 19
                yield $reflectionProperty->getName() => $config;
0 ignored issues
show
Bug Best Practice introduced by
The expression yield $reflectionProperty->getName() => $config returns the type Generator which is incompatible with the documented return type Silverback\ApiComponents...oadableField[]|iterable.
Loading history...
106 19
                $found = true;
107 19
            } catch (InvalidArgumentException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
108
            }
109
        }
110 19
        if (!$found) {
111
            throw new UnsupportedAnnotationException(sprintf('No field configurations on your Uploadable component %s.', \is_string($data) ? $data : \get_class($data)));
112
        }
113 19
    }
114
}
115