Completed
Pull Request — master (#1)
by Romain
02:07
created

AbstractJoin   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 83
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A using() 0 8 1
A on() 0 7 1
A toString() 0 13 1
A hasNeededTable() 0 8 3
getJoinDeclaration() 0 1 ?
A buildUsingConditionClause() 0 9 2
A buildOnConditionClause() 0 14 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Puzzle\QueryBuilder\Queries\Snippets\Joins;
6
7
use Puzzle\QueryBuilder\Snippet;
8
use Puzzle\QueryBuilder\Queries\Snippets\Join;
9
use Puzzle\QueryBuilder\Queries\Snippets;
10
use Puzzle\QueryBuilder\Queries\Snippets\NeedTableAware;
11
12
abstract class AbstractJoin implements Join, Snippet, NeedTableAware
13
{
14
    private
15
        $table,
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
16
        $using,
17
        $on;
18
19 25
    public function __construct(string $table, ?string $alias = null)
20
    {
21 25
        $this->table = new Snippets\TableName($table, $alias);
22 25
        $this->on = [];
23 25
    }
24
25
    /**
26
     * @param array[string]|string $column
0 ignored issues
show
Documentation introduced by
The doc-type array[string]|string could not be parsed: Expected "]" at position 2, but found "string". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
27
     */
28 9
    public function using($column): self
29
    {
30 9
        $this->on = [];
31
32 9
        $this->using = new Snippets\Using($column);
33
34 9
        return $this;
35
    }
36
37 18
    public function on(?string $leftColumn, ?string $rightColumn): self
38
    {
39 18
        $this->using = null;
40 18
        $this->on[] = new Snippets\On($leftColumn, $rightColumn);
41
42 18
        return $this;
43
    }
44
45 25
    public function toString(): string
46
    {
47 25
        $joinQueryPart = sprintf(
48 25
            '%s %s',
49 25
            $this->getJoinDeclaration(),
50 25
            $this->table->toString()
51
        );
52
53 25
        $joinQueryPart .= $this->buildOnConditionClause();
54 25
        $joinQueryPart .= $this->buildUsingConditionClause();
55
56 25
        return $joinQueryPart;
57
    }
58
59 2
    public function hasNeededTable(string $tableName): bool
60
    {
61 2
        if($this->table->getName() === $tableName || $this->table->getAlias() === $tableName)
62
        {
63 2
            return true;
64
        }
65 1
        return false;
66
    }
67
68
    abstract protected function getJoinDeclaration(): string;
69
70 25
    private function buildUsingConditionClause(): string
71
    {
72 25
        if(!$this->using instanceof Snippet)
73
        {
74 19
            return '';
75
        }
76
77 9
        return ' ' . $this->using->toString();
78
    }
79
80 25
    private function buildOnConditionClause(): string
81
    {
82 25
        $conditionClause = array();
83
84 25
        foreach($this->on as $on)
85
        {
86 18
            if($on instanceof Snippet)
87
            {
88 18
                $conditionClause[] = $on->toString();
89
            }
90
        }
91
92 25
        return empty($conditionClause) ? '' : ' ' . implode('', $conditionClause);
93
    }
94
}
95