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

Update::hasNeededTable()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 9.2
cc 4
eloc 5
nc 3
nop 1
crap 4
1
<?php
2
3
namespace Muffin\Queries\Snippets;
4
5
use Muffin\Snippet;
6
7
class Update implements Snippet, NeedTableAware
8
{
9
    private
10
        $tables;
11
12 20
    public function __construct($table = null, $alias = null)
13
    {
14 20
        $this->tables = array();
15
16 20
        if(! empty($table))
17 20
        {
18 7
            $this->addTable($table, $alias);
19 7
        }
20 20
    }
21
22 17
    public function addTable($table, $alias = null)
23
    {
24 17
        if(! $table instanceof TableName)
25 17
        {
26 16
            $table = new TableName($table, $alias);
27 14
        }
28
29 15
        $this->tables[] = $table;
30
31 15
        return $this;
32
    }
33
34 17
    public function toString()
35
    {
36 17
        if(empty($this->tables))
37 17
        {
38 3
            return '';
39
        }
40
41 14
        $tables = array();
42
43 14
        foreach($this->tables as $table)
44
        {
45 14
            $tables[] = $table->toString();
46 14
        }
47
48 14
        $tablesString = implode(', ', array_filter($tables));
49
50 14
        return sprintf('UPDATE %s', $tablesString);
51
    }
52
53 2
    public function hasNeededTable($tableName)
54
    {
55 2
        foreach($this->tables as $table)
56
        {
57 2
            if($table->getName() === $tableName || $table->getAlias() === $tableName)
58 2
            {
59 1
                return true;
60
            }
61 1
        }
62
63 1
        return false;
64
    }
65
}
66