Completed
Push — master ( e03b67...e42da4 )
by
unknown
03:39 queued 10s
created

DoctrineEntityExtractor::isEntityRelation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Smart\EtlBundle\Extractor;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityRepository;
7
use Doctrine\ORM\QueryBuilder;
8
use Smart\EtlBundle\Entity\ImportableInterface;
9
use Smart\EtlBundle\Exception\Extractor\EntityTypeNotHandledException;
10
use Symfony\Component\PropertyAccess\PropertyAccess;
11
use Symfony\Component\PropertyAccess\PropertyAccessor;
12
13
/**
14
 * Nicolas Bastien <[email protected]>
15
 */
16
class DoctrineEntityExtractor extends AbstractExtractor implements ExtractorInterface
17
{
18
    /**
19
     * @var EntityManager
20
     */
21
    protected $entityManager;
22
23
    /**
24
     * @var PropertyAccessor
25
     */
26
    protected $accessor;
27
28
    /**
29
     * @var string
30
     */
31
    protected $entityToExtract;
32
33
    /**
34
     * @var array
35
     */
36
    protected $propertiesToExtract;
37
38
    /**
39
     * @var QueryBuilder
40
     */
41
    protected $queryBuilder = null;
42
43 1
    public function __construct($entityManager)
44
    {
45 1
        $this->entityManager = $entityManager;
46 1
        $this->accessor = PropertyAccess::createPropertyAccessor();
47 1
    }
48
49
    /**
50
     * @param string $entityToExtract
51
     */
52 1
    public function setEntityToExtract($entityToExtract, array $propertiesToExtract)
53
    {
54 1
        $this->entityToExtract = $entityToExtract;
55 1
        $this->propertiesToExtract = $propertiesToExtract;
56 1
        $this->queryBuilder = null;
57 1
    }
58
59
    /**
60
     * @return QueryBuilder
61
     */
62 1
    public function getQueryBuilder()
63
    {
64
65 1
        $repository = $this->entityManager->getRepository($this->entityToExtract);
66 1
        if (!$repository instanceof EntityRepository) {
67
            throw new \UnexpectedValueException("No repository found for class {$this->entityToExtract}");
68
        }
69 1
        $this->queryBuilder = $repository->createQueryBuilder('o');
70
71 1
        if ($this->queryBuilder === null) {
72
            throw new \BadMethodCallException('Invalid entityToExtract');
73
        }
74
75 1
        return $this->queryBuilder;
76
    }
77
78
    /**
79
     * @param QueryBuilder $queryBuilder
80
     */
81 1
    public function setQueryBuilder(QueryBuilder $queryBuilder)
82
    {
83 1
        $this->queryBuilder = $queryBuilder;
84 1
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89 1
    public function extract()
90
    {
91 1
        $entities = $this->queryBuilder->getQuery()->getResult();
92
93
        //Replace relation references
94 1
        foreach ($entities as $key => $entity) {
95 1
            if (!$entity instanceof ImportableInterface) {
96
                throw new EntityTypeNotHandledException(get_class($entity));
97
            }
98 1
            $entityData = [];
99 1
            foreach ($this->propertiesToExtract as $property) {
100 1
                $value = $this->accessor->getValue($entity, $property);
101
102 1
                if ($this->isEntityRelation($value)) {
103 1
                    if (!$value instanceof ImportableInterface) {
104
                        throw new EntityTypeNotHandledException(get_class($value));
105
                    }
106 1
                    $entityData[$property] = '@' . $value->getImportId();
107
                } else {
108 1
                    $entityData[$property] = $value;
109
                }
110
            }
111 1
            $entities[$entity->getImportId()] = $entityData;
112 1
            unset($entities[$key]);
113
        }
114
115 1
        return $entities;
116
    }
117
118
    /**
119
     * Check if $propertyValue is an entity relation to process
120
     *
121
     * @param  mixed $propertyValue
122
     * @return bool
123
     */
124 1
    protected function isEntityRelation($propertyValue)
125
    {
126 1
        return (is_object($propertyValue) && !($propertyValue instanceof \DateTime));
127
    }
128
}
129