Passed
Pull Request — master (#17)
by Aleksei
03:33
created

ReaderFactory::create()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 21
rs 9.4555
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
                        'Reader must be instance of %s. %s given instead.',
42
                        ReaderInterface::class,
43
                        is_object($reader) ? 'Instance of ' . get_class($reader) : ucfirst(gettype($reader))
44
                    )
45
                );
46
        }
47
    }
48
}
49