Completed
Pull Request — master (#1)
by Romain
02:07
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
wmc 11
lcom 1
cbo 1
dl 0
loc 59
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

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
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