Passed
Push — master ( b821d4...2e63df )
by Adrien
08:30
created

AbstractFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 14
c 1
b 0
f 0
dl 0
loc 51
ccs 15
cts 15
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAnnotationReader() 0 12 3
A __construct() 0 4 1
A isPropertyExcluded() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Doctrine\Factory;
6
7
use Doctrine\Common\Annotations\Reader;
8
use Doctrine\ORM\EntityManager;
9
use Doctrine\ORM\Mapping\ClassMetadata;
10
use Doctrine\Persistence\Mapping\Driver\AnnotationDriver;
11
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
12
use GraphQL\Doctrine\Annotation\Exclude;
13
use GraphQL\Doctrine\Exception;
14
use GraphQL\Doctrine\Factory\MetadataReader\MappingDriverChainAdapter;
15
use GraphQL\Doctrine\Types;
16
17
/**
18
 * Abstract factory to be aware of types and entityManager
19
 */
20
abstract class AbstractFactory
21
{
22
    /**
23
     * @var Types
24
     */
25
    protected $types;
26
27
    /**
28
     * @var EntityManager
29
     */
30
    protected $entityManager;
31
32 106
    public function __construct(Types $types, EntityManager $entityManager)
33
    {
34 106
        $this->types = $types;
35 106
        $this->entityManager = $entityManager;
36 106
    }
37
38
    /**
39
     * Get annotation reader
40
     *
41
     * @return Reader
42
     */
43 43
    final protected function getAnnotationReader(): Reader
44
    {
45 43
        $driver = $this->entityManager->getConfiguration()->getMetadataDriverImpl();
46 43
        if ($driver instanceof AnnotationDriver) {
47 39
            return $driver->getReader();
48
        }
49
50 4
        if ($driver instanceof MappingDriverChain) {
51 3
            return new MappingDriverChainAdapter($driver);
52
        }
53
54 1
        throw new Exception('graphql-doctrine requires Doctrine to be configured with a `' . AnnotationDriver::class . '`.');
55
    }
56
57
    /**
58
     * Returns whether the property is excluded
59
     *
60
     * @param ClassMetadata $metadata
61
     * @param string $propertyName
62
     *
63
     * @return bool
64
     */
65 16
    final protected function isPropertyExcluded(ClassMetadata $metadata, string $propertyName): bool
66
    {
67 16
        $property = $metadata->getReflectionProperty($propertyName);
68 16
        $exclude = $this->getAnnotationReader()->getPropertyAnnotation($property, Exclude::class);
69
70 16
        return $exclude !== null;
71
    }
72
}
73