Join::applyJoin()   B
last analyzed

Complexity

Conditions 9
Paths 2

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 27
c 0
b 0
f 0
nc 2
nop 4
dl 0
loc 46
rs 8.0555
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.6
13
 */
14
15
namespace Quantum\Libraries\Database\Adapters\Sleekdb\Statements;
16
17
use Quantum\Libraries\Database\Adapters\Sleekdb\SleekDbal;
18
use Quantum\Libraries\Database\Contracts\DbalInterface;
19
use Quantum\Model\Exceptions\ModelException;
20
use Quantum\Model\QtModel;
21
use SleekDB\QueryBuilder;
22
23
/**
24
 * Trait Join
25
 * @package Quantum\Libraries\Database
26
 */
27
trait Join
28
{
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public function joinTo(QtModel $model, bool $switch = true): DbalInterface
34
    {
35
        $this->addJoin(__FUNCTION__, $model, $switch);
36
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...Sleekdb\Statements\Join which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42
    public function joinThrough(QtModel $model, bool $switch = true): DbalInterface
43
    {
44
        $this->addJoin(__FUNCTION__, $model, $switch);
45
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...Sleekdb\Statements\Join which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
46
    }
47
48
    /**
49
     * Adds join
50
     * @param string $type
51
     * @param QtModel $model
52
     * @param bool $switch
53
     */
54
    private function addJoin(string $type, QtModel $model, bool $switch = true)
55
    {
56
        $this->joins[] = [
0 ignored issues
show
Bug Best Practice introduced by
The property joins does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
57
            'type' => $type,
58
            'model' => serialize($model),
59
            'switch' => $switch,
60
        ];
61
    }
62
63
    /**
64
     * Starts to apply joins
65
     * @throws ModelException
66
     */
67
    private function applyJoins()
68
    {
69
        if (isset($this->joins[0])) {
70
            $this->applyJoin($this->queryBuilder, $this, $this->joins[0]);
71
        }
72
    }
73
74
    /**
75
     * Apply the join to query builder
76
     * @param QueryBuilder $queryBuilder
77
     * @param SleekDbal $currentItem
78
     * @param array $nextItem
79
     * @param int $level
80
     * @return QueryBuilder
81
     * @throws ModelException
82
     */
83
    private function applyJoin(QueryBuilder $queryBuilder, SleekDbal $currentItem, array $nextItem, int $level = 1): QueryBuilder
84
    {
85
        $modelToJoin = unserialize($nextItem['model']);
86
        $switch = $nextItem['switch'];
87
        $joinType = $nextItem['type'];
88
89
        $queryBuilder->join(function ($item) use ($currentItem, $modelToJoin, $switch, $joinType, $level) {
90
91
            $newQueryBuilder = (new self($modelToJoin->table))->getOrmModel()->createQueryBuilder();
0 ignored issues
show
Bug introduced by
It seems like getOrmModel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
            $newQueryBuilder = (new self($modelToJoin->table))->/** @scrutinizer ignore-call */ getOrmModel()->createQueryBuilder();
Loading history...
Unused Code introduced by
The call to Quantum\Libraries\Databa...nts\Join::__construct() has too many arguments starting with $modelToJoin->table. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
            $newQueryBuilder = (/** @scrutinizer ignore-call */ new self($modelToJoin->table))->getOrmModel()->createQueryBuilder();

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
92
93
            if ($joinType == self::JOINTO) {
0 ignored issues
show
Bug introduced by
The constant Quantum\Libraries\Databa...Statements\Join::JOINTO was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
94
                if (!isset($modelToJoin->foreignKeys[$currentItem->table])) {
0 ignored issues
show
Bug Best Practice introduced by
The property $table is declared private in Quantum\Libraries\Databa...pters\Sleekdb\SleekDbal. Since you implement __get, consider adding a @property or @property-read.
Loading history...
95
                    throw ModelException::wrongRelation(get_class($modelToJoin), $currentItem->table);
96
                }
97
98
                $newQueryBuilder->where([
99
                    $modelToJoin->foreignKeys[$currentItem->table],
100
                    '=',
101
                    $item[$currentItem->idColumn]
0 ignored issues
show
Bug Best Practice introduced by
The property $idColumn is declared private in Quantum\Libraries\Databa...pters\Sleekdb\SleekDbal. Since you implement __get, consider adding a @property or @property-read.
Loading history...
102
                ]);
103
            } else if ($joinType == self::JOINTHROUGH) {
0 ignored issues
show
Bug introduced by
The constant Quantum\Libraries\Databa...ments\Join::JOINTHROUGH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
104
                if (!isset($currentItem->foreignKeys[$modelToJoin->table])) {
0 ignored issues
show
Bug Best Practice introduced by
The property $foreignKeys is declared private in Quantum\Libraries\Databa...pters\Sleekdb\SleekDbal. Since you implement __get, consider adding a @property or @property-read.
Loading history...
105
                    throw ModelException::wrongRelation(get_class($modelToJoin), $currentItem->table);
106
                }
107
108
                $newQueryBuilder->where([
109
                    $modelToJoin->idColumn,
110
                    '=',
111
                    $item[$currentItem->foreignKeys[$modelToJoin->table]]
112
                ]);
113
            }
114
115
            if ($switch && isset($this->joins[$level])) {
116
                $sleekModel = new self($modelToJoin->table, $modelToJoin->idColumn, $modelToJoin->foreignKeys);
117
                $this->applyJoin($newQueryBuilder, $sleekModel, $this->joins[$level], ++$level);
118
            }
119
120
            return $newQueryBuilder;
121
122
        }, $modelToJoin->table);
123
124
        if (!$switch && isset($this->joins[$level])) {
125
            $this->applyJoin($queryBuilder, $currentItem, $this->joins[$level], ++$level);
126
        }
127
128
        return $queryBuilder;
129
    }
130
}
131