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

Update   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 59
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1
ccs 31
cts 31
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A addTable() 0 11 2
A toString() 0 18 3
A hasNeededTable() 0 12 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