Passed
Pull Request — master (#190)
by Arman
03:14
created

Join   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 97
rs 10
wmc 10

6 Methods

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

36
        $this->/** @scrutinizer ignore-call */ 
37
               getOrmModel()->join($table, $constraint, $tableAlias);
Loading history...
37
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...\Idiorm\Statements\Join which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
38
    }
39
40
    /**
41
     * @inheritDoc
42
     * @throws DatabaseException
43
     */
44
    public function innerJoin(string $table, array $constraint, ?string $tableAlias = null): DbalInterface
45
    {
46
        $this->getOrmModel()->inner_join($table, $constraint, $tableAlias);
47
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...\Idiorm\Statements\Join which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
48
    }
49
50
    /**
51
     * @inheritDoc
52
     * @throws DatabaseException
53
     */
54
    public function leftJoin(string $table, array $constraint, ?string $tableAlias = null): DbalInterface
55
    {
56
        IdiormPatch::getInstance()->use($this->getOrmModel())->leftJoin($table, $constraint, $tableAlias);
57
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...\Idiorm\Statements\Join which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
58
    }
59
60
    /**
61
     * @inheritDoc
62
     * @throws DatabaseException
63
     */
64
    public function rightJoin(string $table, array $constraint, ?string $tableAlias = null): DbalInterface
65
    {
66
        IdiormPatch::getInstance()->use($this->getOrmModel())->rightJoin($table, $constraint, $tableAlias);
67
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...\Idiorm\Statements\Join which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
68
    }
69
70
    /**
71
     * @inheritDoc
72
     * @throws DatabaseException
73
     * @throws ModelException
74
     */
75
    public function joinTo(QtModel $model, bool $switch = true): DbalInterface
76
    {
77
        if (!isset($model->foreignKeys[$this->table])) {
78
            throw ModelException::wrongRelation(get_class($model), $this->table);
79
        }
80
81
        $this->getOrmModel()->join($model->table,
82
            [
83
                $model->table . '.' . $model->foreignKeys[$this->table],
84
                '=',
85
                $this->table . '.' . $this->idColumn
86
            ]
87
        );
88
89
        if ($switch) {
90
            $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...
91
            $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...
92
            $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...
93
        }
94
95
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...\Idiorm\Statements\Join which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
96
    }
97
98
    /**
99
     * @inheritDoc
100
     * @throws DatabaseException
101
     * @throws ModelException
102
     */
103
    public function joinThrough(QtModel $model, bool $switch = true): DbalInterface
104
    {
105
        if (!isset($this->foreignKeys[$model->table])) {
106
            throw ModelException::wrongRelation(get_class($model), $this->table);
107
        }
108
109
        $this->getOrmModel()->join($model->table,
110
            [
111
                $model->table . '.' . $model->idColumn,
112
                '=',
113
                $this->table . '.' . $this->foreignKeys[$model->table]
114
            ]
115
        );
116
117
        if ($switch) {
118
            $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...
119
            $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...
120
            $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...
121
        }
122
123
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...\Statements\Join&object which includes types incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
124
    }
125
}
126