Completed
Pull Request — master (#1324)
by Vincent
06:45 queued 03:26
created

QueryBuilderHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 4
dl 0
loc 62
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addJoinOnce() 0 19 4
A getExistingJoin() 0 17 4
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Util;
15
16
use Doctrine\ORM\Query\Expr\Join;
17
use Doctrine\ORM\QueryBuilder;
18
19
/**
20
 * @author Vincent Chalamon <[email protected]>
21
 *
22
 * @internal
23
 */
24
final class QueryBuilderHelper
25
{
26
    /**
27
     * Adds a join to the queryBuilder if none exists.
28
     *
29
     * @param QueryBuilder                $queryBuilder
30
     * @param QueryNameGeneratorInterface $queryNameGenerator
31
     * @param string                      $alias
32
     * @param string                      $association        the association field
33
     * @param string|null                 $joinType           the join type (left join / inner join)
34
     * @param string|null                 $conditionType
35
     * @param string|null                 $condition
36
     *
37
     * @return string the new association alias
38
     */
39
    public static function addJoinOnce(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $association, ?string $joinType = null, ?string $conditionType = null, ?string $condition = null): string
40
    {
41
        $join = self::getExistingJoin($queryBuilder, $alias, $association);
42
43
        if (null === $join) {
44
            $associationAlias = $queryNameGenerator->generateJoinAlias($association);
45
            $query = sprintf('%s.%s', $alias, $association);
46
47
            if (Join::LEFT_JOIN === $joinType || true === QueryChecker::hasLeftJoin($queryBuilder)) {
48
                $queryBuilder->leftJoin($query, $associationAlias, $conditionType, $condition);
49
            } else {
50
                $queryBuilder->innerJoin($query, $associationAlias, $conditionType, $condition);
51
            }
52
        } else {
53
            $associationAlias = $join->getAlias();
54
        }
55
56
        return $associationAlias;
57
    }
58
59
    /**
60
     * Get the existing join from queryBuilder DQL parts.
61
     *
62
     * @param QueryBuilder $queryBuilder
63
     * @param string       $alias
64
     * @param string       $association  the association field
65
     *
66
     * @return Join|null
67
     */
68
    private static function getExistingJoin(QueryBuilder $queryBuilder, string $alias, string $association)
69
    {
70
        $parts = $queryBuilder->getDQLPart('join');
71
72
        if (!isset($parts['o'])) {
73
            return null;
74
        }
75
76
        foreach ($parts['o'] as $join) {
77
            /** @var Join $join */
78
            if (sprintf('%s.%s', $alias, $association) === $join->getJoin()) {
79
                return $join;
80
            }
81
        }
82
83
        return null;
84
    }
85
}
86