Passed
Pull Request — master (#58)
by Daniel
05:57
created

UploadableAnnotationReader::isFieldConfigured()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 6
rs 10
c 1
b 0
f 0
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 9
    public function __construct(Reader $reader, ManagerRegistry $managerRegistry, bool $imagineBundleEnabled)
33
    {
34 9
        $this->imagineBundleEnabled = $imagineBundleEnabled;
35 9
        parent::__construct($reader, $managerRegistry);
36 9
    }
37
38 9
    public function isConfigured($class): bool
39
    {
40 9
        $isConfigured = parent::isConfigured($class);
41 9
        if (!$isConfigured || $this->imagineBundleEnabled || !is_a($class, ImagineFiltersInterface::class)) {
42 9
            return $isConfigured;
43
        }
44
        throw new BadMethodCallException(sprintf('LiipImagineBundle is not enabled/installed so you should not configure Imagine filters on %s', $class));
0 ignored issues
show
Bug introduced by
$class of type Silverback\ApiComponents...ImagineFiltersInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

44
        throw new BadMethodCallException(sprintf('LiipImagineBundle is not enabled/installed so you should not configure Imagine filters on %s', /** @scrutinizer ignore-type */ $class));
Loading history...
45
    }
46
47
    /**
48
     * @param object|string $class
49
     */
50 9
    public function getConfiguration($class): Uploadable
51
    {
52 9
        return $this->getClassAnnotationConfiguration($class, Uploadable::class);
53
    }
54
55
    public function isFieldConfigured(\ReflectionProperty $property): bool
56
    {
57
        try {
58
            $this->getPropertyConfiguration($property);
59
        } catch (InvalidArgumentException $e) {
60
            return false;
61
        }
62
63
        return true;
64
    }
65
66 9
    public function getPropertyConfiguration(\ReflectionProperty $property): UploadableField
67
    {
68
        /** @var UploadableField|null $annotation */
69 9
        if (!$annotation = $this->reader->getPropertyAnnotation($property, UploadableField::class)) {
70 9
            throw new InvalidArgumentException(sprintf('%s::%s does not have %s annotation', $property->getDeclaringClass()->getName(), $property->getName(), UploadableField::class));
71
        }
72
73 9
        if (\count($annotation->imagineFilters) && !$this->imagineBundleEnabled) {
74
            throw new BadMethodCallException(sprintf('LiipImagineBundle is not enabled/installed so you should not configure Imagine filters on %s::$%s', $property->class, $property->getName()));
75
        }
76
77 9
        return $annotation;
78
    }
79
80
    /**
81
     * @param object|string $data
82
     */
83 9
    public function getConfiguredProperties($data, bool $skipUploadableCheck = false, bool $returnConfigurations = true): iterable
84
    {
85 9
        if (!$skipUploadableCheck && !$this->isConfigured($data)) {
86
            throw new UnsupportedAnnotationException(sprintf('Cannot get field configuration for %s: is it not configured as Uploadable', \is_string($data) ? $data : \get_class($data)));
87
        }
88
89 9
        $found = false;
90 9
        $reflectionProperties = (new \ReflectionClass($data))->getProperties();
91 9
        foreach ($reflectionProperties as $reflectionProperty) {
92
            try {
93 9
                $config = $this->getPropertyConfiguration($reflectionProperty);
94 9
                if ($returnConfigurations) {
95 9
                    yield $reflectionProperty->getName() => $config;
96
                } else {
97
                    yield $reflectionProperty->getName();
98
                }
99 9
                $found = true;
100 9
            } catch (InvalidArgumentException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
101
            }
102
        }
103 9
        if (!$found) {
104
            throw new UnsupportedAnnotationException(sprintf('No field configurations on your Uploadable component %s.', \is_string($data) ? $data : \get_class($data)));
105
        }
106 9
    }
107
}
108