Passed
Pull Request — master (#48)
by Arman
03:56
created

Join   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 77
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A leftJoin() 0 3 1
A join() 0 3 1
A joinThrough() 0 17 2
A innerJoin() 0 3 1
A rightJoin() 0 3 1
A joinTo() 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.6.0
13
 */
14
15
namespace Quantum\Libraries\Database\Idiorm\Statements;
16
17
use Quantum\Libraries\Database\Idiorm\IdiormPatch;
18
use Quantum\Mvc\QtModel;
19
20
/**
21
 * Trait Join
22
 * @package Quantum\Libraries\Database\Idiorm\Statements
23
 */
24
trait Join
25
{
26
27
    /**
28
     * @inheritDoc
29
     */
30
    public function join(string $table, array $constraint, string $tableAlias = null): object
31
    {
32
        return $this->getOrmModel()->join($table, $constraint, $tableAlias);
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

32
        return $this->/** @scrutinizer ignore-call */ getOrmModel()->join($table, $constraint, $tableAlias);
Loading history...
33
    }
34
35
    /**
36
     * @inheritDoc
37
     */
38
    public function innerJoin(string $table, array $constraint, ?string $tableAlias = null): object
39
    {
40
        return $this->getOrmModel()->inner_join($table, $constraint, $tableAlias);
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public function leftJoin(string $table, array $constraint, ?string $tableAlias = null): object
47
    {
48
        return IdiormPatch::getInstance()->use($this->getOrmModel())->leftJoin($table, $constraint, $tableAlias);
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public function rightJoin(string $table, array $constraint, ?string $tableAlias = null): object
55
    {
56
        return IdiormPatch::getInstance()->use($this->getOrmModel())->rightJoin($table, $constraint, $tableAlias);
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public function joinTo(QtModel $model, bool $switch = true): object
63
    {
64
        $resultObject = $this->getOrmModel()->join($model->table,
65
            [
66
                $model->table . '.' . $model->foreignKeys[$this->table],
67
                '=',
68
                $this->table . '.' . $this->idColumn
69
            ]
70
        );
71
72
        if ($switch) {
73
            $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...
74
            $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...
75
            $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...
76
        }
77
78
        return $resultObject;
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function joinThrough(QtModel $model, bool $switch = true): object
85
    {
86
        $resultObject = $this->getOrmModel()->join($model->table,
87
            [
88
                $model->table . '.' . $model->idColumn,
89
                '=',
90
                $this->table . '.' . $this->foreignKeys[$model->table]
91
            ]
92
        );
93
94
        if ($switch) {
95
            $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...
96
            $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...
97
            $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...
98
        }
99
100
        return $resultObject;
101
    }
102
103
}