Passed
Pull Request — master (#362)
by Arman
03:01
created

Join   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 137
rs 10
wmc 16

8 Methods

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

37
        $this->/** @scrutinizer ignore-call */ 
38
               getOrmModel()->join($table, $constraint, $tableAlias);
Loading history...
38
        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...
39
    }
40
41
    /**
42
     * @inheritDoc
43
     * @throws BaseException
44
     */
45
    public function innerJoin(string $table, array $constraint, ?string $tableAlias = null): DbalInterface
46
    {
47
        $this->getOrmModel()->inner_join($table, $constraint, $tableAlias);
48
        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...
49
    }
50
51
    /**
52
     * @inheritDoc
53
     * @throws BaseException
54
     */
55
    public function leftJoin(string $table, array $constraint, ?string $tableAlias = null): DbalInterface
56
    {
57
        IdiormPatch::getInstance()->use($this->getOrmModel())->leftJoin($table, $constraint, $tableAlias);
58
        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...
59
    }
60
61
    /**
62
     * @inheritDoc
63
     * @throws BaseException
64
     */
65
    public function rightJoin(string $table, array $constraint, ?string $tableAlias = null): DbalInterface
66
    {
67
        IdiormPatch::getInstance()->use($this->getOrmModel())->rightJoin($table, $constraint, $tableAlias);
68
        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...
69
    }
70
71
    /**
72
     * @inheritDoc
73
     * @throws BaseException
74
     * @throws ModelException
75
     */
76
    public function joinTo(QtModel $relatedModel, bool $switch = true): DbalInterface
77
    {
78
        $relation = $this->getValidatedRelation($relatedModel);
79
80
        switch ($relation['type']) {
81
            case Relation::HAS_ONE:
82
            case Relation::HAS_MANY:
83
                $this->applyHasRelation($relatedModel, $relation);
84
                break;
85
86
            case Relation::BELONGS_TO:
87
                $this->applyBelongsTo($relatedModel, $relation);
88
                break;
89
90
            default:
91
                throw ModelException::unsupportedRelationType($relation['type']);
92
        }
93
94
        if ($switch) {
95
            $this->modelName = get_class($relatedModel);
0 ignored issues
show
Bug Best Practice introduced by
The property modelName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
96
            $this->table = $relatedModel->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...
97
            $this->idColumn = $relatedModel->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...
98
            $this->foreignKeys = $relatedModel->relations();
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...
99
        }
100
101
        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...
102
    }
103
104
    /**
105
     * @param QtModel $relatedModel
106
     * @param array $relation
107
     * @return void
108
     * @throws BaseException
109
     */
110
    protected function applyHasRelation(QtModel $relatedModel, array $relation): void
111
    {
112
        $this->getOrmModel()->join(
113
            $relatedModel->table,
114
            [
115
                $relatedModel->table . '.' . $relation['foreign_key'],
116
                '=',
117
                $this->table . '.' . $relation['local_key']
118
            ]
119
        );
120
    }
121
122
    /**
123
     * @param QtModel $relatedModel
124
     * @param array $relation
125
     * @return void
126
     * @throws BaseException
127
     */
128
    protected function applyBelongsTo(QtModel $relatedModel, array $relation): void
129
    {
130
        $this->getOrmModel()->join(
131
            $relatedModel->table,
132
            [
133
                $relatedModel->table . '.' . $relation['local_key'],
134
                '=',
135
                $this->table . '.' . $relation['foreign_key']
136
            ]
137
        );
138
    }
139
140
    /**
141
     * @param QtModel $modelToJoin
142
     * @return array
143
     * @throws ModelException
144
     */
145
    private function getValidatedRelation(QtModel $modelToJoin): array
146
    {
147
        $relations = $this->getForeignKeys();
0 ignored issues
show
Bug introduced by
It seems like getForeignKeys() 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

147
        /** @scrutinizer ignore-call */ 
148
        $relations = $this->getForeignKeys();
Loading history...
148
        $relatedModelName = get_class($modelToJoin);
149
150
        if (!isset($relations[$relatedModelName])) {
151
            throw ModelException::wrongRelation($this->getModelName(), $relatedModelName);
0 ignored issues
show
Bug introduced by
It seems like getModelName() 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

151
            throw ModelException::wrongRelation($this->/** @scrutinizer ignore-call */ getModelName(), $relatedModelName);
Loading history...
152
        }
153
154
        $relation = $relations[$relatedModelName];
155
156
        if (empty($relation['type'])) {
157
            throw ModelException::relationTypeMissing($this->getModelName(), $relatedModelName);
158
        }
159
160
        if (empty($relation['foreign_key']) || empty($relation['local_key'])) {
161
            throw ModelException::missingRelationKeys($this->getModelName(), $relatedModelName);
162
        }
163
164
        return $relation;
165
    }
166
}