Passed
Pull Request — master (#52)
by Daniel
06:09
created

getPropertyConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component 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\ApiComponentBundle\AnnotationReader;
15
16
use Silverback\ApiComponentBundle\Annotation\Uploadable;
17
use Silverback\ApiComponentBundle\Annotation\UploadableField;
18
use Silverback\ApiComponentBundle\Exception\InvalidArgumentException;
19
use Silverback\ApiComponentBundle\Exception\UnsupportedAnnotationException;
20
21
/**
22
 * @author Vincent Chalamon <[email protected]>
23
 */
24
final class UploadableAnnotationReader extends AbstractAnnotationReader
25
{
26
    /**
27
     * @param object|string $class
28
     */
29 9
    public function getConfiguration($class): Uploadable
30
    {
31 9
        return $this->getClassAnnotationConfiguration($class, Uploadable::class);
32
    }
33
34
    public function isFieldConfigured(\ReflectionProperty $property): bool
35
    {
36
        try {
37
            $this->getPropertyConfiguration($property);
38
        } catch (InvalidArgumentException $e) {
39
            return false;
40
        }
41
42
        return true;
43
    }
44
45 9
    public function getPropertyConfiguration(\ReflectionProperty $property): UploadableField
46
    {
47
        /** @var UploadableField|null $annotation */
48 9
        if (!$annotation = $this->reader->getPropertyAnnotation($property, UploadableField::class)) {
49 9
            throw new InvalidArgumentException(sprintf('%s::%s does not have %s annotation', $property->getDeclaringClass()->getName(), $property->getName(), UploadableField::class));
50
        }
51
52 9
        return $annotation;
53
    }
54
55
    /**
56
     * @param object|string $data
57
     */
58 9
    public function getConfiguredProperties($data, bool $skipUploadableCheck = false, bool $returnConfigurations = true): iterable
59
    {
60 9
        if (!$skipUploadableCheck && !$this->isConfigured($data)) {
61
            throw new UnsupportedAnnotationException(sprintf('Cannot get field configuration for %s: is it not configured as Uploadable', \is_string($data) ? $data : \get_class($data)));
62
        }
63
64 9
        $found = false;
65 9
        $reflectionProperties = (new \ReflectionClass($data))->getProperties();
66 9
        foreach ($reflectionProperties as $reflectionProperty) {
67
            try {
68 9
                $config = $this->getPropertyConfiguration($reflectionProperty);
69 9
                if ($returnConfigurations) {
70 9
                    yield $reflectionProperty->getName() => $config;
71
                } else {
72
                    yield $reflectionProperty->getName();
73
                }
74 9
                $found = true;
75 9
            } catch (InvalidArgumentException $e) {
76
            }
77
        }
78 9
        if (!$found) {
79
            throw new UnsupportedAnnotationException(sprintf('No field configurations on your Uploadable component %s.', \is_string($data) ? $data : \get_class($data)));
80
        }
81 9
    }
82
}
83