Completed
Pull Request — master (#2)
by René
16:36 queued 06:34
created

SQLCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 10
c 4
b 0
f 0
lcom 1
cbo 1
dl 0
loc 136
ccs 33
cts 33
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A insertInto() 0 11 1
A updateSetWhere() 0 7 1
A selectFrom() 0 6 1
A selectFromWhere() 0 8 1
A getColumnNames() 0 6 1
A getColumnValues() 0 6 1
A getEqualFromColumns() 0 14 3
1
<?php
2
declare(strict_types = 1);
3
4
namespace Zortje\MVC\Model;
5
6
use Zortje\MVC\Model\Table\Entity\Exception\InvalidEntityPropertyException;
7
8
/**
9
 * Class SQLCommand
10
 *
11
 * @package Zortje\MVC\Model
12
 */
13
class SQLCommand
14
{
15
16
    /**
17
     * @var String Table name
18
     */
19
    private $tableName;
20
21
    /**
22
     * @var String[] Table columns
23
     */
24
    private $columns;
25
26
    /**
27 1
     * @param string   $tableName
28 1
     * @param string[] $columns
29
     */
30 1
    public function __construct(string $tableName, array $columns)
31 1
    {
32
        $this->tableName = $tableName;
33 1
        $this->columns   = $columns;
34
    }
35 1
36
    /**
37
     * Create INSERT INTO command
38
     *
39
     * @return string INSERT INTO query
40
     */
41
    public function insertInto(): string
42
    {
43 1
        $columnNames = $this->getColumnNames($this->columns);
44 1
45
        $columns = $this->columns;
46 1
        unset($columns['id']);
47
48
        $columnValues = $this->getColumnValues($columns);
49
50
        return "INSERT INTO `$this->tableName` ($columnNames) VALUES (NULL, $columnValues);";
51
    }
52
53
    /**
54
     * Create UPDATE SET command with WHERE for updating a single row with ID
55
     *
56 2
     * @param array $columns Columns to use in SET condition
57 2
     *
58
     * @return string
59 2
     */
60
    public function updateSetWhere(array $columns): string
61 2
    {
62
        $set   = $this->getEqualFromColumns(', ', $columns);
63
        $where = $this->getEqualFromColumns(' AND ', ['id']);
64
65
        return "UPDATE `$this->tableName` SET $set WHERE $where;";
66
    }
67
68
    /**
69
     * Create SELECT FROM command
70
     *
71 1
     * @return string SELECT FROM query
72 1
     */
73
    public function selectFrom(): string
74 1
    {
75
        $tableColumnNames = $this->getColumnNames($this->columns);
76
77
        return "SELECT $tableColumnNames FROM `$this->tableName`;";
78
    }
79
80
    /**
81
     * Create SELECT FROM command with WHERE
82
     *
83
     * @param string[] $columns Columns to use in WHERE condition
84 1
     *
85 1
     * @return string SELECT FROM query
86
     */
87 1
    public function selectFromWhere($columns): string
88
    {
89
        $tableColumnNames = $this->getColumnNames($this->columns);
90
91
        $where = $this->getEqualFromColumns(' AND ', $columns);
92
93
        return "SELECT $tableColumnNames FROM `$this->tableName` WHERE $where;";
94
    }
95
96
    /**
97 2
     * Get columns names for SQL command
98 2
     *
99
     * @param String[] $columns
100 2
     *
101 1
     * @return string Column names for column list
102 2
     */
103 1
    protected function getColumnNames(array $columns): string
104
    {
105
        $tableColumnNames = implode('`, `', array_keys($columns));
106 1
107 1
        return "`{$tableColumnNames}`";
108 1
    }
109
110 1
    /**
111
     * Get columns values for SQL command
112
     *
113
     * @param String[] $columns
114
     *
115
     * @return string Column names for column values
116
     */
117 1
    protected function getColumnValues(array $columns): string
118 1
    {
119 1
        $tableColumnValues = implode(', :', array_keys($columns));
120 1
121
        return ":{$tableColumnValues}";
122
    }
123
124
    /**
125
     * Get equal string for columns with glue
126
     *
127
     * @param string $glue    String glue between columns
128
     * @param array  $columns Columns to use
129
     *
130
     * @return string
131
     *
132
     * @throws InvalidEntityPropertyException If provided with columns which the entity dosnt have
133
     */
134
    protected function getEqualFromColumns(string $glue, array $columns): string
135
    {
136
        $equal = [];
137
138
        foreach ($columns as $column) {
139
            if (!isset($this->columns[$column])) {
140
                throw new InvalidEntityPropertyException([get_class($this), $column]);
141
            }
142
143
            $equal[] = "`$column` = :$column";
144
        }
145
146
        return implode($glue, $equal);
147
    }
148
}
149