Completed
Pull Request — development (#824)
by
unknown
04:40
created

CacheReportsRepository::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
226
227
        return $entity;
228
    }
229
230
    /**
231
     * @param GeoCacheReportsEntity $entity
232
     *
233
     * @return array
234
     */
235
    public function getDatabaseArrayFromEntity(GeoCacheReportsEntity $entity)
236
    {
237
        return [
238
            'id' => $entity->id,
239
            'date_created' => $entity->dateCreated,
240
            'cacheid' => $entity->cacheid,
241
            'userid' => $entity->userid,
242
            'reason' => $entity->reason,
243
            'note' => $entity->note,
244
            'status' => $entity->status,
245
            'adminid' => $entity->adminid,
246
            'lastmodified' => $entity->lastmodified,
247
            'comment' => $entity->comment,
248
            'user' => $entity->user,
249
        ];
250
    }
251
252
    /**
253
     * @param array $data
254
     *
255
     * @return GeoCacheReportsEntity
256
     * @throws Exception
257
     */
258
    public function getEntityFromDatabaseArray(array $data)
259
    {
260
        $entity = new GeoCacheReportsEntity();
261
        $entity->id = (int) $data['id'];
262
        $entity->dateCreated = new DateTime($data['date_created']);
263
        $entity->cacheid = (int) $data['cacheid'];
264
        $entity->userid = (int) $data['userid'];
265
        $entity->reason = (int) $data['reason'];
266
        $entity->note = (string) $data['note'];
267
        $entity->status = (int) $data['status'];
268
        $entity->adminid = (int) $data['adminid'];
269
        $entity->lastmodified = (string) $data['lastmodified'];
270
        $entity->comment = (string) $data['comment'];
271
        $entity->cache = $this->cachesRepository->fetchOneBy(['cache_id' => $entity->cacheid]);
272
        $entity->user = $this->userRepository->fetchOneById($entity->userid);
273
        if ($entity->adminid) $entity->admin = $this->userRepository->fetchOneById($entity->adminid);
0 ignored issues
show
Bug introduced by
The property admin does not seem to exist in Oc\Entity\GeoCacheReportsEntity.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
274
        $entity->reportReason = $this->cacheReportReasonsRepository->fetchOneBy(['id' => $entity->reason]);
275
        $entity->reportStatus = $this->cacheReportStatusRepository->fetchOneBy(['id' => $entity->status]);
276
277
        return $entity;
278
    }
279
}
280