Completed
Pull Request — master (#11)
by Romain
02:27
created

AbstractJoin::hasNeededTable()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.6666
cc 3
eloc 4
nc 2
nop 1
crap 3
1
<?php
2
3
namespace Muffin\Queries\Snippets\Joins;
4
5
use Muffin\Snippet;
6
use Muffin\Queries\Snippets\Join;
7
use Muffin\Queries\Snippets;
8
use Muffin\Queries\Snippets\NeedTableAware;
9
10
abstract class AbstractJoin implements Join, Snippet, NeedTableAware
11
{
12
    private
13
        $table,
14
        $using,
15
        $on;
16
17 25
    public function __construct($table, $alias = null)
18
    {
19 25
        $this->table = new Snippets\TableName($table, $alias);
20 25
        $this->on = array();
21 25
    }
22
23 9
    public function using($column)
24
    {
25 9
        $this->on = array();
26
27 9
        $this->using = new Snippets\Using($column);
28
29 9
        return $this;
30
    }
31
32 18
    public function on($leftColumn, $rightColumn)
33
    {
34 18
        $this->using = null;
35 18
        $this->on[] = new Snippets\On($leftColumn, $rightColumn);
36
37 18
        return $this;
38
    }
39
40 25
    public function toString()
41
    {
42 25
        $joinQueryPart = sprintf(
43 25
            '%s %s',
44 25
            $this->getJoinDeclaration(),
45 25
            $this->table->toString()
46 25
        );
47
48 25
        $joinQueryPart .= $this->buildOnConditionClause();
49 25
        $joinQueryPart .= $this->buildUsingConditionClause();
50
51 25
        return $joinQueryPart;
52
    }
53
54 2
    public function hasNeededTable($tableName)
55
    {
56 2
        if($this->table->getName() === $tableName || $this->table->getAlias() === $tableName)
57 2
        {
58 2
            return true;
59
        }
60
61 1
        return false;
62
    }
63
64
    abstract protected function getJoinDeclaration();
65
66 25
    private function buildUsingConditionClause()
67
    {
68 25
        if(!$this->using instanceof Snippet)
69 25
        {
70 19
            return '';
71
        }
72
73 9
        return ' ' . $this->using->toString();
74
    }
75
76 25
    private function buildOnConditionClause()
77
    {
78 25
        $conditionClause = array();
79
80 25
        foreach($this->on as $on)
81
        {
82 18
            if($on instanceof Snippet)
83 18
            {
84 18
                $conditionClause[] = $on->toString();
85 18
            }
86 25
        }
87
88 25
        return empty($conditionClause) ? '' : ' ' . implode('', $conditionClause);
89
    }
90
}
91