Completed
Pull Request — master (#83)
by Krzysztof
02:54
created

AbstractORMCriteriaBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 75
ccs 19
cts 19
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsSearchingContext() 0 5 1
A join() 0 17 2
B filterExistingJoins() 0 22 4
1
<?php
2
3
namespace KGzocha\Searcher\CriteriaBuilder\Doctrine;
4
5
use Doctrine\ORM\Query\Expr\Join;
6
use Doctrine\ORM\QueryBuilder;
7
use KGzocha\Searcher\Context\Doctrine\QueryBuilderSearchingContext;
8
use KGzocha\Searcher\Context\SearchingContextInterface;
9
use KGzocha\Searcher\CriteriaBuilder\CriteriaBuilderInterface;
10
11
/**
12
 * Abstract CriteriaBuilder that can be used in builders that supports
13
 * only ODMBuilderSearchingContext.
14
 * Extra feature is join() method which will add another join only
15
 * if there is not such join already.
16
 *
17
 *@author Krzysztof Gzocha <[email protected]>
18
 */
19
abstract class AbstractORMCriteriaBuilder implements
20
    CriteriaBuilderInterface
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 2
    public function supportsSearchingContext(
26
        SearchingContextInterface $searchingContext
27
    ) {
28 2
        return $searchingContext instanceof QueryBuilderSearchingContext;
29
    }
30
31
    /**
32
     * Will do JOIN only if there is no such join already.
33
     * For any other more advanced join strategies please use unique aliases.
34
     * Remember: for performance reasons you should keep number of joins as low as possible
35
     * Example usage: $this->join($qb, 'p.house', 'h', Join::LEFT_JOIN).
36
     *
37
     * @param QueryBuilder $queryBuilder
38
     * @param string       $join
39
     * @param string       $alias
40
     * @param string       $joinType
41
     *
42
     * @return QueryBuilder
43
     */
44 2
    protected function join(QueryBuilder $queryBuilder, $join, $alias, $joinType)
45
    {
46 2
        list($entity) = explode('.', $join);
47
48 2
        $joinParts = $queryBuilder->getDQLPart('join');
49 2
        if (!array_key_exists($entity, $joinParts)) {
50 1
            return $queryBuilder->join($join, $alias);
51
        }
52
53 1
        return $this->filterExistingJoins(
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->filterExistingJoi...ias, $join, $joinType); of type Doctrine\ORM\QueryBuilde...tractORMCriteriaBuilder adds the type KGzocha\Searcher\Criteri...tractORMCriteriaBuilder to the return on line 53 which is incompatible with the return type documented by KGzocha\Searcher\Criteri...RMCriteriaBuilder::join of type Doctrine\ORM\QueryBuilder.
Loading history...
54
            $queryBuilder,
55 1
            $joinParts[$entity],
56
            $alias,
57
            $join,
58
            $joinType
59
        );
60
    }
61
62
    /**
63
     * @param QueryBuilder $queryBuilder
64
     * @param array        $joinParts
65
     * @param string       $alias
66
     * @param string       $join
67
     * @param string       $joinType
68
     *
69
     * @return QueryBuilder|static
70
     */
71 7
    protected function filterExistingJoins(
72
        QueryBuilder $queryBuilder,
73
        $joinParts,
74
        $alias,
75
        $join,
76
        $joinType
77
    ) {
78 7
        $existingJoin = array_filter(
79
            $joinParts,
80 7
            function(Join $joinObj) use ($alias, $join, $joinType) {
81 7
                return $joinObj->getJoinType() == $joinType
82 7
                    && $joinObj->getAlias() == $alias
83 7
                    && $joinObj->getJoin() == $join;
84 7
            }
85
        );
86
87 7
        if ([] != $existingJoin) {
88 2
            return $queryBuilder;
89
        }
90
91 5
        return $queryBuilder->join($join, $alias);
92
    }
93
}
94