Passed
Push — master ( cbe288...130872 )
by Peter
02:31
created

getColumnNamesToValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Orm\DataMappers;
6
7
use AbterPhp\Website\Domain\Entities\PageCategory as Entity;
8
use Opulence\Orm\DataMappers\SqlDataMapper;
9
use Opulence\QueryBuilders\MySql\QueryBuilder;
10
use Opulence\QueryBuilders\MySql\SelectQuery;
11
12
class PageCategorySqlDataMapper extends SqlDataMapper implements IPageCategoryDataMapper
13
{
14
    /**
15
     * @param Entity $entity
16
     */
17
    public function add($entity)
18
    {
19
        if (!($entity instanceof Entity)) {
0 ignored issues
show
introduced by
$entity is always a sub-type of AbterPhp\Website\Domain\Entities\PageCategory.
Loading history...
20
            throw new \InvalidArgumentException(__CLASS__ . ':' . __FUNCTION__ . ' expects a Page Category entity.');
21
        }
22
23
        $data  = $this->getColumnNamesToValues($entity, true);
24
        $query = (new QueryBuilder())->insert('page_categories', $data);
25
26
        $sql    = $query->getSql();
27
        $params = $query->getParameters();
28
29
        $statement = $this->writeConnection->prepare($sql);
30
        $statement->bindValues($params);
31
        $statement->execute();
32
    }
33
34
    /**
35
     * @param Entity $entity
36
     */
37
    public function delete($entity)
38
    {
39
        if (!($entity instanceof Entity)) {
0 ignored issues
show
introduced by
$entity is always a sub-type of AbterPhp\Website\Domain\Entities\PageCategory.
Loading history...
40
            throw new \InvalidArgumentException(__CLASS__ . ':' . __FUNCTION__ . ' expects a Page Category entity.');
41
        }
42
43
        $query = (new QueryBuilder())
44
            ->update('page_categories', 'page_categories', ['deleted' => [1, \PDO::PARAM_INT]])
45
            ->where('id = ?')
46
            ->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR);
47
48
        $sql    = $query->getSql();
49
        $params = $query->getParameters();
50
51
        $statement = $this->writeConnection->prepare($sql);
52
        $statement->bindValues($params);
53
        $statement->execute();
54
    }
55
56
    /**
57
     * @return Entity[]
58
     */
59
    public function getAll(): array
60
    {
61
        $query = $this->getBaseQuery();
62
63
        return $this->read($query->getSql(), [], self::VALUE_TYPE_ARRAY);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->read($quer...self::VALUE_TYPE_ARRAY) could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
64
    }
65
66
    /**
67
     * @param int      $limitFrom
68
     * @param int      $pageSize
69
     * @param string[] $orders
70
     * @param array    $conditions
71
     * @param array    $params
72
     *
73
     * @return Entity[]
74
     */
75
    public function getPage(int $limitFrom, int $pageSize, array $orders, array $conditions, array $params): array
76
    {
77
        $query = $this->getBaseQuery()
78
            ->limit($pageSize)
79
            ->offset($limitFrom);
80
81
        foreach ($orders as $order) {
82
            $query->addOrderBy($order);
83
        }
84
85
        foreach ($conditions as $condition) {
86
            $query->andWhere($condition);
87
        }
88
89
        $replaceCount = 1;
90
91
        $sql = $query->getSql();
92
        $sql = str_replace('SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $sql, $replaceCount);
93
94
        return $this->read($sql, $params, self::VALUE_TYPE_ARRAY);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->read($sql,...self::VALUE_TYPE_ARRAY) could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
95
    }
96
97
    /**
98
     * @param int|string $id
99
     *
100
     * @return Entity|null
101
     */
102
    public function getById($id)
103
    {
104
        $query = $this->getBaseQuery()->andWhere('page_categories.id = :category_id');
105
106
        $sql    = $query->getSql();
107
        $params = [
108
            'category_id' => [$id, \PDO::PARAM_STR],
109
        ];
110
111
112
        return $this->read($sql, $params, self::VALUE_TYPE_ENTITY, true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->read($sql,...ALUE_TYPE_ENTITY, true) also could return the type array which is incompatible with the documented return type AbterPhp\Website\Domain\Entities\PageCategory|null.
Loading history...
113
    }
114
115
    /**
116
     * @param string $identifier
117
     *
118
     * @return Entity|null
119
     * @throws \Opulence\Orm\OrmException
120
     */
121
    public function getByIdentifier(string $identifier): ?Entity
122
    {
123
        $query = $this->getBaseQuery()->andWhere('identifier = :identifier');
124
125
        $sql    = $query->getSql();
126
        $params = [
127
            'identifier' => $identifier,
128
        ];
129
130
        return $this->read($sql, $params, self::VALUE_TYPE_ENTITY, true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->read($sql,...ALUE_TYPE_ENTITY, true) could return the type array which is incompatible with the type-hinted return AbterPhp\Website\Domain\Entities\PageCategory|null. Consider adding an additional type-check to rule them out.
Loading history...
131
    }
132
133
    /**
134
     * @param Entity $entity
135
     */
136
    public function update($entity)
137
    {
138
        if (!($entity instanceof Entity)) {
0 ignored issues
show
introduced by
$entity is always a sub-type of AbterPhp\Website\Domain\Entities\PageCategory.
Loading history...
139
            throw new \InvalidArgumentException(__CLASS__ . ':' . __FUNCTION__ . ' expects a Page Category entity.');
140
        }
141
142
        $columnNamesToValues = $this->getColumnNamesToValues($entity, false);
143
144
        $query = (new QueryBuilder())
145
            ->update('page_categories', 'page_categories', $columnNamesToValues)
146
            ->where('id = ?')
147
            ->andWhere('deleted = 0')
148
            ->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR);
149
150
        $sql    = $query->getSql();
151
        $params = $query->getParameters();
152
153
        $statement = $this->writeConnection->prepare($sql);
154
        $statement->bindValues($params);
155
        $statement->execute();
156
    }
157
158
    /**
159
     * @param Entity $entity
160
     * @param bool   $create
161
     *
162
     * @return array
163
     */
164
    protected function getColumnNamesToValues(Entity $entity, bool $create): array
165
    {
166
        $columnNamesToValues = [
167
            'name'       => [$entity->getName(), \PDO::PARAM_STR],
168
            'identifier' => [$entity->getIdentifier(), \PDO::PARAM_STR],
169
        ];
170
171
        if ($create) {
172
            $columnNamesToValues = array_merge(['id' => [$entity->getId(), \PDO::PARAM_STR]], $columnNamesToValues);
173
        }
174
175
        return $columnNamesToValues;
176
    }
177
178
    /**
179
     * @param array $hash
180
     *
181
     * @return Entity
182
     */
183
    protected function loadEntity(array $hash)
184
    {
185
        return new Entity(
186
            $hash['id'],
187
            $hash['name'],
188
            $hash['identifier']
189
        );
190
    }
191
192
    /**
193
     * @return SelectQuery
194
     */
195
    private function getBaseQuery()
196
    {
197
        /** @var SelectQuery $query */
198
        $query = (new QueryBuilder())
199
            ->select(
200
                'page_categories.id',
201
                'page_categories.name',
202
                'page_categories.identifier'
203
            )
204
            ->from('page_categories')
205
            ->where('page_categories.deleted = 0');
206
207
        return $query;
208
    }
209
}
210