Passed
Pull Request — master (#17)
by Aleksei
02:50
created

ReaderFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
eloc 17
c 3
b 0
f 0
dl 0
loc 29
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 22 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Annotated;
6
7
use Doctrine\Common\Annotations\Reader as DoctrineReader;
8
use Spiral\Attributes\AnnotationReader;
9
use Spiral\Attributes\AttributeReader;
10
use Spiral\Attributes\Composite\SelectiveReader;
11
use Spiral\Attributes\ReaderInterface;
12
13
final class ReaderFactory
14
{
15
    /**
16
     * @param object<ReaderInterface|DoctrineReader>|null $reader
17
     *
18
     * @return ReaderInterface
19
     */
20
    public static function create(object $reader = null): ReaderInterface
21
    {
22
        switch (true) {
23
            case $reader instanceof ReaderInterface:
24
                return $reader;
25
26
            case $reader instanceof DoctrineReader:
27
                return new AnnotationReader($reader);
28
29
            case $reader === null:
30
                return new SelectiveReader([
31
                    new AttributeReader(),
32
                    new AnnotationReader(),
33
                ]);
34
35
            default:
36
                throw new \InvalidArgumentException(
37
                    sprintf(
38
                        'Argument $reader must be an instance of %s or %s, but %s passed.',
39
                        ReaderInterface::class,
40
                        DoctrineReader::class,
41
                        'instance of ' . explode("\0", get_class($reader))[0]
42
                    )
43
                );
44
        }
45
    }
46
}
47