Passed
Push — feature/uploadable ( 913e6d...679758 )
by Daniel
05:53
created

getClassAnnotationConfiguration()   C

Complexity

Conditions 12
Paths 17

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 12.2812

Importance

Changes 0
Metric Value
cc 12
eloc 16
nc 17
nop 2
dl 0
loc 29
ccs 14
cts 16
cp 0.875
crap 12.2812
rs 6.9666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Timestamped;
19
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
20
use Silverback\ApiComponentsBundle\Utility\ClassMetadataTrait;
21
22
/**
23
 * @author Vincent Chalamon <[email protected]>
24
 * @author Daniel West <[email protected]>
25
 */
26
abstract class AnnotationReader implements AnnotationReaderInterface
27
{
28
    use ClassMetadataTrait;
29
30
    protected Reader $reader;
31
32 9
    public function __construct(Reader $reader, ManagerRegistry $managerRegistry)
33
    {
34 9
        $this->reader = $reader;
35 9
        $this->initRegistry($managerRegistry);
36 9
    }
37
38
    abstract public function getConfiguration($class);
39
40
    /**
41
     * @param object|string $class
42
     */
43 9
    public function isConfigured($class): bool
44
    {
45
        try {
46 9
            $this->getConfiguration($class);
47 9
        } catch (InvalidArgumentException $e) {
48 9
            return false;
49
        }
50
51 9
        return true;
52
    }
53
54
    /**
55
     * @param object|string $class
56
     *
57
     * @throws \ReflectionException
58
     */
59 9
    protected function getClassAnnotationConfiguration($class, string $annotationClass)
60
    {
61 9
        if (null === $class || (\is_string($class) && !class_exists($class))) {
62
            throw new InvalidArgumentException(sprintf('$class passed to %s must be a valid class FQN or object', __CLASS__));
63
        }
64
65 9
        $originalReflection = $reflection = new \ReflectionClass($class);
66
        /** @var $annotationClass|null $annotation */
67
        while (
68 9
            !($annotation = $this->reader->getClassAnnotation($reflection, $annotationClass)) &&
69 9
            ($reflection = $reflection->getParentClass())
70
        ) {
71 9
            continue;
72
        }
73 9
        if (!$annotation && Timestamped::class === $annotationClass) {
74 9
            $traits = $originalReflection->getTraits();
75 9
            foreach ($traits as $trait) {
76 9
                $annotation = $this->reader->getClassAnnotation($trait, $annotationClass);
77 9
                if ($annotation) {
78
                    break;
79
                }
80
            }
81
        }
82
83 9
        if (!$annotation) {
84 9
            throw new InvalidArgumentException(sprintf('%s does not have %s annotation', \is_object($class) ? \get_class($class) : $class, $annotationClass));
85
        }
86
87 9
        return $annotation;
88
    }
89
}
90