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

AbstractFactory::isPropertyExcluded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 2
crap 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