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

ReaderFactory::create()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 24
rs 9.3554
cc 5
nc 4
nop 1
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 ReaderInterface|DoctrineReader|null $reader
17
     *
18
     * @return ReaderInterface
19
     *
20
     * @psalm-type ReaderType = ReaderInterface | DoctrineAnnotationReader | null
21
     * @psalm-param ReaderType $reader
22
     */
23
    public static function create($reader = null): ReaderInterface
24
    {
25
        switch (true) {
26
            case $reader instanceof ReaderInterface:
27
                return $reader;
28
29
            case $reader instanceof DoctrineReader:
30
                return new AnnotationReader($reader);
31
32
            case $reader === null:
33
                return new SelectiveReader([
34
                    new AttributeReader(),
35
                    new AnnotationReader(),
36
                ]);
37
38
            default:
39
                throw new \InvalidArgumentException(
40
                    sprintf(
41
                        'Argument $reader must be an instance of %s or %s, but %s passed.',
42
                        ReaderInterface::class,
43
                        DoctrineReader::class,
44
                        is_object($reader)
45
                            ? 'instance of ' . explode("\0", get_class($reader))[0]
46
                            : gettype($reader)
47
                    )
48
                );
49
        }
50
    }
51
}
52