Completed
Pull Request — development (#813)
by
unknown
04:24
created

CacheStatusRepository::fetchOneBy()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\Repository;
4
5
use Doctrine\DBAL\Connection;
6
use Oc\Entity\GeoCacheStatusEntity;
7
use Oc\Repository\Exception\RecordAlreadyExistsException;
8
use Oc\Repository\Exception\RecordNotFoundException;
9
use Oc\Repository\Exception\RecordNotPersistedException;
10
use Oc\Repository\Exception\RecordsNotFoundException;
11
12
class CacheStatusRepository
13
{
14
    const TABLE = 'cache_status';
15
16
    /** @var Connection */
17
    private $connection;
18
19
    public function __construct(Connection $connection)
20
    {
21
        $this->connection = $connection;
22
    }
23
24
    /**
25
     * @return array
26
     * @throws RecordsNotFoundException
27
     */
28
    public function fetchAll()
29
    {
30
        $statement = $this->connection->createQueryBuilder()
31
            ->select('*')
32
            ->from(self::TABLE)
33
            ->execute();
34
35
        $result = $statement->fetchAll();
36
37
        if ($statement->rowCount() === 0) {
38
            throw new RecordsNotFoundException('No records found');
39
        }
40
41
        $records = [];
42
43
        foreach ($result as $item) {
44
            $records[] = $this->getEntityFromDatabaseArray($item);
45
        }
46
47
        return $records;
48
    }
49
50
    /**
51
     * @param array $where
52
     *
53
     * @return GeoCacheStatusEntity
54
     * @throws RecordNotFoundException
55
     */
56
    public function fetchOneBy(array $where = [])
57
    {
58
        $queryBuilder = $this->connection->createQueryBuilder()
59
            ->select('*')
60
            ->from(self::TABLE)
61
            ->setMaxResults(1);
62
63
        if (count($where) > 0) {
64
            foreach ($where as $column => $value) {
65
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
66
            }
67
        }
68
69
        $statement = $queryBuilder->execute();
70
71
        $result = $statement->fetch();
72
73
        if ($statement->rowCount() === 0) {
74
            throw new RecordNotFoundException('Record with given where clause not found');
75
        }
76
77
        return $this->getEntityFromDatabaseArray($result);
78
    }
79
80
    /**
81
     * @param array $where
82
     *
83
     * @return array
84
     * @throws RecordsNotFoundException
85
     */
86
    public function fetchBy(array $where = [])
87
    {
88
        $queryBuilder = $this->connection->createQueryBuilder()
89
            ->select('*')
90
            ->from(self::TABLE);
91
92
        if (count($where) > 0) {
93
            foreach ($where as $column => $value) {
94
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
95
            }
96
        }
97
98
        $statement = $queryBuilder->execute();
99
100
        $result = $statement->fetchAll();
101
102
        if ($statement->rowCount() === 0) {
103
            throw new RecordsNotFoundException('No records with given where clause found');
104
        }
105
106
        $entities = [];
107
108
        foreach ($result as $item) {
109
            $entities[] = $this->getEntityFromDatabaseArray($item);
110
        }
111
112
        return $entities;
113
    }
114
115
    /**
116
     * @param GeoCacheStatusEntity $entity
117
     *
118
     * @return GeoCacheStatusEntity
119
     * @throws RecordAlreadyExistsException
120
     * @throws \Doctrine\DBAL\DBALException
121
     */
122
    public function create(GeoCacheStatusEntity $entity)
123
    {
124
        if (!$entity->isNew()) {
125
            throw new RecordAlreadyExistsException('The entity does already exist.');
126
        }
127
128
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
129
130
        $this->connection->insert(
131
            self::TABLE,
132
            $databaseArray
133
        );
134
135
        $entity->id = (int)$this->connection->lastInsertId();
136
137
        return $entity;
138
    }
139
140
    /**
141
     * @param GeoCacheStatusEntity $entity
142
     *
143
     * @return GeoCacheStatusEntity
144
     * @throws RecordNotPersistedException
145
     * @throws \Doctrine\DBAL\DBALException
146
     */
147
    public function update(GeoCacheStatusEntity $entity)
148
    {
149
        if ($entity->isNew()) {
150
            throw new RecordNotPersistedException('The entity does not exist.');
151
        }
152
153
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
154
155
        $this->connection->update(
156
            self::TABLE,
157
            $databaseArray,
158
            ['id' => $entity->id]
159
        );
160
161
        return $entity;
162
    }
163
164
    /**
165
     * @param GeoCacheStatusEntity $entity
166
     *
167
     * @return GeoCacheStatusEntity
168
     * @throws RecordNotPersistedException
169
     * @throws \Doctrine\DBAL\DBALException
170
     * @throws \Doctrine\DBAL\Exception\InvalidArgumentException
171
     */
172
    public function remove(GeoCacheStatusEntity $entity)
173
    {
174
        if ($entity->isNew()) {
175
            throw new RecordNotPersistedException('The entity does not exist.');
176
        }
177
178
        $this->connection->delete(
179
            self::TABLE,
180
            ['id' => $entity->id]
181
        );
182
183
        $entity->id = null;
184
185
        return $entity;
186
    }
187
188
    /**
189
     * @param GeoCacheStatusEntity $entity
190
     *
191
     * @return array
192
     */
193
    public function getDatabaseArrayFromEntity(GeoCacheStatusEntity $entity)
194
    {
195
        return [
196
            'id' => $entity->id,
197
            'name' => $entity->name,
198
            'trans_id' => $entity->transId,
199
            'de' => $entity->de,
200
            'en' => $entity->en,
201
            'allow_user_view' => $entity->allowUserView,
202
            'allow_owner_edit_status' => $entity->allowOwnerEditStatus,
203
            'allow_user_log' => $entity->allowUserLog,
204
        ];
205
    }
206
207
    /**
208
     * @param array $data
209
     *
210
     * @return GeoCacheStatusEntity
211
     */
212
    public function getEntityFromDatabaseArray(array $data)
213
    {
214
        $entity = new GeoCacheStatusEntity();
215
        $entity->id = (int)$data['id'];
216
        $entity->name = (string)$data['name'];
217
        $entity->transId = (int)$data['trans_id'];
218
        $entity->de = (string)$data['de'];
219
        $entity->en = (string)$data['en'];
220
        $entity->allowUserView = (int)$data['allow_user_view'];
221
        $entity->allowOwnerEditStatus = (int)$data['allow_owner_edit_status'];
222
        $entity->allowUserLog = (int)$data['allow_user_log'];
223
224
        return $entity;
225
    }
226
}
227