Passed
Push — master ( 80fed1...32ef61 )
by Peter
09:46
created

UserLanguageSqlDataMapper   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 75
c 1
b 0
f 0
dl 0
loc 191
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getByIdentifier() 0 10 1
A add() 0 17 1
A getBaseQuery() 0 13 1
A update() 0 23 1
A getAll() 0 7 1
A loadEntity() 0 6 1
A getById() 0 10 1
A delete() 0 16 1
A getPage() 0 23 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Orm\DataMappers;
6
7
use AbterPhp\Admin\Domain\Entities\UserLanguage as Entity;
8
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
9
use Opulence\Orm\DataMappers\SqlDataMapper;
10
use Opulence\QueryBuilders\Expression;
11
use Opulence\QueryBuilders\MySql\QueryBuilder;
12
use Opulence\QueryBuilders\MySql\SelectQuery;
13
14
/** @phan-file-suppress PhanTypeMismatchArgument */
15
class UserLanguageSqlDataMapper extends SqlDataMapper implements IUserLanguageDataMapper
16
{
17
    /**
18
     * @param IStringerEntity $entity
19
     */
20
    public function add($entity)
21
    {
22
        assert($entity instanceof Entity, new \InvalidArgumentException());
23
24
        $query = (new QueryBuilder())
25
            ->insert(
26
                'user_languages',
27
                [
28
                    'id'         => $entity->getId(),
29
                    'identifier' => $entity->getIdentifier(),
30
                    'name'       => $entity->getName(),
31
                ]
32
            );
33
34
        $statement = $this->writeConnection->prepare($query->getSql());
35
        $statement->bindValues($query->getParameters());
36
        $statement->execute();
37
    }
38
39
    /**
40
     * @param IStringerEntity $entity
41
     *
42
     * @throws \Opulence\QueryBuilders\InvalidQueryException
43
     */
44
    public function delete($entity)
45
    {
46
        assert($entity instanceof Entity, new \InvalidArgumentException());
47
48
        $query = (new QueryBuilder())
49
            ->update(
50
                'user_languages',
51
                'user_languages',
52
                ['deleted_at' => new Expression('NOW()')]
53
            )
54
            ->where('id = ?')
55
            ->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR);
56
57
        $statement = $this->writeConnection->prepare($query->getSql());
58
        $statement->bindValues($query->getParameters());
59
        $statement->execute();
60
    }
61
62
    /**
63
     * @return Entity[]
64
     * @throws \Opulence\Orm\OrmException
65
     */
66
    public function getAll(): array
67
    {
68
        $query = $this->getBaseQuery();
69
70
        $sql = $query->getSql();
71
72
        return $this->read($sql, [], 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...
73
    }
74
75
    /**
76
     * @param int      $limitFrom
77
     * @param int      $pageSize
78
     * @param string[] $orders
79
     * @param array    $conditions
80
     * @param array    $params
81
     *
82
     * @return Entity[]
83
     * @throws \Opulence\Orm\OrmException
84
     */
85
    public function getPage(int $limitFrom, int $pageSize, array $orders, array $conditions, array $params): array
86
    {
87
        $query = $this->getBaseQuery()
88
            ->limit($pageSize)
89
            ->offset($limitFrom);
90
91
        if (!$orders) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $orders of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
92
            $query->orderBy('created_at ASC');
93
        }
94
        foreach ($orders as $order) {
95
            $query->addOrderBy($order);
96
        }
97
98
        foreach ($conditions as $condition) {
99
            $query->andWhere($condition);
100
        }
101
102
        $replaceCount = 1;
103
104
        $sql = $query->getSql();
105
        $sql = str_replace('SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $sql, $replaceCount);
106
107
        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...
108
    }
109
110
    /**
111
     * @param string $id
112
     *
113
     * @return Entity|null
114
     * @throws \Opulence\Orm\OrmException
115
     */
116
    public function getById($id)
117
    {
118
        $query = $this->getBaseQuery()->andWhere('user_languages.id = :user_language_id');
119
120
        $sql    = $query->getSql();
121
        $params = [
122
            'user_language_id' => [$id, \PDO::PARAM_STR],
123
        ];
124
125
        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\Admin\Domain\Entities\UserLanguage|null.
Loading history...
126
    }
127
128
    /**
129
     * @param string $identifier
130
     *
131
     * @return Entity|null
132
     * @throws \Opulence\Orm\OrmException
133
     */
134
    public function getByIdentifier(string $identifier): ?Entity
135
    {
136
        $query = $this->getBaseQuery()->andWhere('identifier = :identifier');
137
138
        $sql    = $query->getSql();
139
        $params = [
140
            'identifier' => [$identifier, \PDO::PARAM_STR],
141
        ];
142
143
        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\Admin\Domain\Entities\UserLanguage|null. Consider adding an additional type-check to rule them out.
Loading history...
144
    }
145
146
    /**
147
     * @param IStringerEntity $entity
148
     *
149
     * @throws \Opulence\QueryBuilders\InvalidQueryException
150
     */
151
    public function update($entity)
152
    {
153
        assert($entity instanceof Entity, new \InvalidArgumentException());
154
155
        $query = (new QueryBuilder())
156
            ->update(
157
                'user_languages',
158
                'user_languages',
159
                [
160
                    'identifier' => [$entity->getIdentifier(), \PDO::PARAM_STR],
161
                    'name'       => [$entity->getName(), \PDO::PARAM_STR],
162
                ]
163
            )
164
            ->where('id = ?')
165
            ->andWhere('deleted_at IS NULL')
166
            ->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR);
167
168
        $sql    = $query->getSql();
169
        $params = $query->getParameters();
170
171
        $statement = $this->writeConnection->prepare($sql);
172
        $statement->bindValues($params);
173
        $statement->execute();
174
    }
175
176
    /**
177
     * @param array $hash
178
     *
179
     * @return Entity
180
     */
181
    protected function loadEntity(array $hash): Entity
182
    {
183
        return new Entity(
184
            $hash['id'],
185
            $hash['identifier'],
186
            $hash['name']
187
        );
188
    }
189
190
    /**
191
     * @return SelectQuery
192
     */
193
    private function getBaseQuery(): SelectQuery
194
    {
195
        /** @var SelectQuery $query */
196
        $query = (new QueryBuilder())
197
            ->select(
198
                'user_languages.id',
199
                'user_languages.identifier',
200
                'user_languages.name'
201
            )
202
            ->from('user_languages')
203
            ->where('user_languages.deleted_at IS NULL');
204
205
        return $query;
206
    }
207
}
208