Completed
Pull Request — master (#2)
by René
04:42
created

SQLCommand::getWhereConditionFromKeys()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 3
cts 3
cp 1
rs 9.2
cc 4
eloc 9
nc 5
nop 1
crap 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A SQLCommand::getColumnNames() 0 6 1
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
     * @param string   $tableName
28
     * @param string[] $columns
29
     */
30 1
    public function __construct(string $tableName, array $columns)
31
    {
32 1
        $this->tableName = $tableName;
33 1
        $this->columns   = $columns;
34 1
    }
35
36
    /**
37
     * Create INSERT INTO command
38
     *
39
     * @return string INSERT INTO query
40
     */
41 1
    public function insertInto(): string
42
    {
43 1
        $columnNames = $this->getColumnNames($this->columns);
44
45 1
        $columns = $this->columns;
46 1
        unset($columns['id']);
47
48 1
        $columnValues = $this->getColumnValues($columns);
49
50 1
        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
     * @param array $columns Columns to use in SET condition
57
     *
58
     * @return string
59
     */
60 1
    public function updateSetWhere(array $columns): string
61
    {
62 1
        $set   = $this->getEqualFromColumns(', ', $columns);
63 1
        $where = $this->getEqualFromColumns(' AND ', ['id']);
64
65 1
        return "UPDATE `$this->tableName` SET $set WHERE $where;";
66
    }
67
68
    /**
69
     * Create SELECT FROM command
70
     *
71
     * @return string SELECT FROM query
72
     */
73 1
    public function selectFrom(): string
74
    {
75 1
        $tableColumnNames = $this->getColumnNames($this->columns);
76
77 1
        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
     *
85
     * @return string SELECT FROM query
86
     */
87 1
    public function selectFromWhere($columns): string
88
    {
89 1
        $tableColumnNames = $this->getColumnNames($this->columns);
90
91 1
        $where = $this->getEqualFromColumns(' AND ', $columns);
92
93 1
        return "SELECT $tableColumnNames FROM `$this->tableName` WHERE $where;";
94
    }
95
96
    /**
97
     * Get columns names for SQL command
98
     *
99
     * @param String[] $columns
100
     *
101
     * @return string Column names for column list
102
     */
103 1
    protected function getColumnNames(array $columns): string
104
    {
105 1
        $tableColumnNames = implode('`, `', array_keys($columns));
106
107 1
        return "`{$tableColumnNames}`";
108
    }
109
110
    /**
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
    {
119 1
        $tableColumnValues = implode(', :', array_keys($columns));
120
121 1
        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 1
    protected function getEqualFromColumns(string $glue, array $columns): string
135
    {
136 1
        $equal = [];
137
138 1
        foreach ($columns as $column) {
139 1
            if (!isset($this->columns[$column])) {
140
                throw new InvalidEntityPropertyException([get_class($this), $column]);
141
            }
142
143 1
            $equal[] = "`$column` = :$column";
144
        }
145
146 1
        return implode($glue, $equal);
147
    }
148
}
149