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

AbstractJoin::hasNeededTable()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
crap 3
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