Passed
Pull Request — master (#19)
by James
05:53
created

AbstractFactory::getAnnotationReader()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 9
ccs 6
cts 7
cp 0.8571
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3.0261
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Doctrine\Factory;
6
7
use Doctrine\Common\Annotations\Reader;
8
use Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver;
9
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
10
use Doctrine\ORM\EntityManager;
11
use GraphQL\Doctrine\Exception;
12
use GraphQL\Doctrine\Factory\MetadataReader\MappingDriverChainAdapter;
13
use GraphQL\Doctrine\Types;
14
15
/**
16
 * Abstract factory to be aware of types and entityManager
17
 */
18
abstract class AbstractFactory
19
{
20
    /**
21
     * @var Types
22
     */
23
    protected $types;
24
25
    /**
26
     * @var EntityManager
27
     */
28
    protected $entityManager;
29
30 92
    public function __construct(Types $types, EntityManager $entityManager)
31
    {
32 92
        $this->types = $types;
33 92
        $this->entityManager = $entityManager;
34 92
    }
35
36
    /**
37
     * Get annotation reader
38
     *
39
     * @return Reader
40
     * @throws Exception
41
     * @throws \Doctrine\ORM\ORMException
42
     */
43 37
    final protected function getAnnotationReader(): Reader
44
    {
45 37
        $driver = $this->entityManager->getConfiguration()->getMetadataDriverImpl();
46 37
        if ($driver instanceof AnnotationDriver) {
47 34
            return $driver->getReader();
48 3
        } else if ($driver instanceof MappingDriverChain) {
49 3
            return new MappingDriverChainAdapter($driver);
50
        } else {
51
            throw new Exception('graphql-doctrine requires Doctrine to be configured with a `' . AnnotationDriver::class . '`.');
52
        }
53
    }
54
}
55