Completed
Push — master ( e43f1a...57db71 )
by Arman
26s queued 12s
created

Join   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 25
c 1
b 0
f 0
dl 0
loc 84
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A leftJoin() 0 4 1
A joinTo() 0 17 2
A rightJoin() 0 4 1
A join() 0 3 1
A innerJoin() 0 3 1
A joinThrough() 0 17 2
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.4.0
13
 */
14
namespace Quantum\Libraries\Database\Statements;
15
16
use Quantum\Libraries\Database\IdiormPatch;
17
use Quantum\Mvc\QtModel;
18
19
/**
20
 * Trait Join
21
 * @package Quantum\Libraries\Database\Statements
22
 */
23
Trait Join
24
{
25
    /**
26
     * Adds a simple JOIN source to the query
27
     * @inheritDoc
28
     */
29
    public function join(string $table, array $constraint, string $tableAlias = null): object
30
    {
31
        return $this->ormObject->join($table, $constraint, $tableAlias);
32
    }
33
34
    /**
35
     * Adds an INNER JOIN source to the query
36
     * @inheritDoc
37
     */
38
    public function innerJoin(string $table, array $constraint, ?string $tableAlias = null): object
39
    {
40
        return $this->ormObject->inner_join($table, $constraint, $tableAlias);
41
    }
42
43
    /**
44
     * Adds an LEFT JOIN source to the query
45
     * @inheritDoc
46
     */
47
    public function leftJoin(string $table, array $constraint, ?string $tableAlias = null): object
48
    {
49
        $this->ormPatch = IdiormPatch::getInstance()->setOrmObject($this->ormObject);
0 ignored issues
show
Bug Best Practice introduced by
The property ormPatch does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
50
        return $this->ormPatch->left_join($table, $constraint, $tableAlias);
51
    }
52
53
    /**
54
     * Adds an RIGHT JOIN source to the query
55
     * @inheritDoc
56
     */
57
    public function rightJoin(string $table, array $constraint, ?string $tableAlias = null): object
58
    {
59
        $this->ormPatch = IdiormPatch::getInstance()->setOrmObject($this->ormObject);
0 ignored issues
show
Bug Best Practice introduced by
The property ormPatch does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
60
        return $this->ormPatch->right_join($table, $constraint, $tableAlias);
61
    }
62
63
    /**
64
     * Joins two models
65
     * @inheritDoc
66
     */
67
    public function joinTo(QtModel $model, bool $switch = true): object
68
    {
69
        $resultObject = $this->ormObject->join($model->table,
70
            [
71
                $model->table . '.' . $model->foreignKeys[$this->table],
72
                '=',
73
                $this->table . '.' . $this->idColumn
74
            ]
75
        );
76
77
        if ($switch) {
78
            $this->table = $model->table;
0 ignored issues
show
Bug Best Practice introduced by
The property table does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
79
            $this->idColumn = $model->idColumn;
0 ignored issues
show
Bug Best Practice introduced by
The property idColumn does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
80
            $this->foreignKeys = $model->foreignKeys;
0 ignored issues
show
Bug Best Practice introduced by
The property foreignKeys does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
81
        }
82
83
        return $resultObject;
84
    }
85
86
    /**
87
     * Joins through connector model
88
     * @inheritDoc
89
     */
90
    public function joinThrough(QtModel $model, bool $switch = true): object
91
    {
92
        $resultObject = $this->ormObject->join($model->table,
93
            [
94
                $model->table . '.' . $model->idColumn,
95
                '=',
96
                $this->table . '.' . $this->foreignKeys[$model->table]
97
            ]
98
        );
99
100
        if ($switch) {
101
            $this->table = $model->table;
0 ignored issues
show
Bug Best Practice introduced by
The property table does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
102
            $this->idColumn = $model->idColumn;
0 ignored issues
show
Bug Best Practice introduced by
The property idColumn does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
103
            $this->foreignKeys = $model->foreignKeys;
0 ignored issues
show
Bug Best Practice introduced by
The property foreignKeys does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
104
        }
105
106
        return $resultObject;
107
    }
108
109
}