Passed
Pull Request — master (#24)
by Iakov
03:34
created

BuildSelectQueryStep::addJoinIfRelation()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4.944

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 2
dl 0
loc 6
ccs 2
cts 5
cp 0.4
crap 4.944
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Kami\ApiCoreBundle\RequestProcessor\Step\Common;
4
5
6
use Doctrine\Common\Annotations\Reader;
7
use Doctrine\Common\Util\Inflector;
8
use Doctrine\ORM\Mapping\ManyToMany;
9
use Doctrine\ORM\Mapping\ManyToOne;
10
use Doctrine\ORM\Mapping\OneToMany;
11
use Doctrine\ORM\Mapping\OneToOne;
12
use Doctrine\ORM\QueryBuilder;
13
use Kami\ApiCoreBundle\Annotation\Relation;
14
use Kami\ApiCoreBundle\RequestProcessor\ProcessingException;
15
use Kami\ApiCoreBundle\RequestProcessor\Step\AbstractStep;
16
use Kami\ApiCoreBundle\Security\AccessManager;
17
18
class BuildSelectQueryStep extends AbstractStep
19
{
20
    /**
21
     * @var AccessManager
22
     */
23
    private $accessManager;
24
25
    /**
26
     * @var Reader
27
     */
28
    private $reader;
29
30
    /**
31
     * @var array
32
     */
33
    protected $aliases = [];
34
35
    private $accessible = [];
0 ignored issues
show
introduced by
The private property $accessible is not used, and could be removed.
Loading history...
36
37 3
    public function __construct(AccessManager $accessManager, Reader $reader)
38
    {
39 3
        $this->accessManager = $accessManager;
40 3
        $this->reader = $reader;
41 3
    }
42
43 1
    public function execute()
44
    {
45
        /** @var \ReflectionClass $reflection */
46 1
        $reflection = $this->getFromResponse('reflection');
47
        /** @var QueryBuilder $queryBuilder */
48 1
        $queryBuilder = $this->getFromResponse('query_builder');
49 1
        $queryBuilder->from($reflection->getName(), 'e');
50 1
        $queryBuilder->addSelect('e');
51
52 1
        foreach ($reflection->getProperties() as $property) {
53 1
            $this->addJoinIfRelation($property, $queryBuilder);
54
        }
55
56 1
        return $this->createResponse(['query_builder' => $queryBuilder]);
57
    }
58
59
60 1
    public function requiresBefore()
61
    {
62
        return [
63 1
            GetQueryBuilderStep::class
64
        ];
65
    }
66
67
    /**
68
     * @param $property
69
     * @param $queryBuilder
70
     */
71 1
    protected function addJoinIfRelation(\ReflectionProperty $property, QueryBuilder $queryBuilder)
72
    {
73 1
        if ($this->isRelation($property) && $this->accessManager->canAccessProperty($property)) {
74
            $alias = Inflector::tableize($property->getName());
75
            $queryBuilder->leftJoin(sprintf('e.%s', $property->getName()), $alias);
76
            $queryBuilder->addSelect($alias);
77
        }
78
79 1
    }
80
81 1
    protected function isRelation(\ReflectionProperty $property)
82
    {
83 1
        return !empty($this->reader->getPropertyAnnotation($property, Relation::class));
84
    }
85
}