Passed
Pull Request — master (#52)
by Vincent
11:12 queued 04:00
created

UploadableHelper::getConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
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\Helper;
15
16
use Doctrine\Common\Annotations\Reader;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Silverback\ApiComponentBundle\Annotation\Uploadable;
19
use Silverback\ApiComponentBundle\Annotation\UploadableField;
20
use Silverback\ApiComponentBundle\Exception\InvalidArgumentException;
21
22
/**
23
 * @author Vincent Chalamon <[email protected]>
24
 */
25
final class UploadableHelper extends AbstractHelper
26
{
27
    public function __construct(Reader $reader, ManagerRegistry $registry)
28
    {
29
        $this->initRegistry($registry);
30
        $this->initReader($reader);
31
    }
32
33
    public function isFieldConfigured(\ReflectionProperty $property): bool
34
    {
35
        try {
36
            $this->getFieldConfiguration($property);
37
        } catch (InvalidArgumentException $e) {
38
            return false;
39
        }
40
41
        return true;
42
    }
43
44
    public function getFieldConfiguration(\ReflectionProperty $property): UploadableField
45
    {
46
        /** @var UploadableField|null $annotation */
47
        if (!$annotation = $this->reader->getPropertyAnnotation($property, UploadableField::class)) {
48
            throw new InvalidArgumentException(sprintf('%s::%s does not have %s annotation', $property->getDeclaringClass()->getName(), $property->getName(), UploadableField::class));
49
        }
50
51
        return $annotation;
52
    }
53
54
    public function getFields(object $data): iterable
55
    {
56
        foreach ($this->getClassMetadata($data)->getFieldNames() as $fieldName) {
57
            if ($this->isFieldConfigured(new \ReflectionProperty($data, $fieldName))) {
58
                yield $fieldName;
59
            }
60
        }
61
    }
62
63
    /**
64
     * @param object|string $class
65
     */
66
    public function getConfiguration($class): Uploadable
67
    {
68
        return $this->getClassAnnotationConfiguration($class, Uploadable::class);
69
    }
70
}
71