Passed
Pull Request — master (#17)
by Aleksei
02:31
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\AnnotationReader as DoctrineAnnotationReader;
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|DoctrineAnnotationReader|null $reader
17
     * @return ReaderInterface
18
     *
19
     * @psalm-type ReaderType = ReaderInterface | DoctrineAnnotationReader | null
20
     * @psalm-param ReaderType $reader
21
     */
22
    public static function create($reader = null): ReaderInterface
23
    {
24
        switch (true) {
25
            case $reader instanceof ReaderInterface:
26
                return $reader;
27
28
            case $reader instanceof DoctrineAnnotationReader:
29
                return new AnnotationReader($reader);
30
31
            case $reader === null:
32
                return new SelectiveReader([
33
                    new AttributeReader(),
34
                    new AnnotationReader(),
35
                ]);
36
37
            default:
38
                throw new \InvalidArgumentException(
39
                    sprintf(
40
                        'Reader must be instance of %s. %s given instead.',
41
                        ReaderInterface::class,
42
                        is_object($reader) ? 'Instance of ' . get_class($reader) : ucfirst(gettype($reader))
43
                    )
44
                );
45
        }
46
    }
47
}
48