Passed
Push — feature/uploadable ( 7c6d25...a7ed20 )
by Daniel
11:07
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 AbstractAnnotationReader
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 implement %s', ImagineFiltersInterface::class));
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
        return $annotation;
74
    }
75
76
    /**
77
     * @param object|string $data
78
     */
79 9
    public function getConfiguredProperties($data, bool $skipUploadableCheck = false, bool $returnConfigurations = true): iterable
80
    {
81 9
        if (!$skipUploadableCheck && !$this->isConfigured($data)) {
82
            throw new UnsupportedAnnotationException(sprintf('Cannot get field configuration for %s: is it not configured as Uploadable', \is_string($data) ? $data : \get_class($data)));
83
        }
84
85 9
        $found = false;
86 9
        $reflectionProperties = (new \ReflectionClass($data))->getProperties();
87 9
        foreach ($reflectionProperties as $reflectionProperty) {
88
            try {
89 9
                $config = $this->getPropertyConfiguration($reflectionProperty);
90 9
                if ($returnConfigurations) {
91 9
                    yield $reflectionProperty->getName() => $config;
92
                } else {
93
                    yield $reflectionProperty->getName();
94
                }
95 9
                $found = true;
96 9
            } catch (InvalidArgumentException $e) {
97
            }
98
        }
99 9
        if (!$found) {
100
            throw new UnsupportedAnnotationException(sprintf('No field configurations on your Uploadable component %s.', \is_string($data) ? $data : \get_class($data)));
101
        }
102 9
    }
103
}
104