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

Update::hasNeededTable()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

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