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

AbstractHelper::initReader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 Silverback\ApiComponentBundle\Annotation\Timestamped;
18
use Silverback\ApiComponentBundle\Exception\InvalidArgumentException;
19
use Silverback\ApiComponentBundle\Utility\ClassMetadataTrait;
20
21
abstract class AbstractHelper
22
{
23
    use ClassMetadataTrait;
24
25
    protected Reader $reader;
26
27
    abstract public function getConfiguration($class);
28
29
    /**
30
     * @param object|string $class
31
     */
32 9
    public function isConfigured($class): bool
33
    {
34
        try {
35 9
            $this->getConfiguration($class);
36 9
        } catch (InvalidArgumentException $e) {
37 9
            return false;
38
        }
39
40 9
        return true;
41
    }
42
43
    /**
44
     * @required
45
     */
46 9
    protected function initReader(Reader $reader): void
47
    {
48 9
        $this->reader = $reader;
49 9
    }
50
51
    /**
52
     * @param object|string $class
53
     *
54
     * @throws \ReflectionException
55
     */
56 9
    protected function getClassAnnotationConfiguration($class, string $annotationClass)
57
    {
58 9
        if (null === $class || (\is_string($class) && !class_exists($class))) {
59
            throw new InvalidArgumentException(sprintf('$class passed to %s must be a valid class FQN or object', __CLASS__));
60
        }
61
62 9
        $originalReflection = $reflection = new \ReflectionClass($class);
63
        /** @var $annotationClass|null $annotation */
64
        while (
65 9
            !($annotation = $this->reader->getClassAnnotation($reflection, $annotationClass)) &&
66 9
            ($reflection = $reflection->getParentClass())
67
        ) {
68 9
            continue;
69
        }
70 9
        if (!$annotation && Timestamped::class === $annotationClass) {
71 9
            $traits = $originalReflection->getTraits();
72 9
            foreach ($traits as $trait) {
73 9
                $annotation = $this->reader->getClassAnnotation($trait, $annotationClass);
74 9
                if ($annotation) {
75
                    break;
76
                }
77
            }
78
        }
79
80 9
        if (!$annotation) {
81 9
            throw new InvalidArgumentException(sprintf('%s does not have %s annotation', \is_object($class) ? \get_class($class) : $class, $annotationClass));
82
        }
83
84 9
        return $annotation;
85
    }
86
}
87