Passed
Pull Request — master (#51)
by Arman
05:24 queued 02:50
created

Join::joinTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
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.6.0
13
 */
14
15
namespace Quantum\Libraries\Database\Sleekdb\Statements;
16
17
use Quantum\Libraries\Database\Sleekdb\SleekDbal;
18
use Quantum\Libraries\Database\DbalInterface;
19
use SleekDB\QueryBuilder;
20
use Quantum\Mvc\QtModel;
21
22
/**
23
 * Trait Join
24
 * @package Quantum\Libraries\Database\Idiorm\Statements
25
 */
26
trait Join
27
{
28
29
    /**
30
     * @inheritDoc
31
     */
32
    public function joinTo(QtModel $model, bool $switch = true): DbalInterface
33
    {
34
        $this->addJoin(__FUNCTION__, $model, $switch);
35
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Database\Sleekdb\Statements\Join which is incompatible with the type-hinted return Quantum\Libraries\Database\DbalInterface.
Loading history...
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function joinThrough(QtModel $model, bool $switch = true): DbalInterface
42
    {
43
        $this->addJoin(__FUNCTION__, $model, $switch);
44
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Database\Sleekdb\Statements\Join which is incompatible with the type-hinted return Quantum\Libraries\Database\DbalInterface.
Loading history...
45
    }
46
47
    /**
48
     * Adds join
49
     * @param string $type
50
     * @param \Quantum\Mvc\QtModel $model
51
     * @param bool $switch
52
     */
53
    private function addJoin(string $type, QtModel $model, bool $switch = true)
54
    {
55
        $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...
56
            'type' => $type,
57
            'model' => serialize($model),
58
            'switch' => $switch,
59
        ];
60
    }
61
62
    /**
63
     * Starts to apply joins
64
     */
65
    private function applyJoins()
66
    {
67
        if (isset($this->joins[0])) {
68
            $this->applyJoin($this->queryBuilder, $this, $this->joins[0]);
69
        }
70
    }
71
72
    /**
73
     * Apply the join to query builder
74
     * @param \SleekDB\QueryBuilder $queryBuilder
75
     * @param \Quantum\Libraries\Database\Sleekdb\SleekDbal $currentItem
76
     * @param array $nextItem
77
     * @param int $level
78
     * @return \SleekDB\QueryBuilder
79
     */
80
    private function applyJoin(QueryBuilder $queryBuilder, SleekDbal $currentItem, array $nextItem, int $level = 1): QueryBuilder
81
    {
82
        $modelToJoin = unserialize($nextItem['model']);
83
        $switch = $nextItem['switch'];
84
        $joinType = $nextItem['type'];
85
86
        $queryBuilder->join(function ($item) use ($currentItem, $modelToJoin, $switch, $joinType, $level) {
87
88
            $newQueryBuilder = (new self($modelToJoin->table))->getOrmModel()->createQueryBuilder();
0 ignored issues
show
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

88
            $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...
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

88
            $newQueryBuilder = (new self($modelToJoin->table))->/** @scrutinizer ignore-call */ getOrmModel()->createQueryBuilder();
Loading history...
89
90
            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...
91
                $newQueryBuilder->where([
92
                    $modelToJoin->foreignKeys[$currentItem->table],
0 ignored issues
show
Bug introduced by
The property table is declared private in Quantum\Libraries\Database\Sleekdb\SleekDbal and cannot be accessed from this context.
Loading history...
93
                    '=',
94
                    $item[$currentItem->idColumn]
0 ignored issues
show
Bug introduced by
The property idColumn is declared private in Quantum\Libraries\Database\Sleekdb\SleekDbal and cannot be accessed from this context.
Loading history...
95
                ]);
96
            } 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...
97
                $newQueryBuilder->where([
98
                    $modelToJoin->idColumn,
99
                    '=',
100
                    $item[$currentItem->foreignKeys[$modelToJoin->table]]
0 ignored issues
show
Bug introduced by
The property foreignKeys is declared private in Quantum\Libraries\Database\Sleekdb\SleekDbal and cannot be accessed from this context.
Loading history...
101
                ]);
102
            }
103
104
            if ($switch && isset($this->joins[$level])) {
105
                $sleekModel = new self($modelToJoin->table, $modelToJoin->idColumn, $modelToJoin->foreignKeys);
106
                $this->applyJoin($newQueryBuilder, $sleekModel, $this->joins[$level], ++$level);
107
            }
108
109
            return $newQueryBuilder;
110
111
        }, $modelToJoin->table);
112
113
        if (!$switch && isset($this->joins[$level])) {
114
            $this->applyJoin($queryBuilder, $currentItem, $this->joins[$level], ++$level);
115
        }
116
117
        return $queryBuilder;
118
    }
119
}