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

AbstractJoin   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

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

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 9 3
getJoinDeclaration() 0 1 ?
A buildUsingConditionClause() 0 9 2
A buildOnConditionClause() 0 14 4
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