Completed
Push — development ( f93eb8...ffa1a0 )
by Thomas
20s
created

Persistence/GeoCacheLog/GeoCacheLogRepository.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Oc\GeoCache\Persistence\GeoCacheLog;
4
5
use DateTime;
6
use Doctrine\DBAL\Connection;
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 GeoCacheLogRepository
13
{
14
    /**
15
     * Database table name that this repository maintains.
16
     *
17
     * @var string
18
     */
19
    const TABLE = 'cache_logs';
20
21
    /**
22
     * @var Connection
23
     */
24
    private $connection;
25
26
    /**
27
     * GeoCacheLogRepository constructor.
28
     *
29
     * @param Connection $connection
30
     */
31
    public function __construct(Connection $connection)
32
    {
33
        $this->connection = $connection;
34
    }
35
36
    /**
37
     * Fetches all GeoCacheLogs.
38
     *
39
     * @throws RecordsNotFoundException Thrown when no records are found
40
     * @return GeoCacheLogEntity[]
41
     */
42 View Code Duplication
    public function fetchAll()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        $statement = $this->connection->createQueryBuilder()
45
            ->select('*')
46
            ->from(self::TABLE)
47
            ->execute();
48
49
        $result = $statement->fetchAll();
50
51
        if ($statement->rowCount() === 0) {
52
            throw new RecordsNotFoundException('No records found');
53
        }
54
55
        $records = [];
56
57
        foreach ($result as $item) {
58
            $records[] = $this->getEntityFromDatabaseArray($item);
59
        }
60
61
        return $records;
62
    }
63
64
    /**
65
     * Fetches a GeoCacheLog by given where clause.
66
     *
67
     * @param array $where
68
     *
69
     * @throws RecordNotFoundException Thrown when no record is found
70
     * @return null|GeoCacheLogEntity
71
     */
72 View Code Duplication
    public function fetchOneBy(array $where = [])
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        $queryBuilder = $this->connection->createQueryBuilder()
75
             ->select('*')
76
             ->from(self::TABLE)
77
             ->setMaxResults(1);
78
79
        if (count($where) > 0) {
80
            foreach ($where as $column => $value) {
81
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
82
            }
83
        }
84
85
        $statement = $queryBuilder->execute();
86
87
        $result = $statement->fetch();
88
89
        if ($statement->rowCount() === 0) {
90
            throw new RecordNotFoundException('Record with given where clause not found');
91
        }
92
93
        return $this->getEntityFromDatabaseArray($result);
94
    }
95
96
    /**
97
     * Fetches all GeoCacheLogs by given where clause.
98
     *
99
     * @param array $where
100
     *
101
     * @throws RecordsNotFoundException Thrown when no records are found
102
     * @return GeoCacheLogEntity[]
103
     */
104 View Code Duplication
    public function fetchBy(array $where = [])
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        $queryBuilder = $this->connection->createQueryBuilder()
107
             ->select('*')
108
             ->from(self::TABLE);
109
110
        if (count($where) > 0) {
111
            foreach ($where as $column => $value) {
112
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
113
            }
114
        }
115
116
        $statement = $queryBuilder->execute();
117
118
        $result = $statement->fetchAll();
119
120
        if ($statement->rowCount() === 0) {
121
            throw new RecordsNotFoundException('No records with given where clause found');
122
        }
123
124
        $geoCaches = [];
125
126
        foreach ($result as $item) {
127
            $geoCaches[] = $this->getEntityFromDatabaseArray($item);
128
        }
129
130
        return $geoCaches;
131
    }
132
133
    /**
134
     * Fetch latest user geo cache log.
135
     *
136
     * @param int $userId
137
     *
138
     * @throws RecordNotFoundException
139
     * @return GeoCacheLogEntity
140
     */
141 View Code Duplication
    public function getLatestUserLog($userId)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        $queryBuilder = $this->connection->createQueryBuilder()
144
            ->select('*')
145
            ->from(self::TABLE)
146
            ->where('user_id = :userId')
147
            ->orderBy('date', 'DESC')
148
            ->setParameter('userId', $userId)
149
            ->setMaxResults(1);
150
151
        $statement = $queryBuilder->execute();
152
153
        $result = $statement->fetch();
154
155
        if ($statement->rowCount() === 0) {
156
            throw new RecordNotFoundException('Record with given where clause not found');
157
        }
158
159
        return $this->getEntityFromDatabaseArray($result);
160
    }
161
162
    /**
163
     * Creates a GeoCacheLog in the database.
164
     *
165
     * @param GeoCacheLogEntity $entity
166
     *
167
     * @throws RecordAlreadyExistsException
168
     * @return GeoCacheLogEntity
169
     */
170
    public function create(GeoCacheLogEntity $entity)
171
    {
172
        if (!$entity->isNew()) {
173
            throw new RecordAlreadyExistsException('The entity does already exist.');
174
        }
175
176
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
177
178
        $this->connection->insert(
179
            self::TABLE,
180
            $databaseArray
181
        );
182
183
        $entity->id = (int) $this->connection->lastInsertId();
184
185
        return $entity;
186
    }
187
188
    /**
189
     * Update a GeoCacheLog in the database.
190
     *
191
     * @param GeoCacheLogEntity $entity
192
     *
193
     * @throws RecordNotPersistedException
194
     * @return GeoCacheLogEntity
195
     */
196
    public function update(GeoCacheLogEntity $entity)
197
    {
198
        if ($entity->isNew()) {
199
            throw new RecordNotPersistedException('The entity does not exist.');
200
        }
201
202
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
203
204
        $this->connection->update(
205
            self::TABLE,
206
            $databaseArray,
207
            ['id' => $entity->id]
208
        );
209
210
        $entity->id = (int) $this->connection->lastInsertId();
211
212
        return $entity;
213
    }
214
215
    /**
216
     * Removes a GeoCacheLog from the database.
217
     *
218
     * @param GeoCacheLogEntity $entity
219
     *
220
     * @throws RecordNotPersistedException
221
     * @return GeoCacheLogEntity
222
     */
223 View Code Duplication
    public function remove(GeoCacheLogEntity $entity)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
224
    {
225
        if ($entity->isNew()) {
226
            throw new RecordNotPersistedException('The entity does not exist.');
227
        }
228
229
        $this->connection->delete(
230
            self::TABLE,
231
            ['id' => $entity->id]
232
        );
233
234
        $entity->id = null;
235
236
        return $entity;
237
    }
238
239
    /**
240
     * Maps the given entity to the database array.
241
     *
242
     * @param GeoCacheLogEntity $entity
243
     *
244
     * @return array
245
     */
246
    public function getDatabaseArrayFromEntity(GeoCacheLogEntity $entity)
247
    {
248
        return [
249
            'id' => $entity->id,
250
            'uuid' => $entity->uuid,
251
            'node' => $entity->node,
252
            'date_created' => $entity->dateCreated->format(DateTime::ATOM),
253
            'entry_last_modified' => $entity->entryLastModified->format(DateTime::ATOM),
254
            'last_modified' => $entity->lastModified->format(DateTime::ATOM),
255
            'okapi_syncbase' => $entity->okapiSyncbase,
256
            'log_last_modified' => $entity->logLastModified->format(DateTime::ATOM),
257
            'cache_id' => $entity->cacheId,
258
            'user_id' => $entity->userId,
259
            'type' => $entity->type,
260
            'oc_team_comment' => $entity->ocTeamComment,
261
            'date' => $entity->date->format(DateTime::ATOM),
262
            'order_date' => $entity->orderDate->format(DateTime::ATOM),
263
            'needs_maintenance' => $entity->needsMaintenance,
264
            'listing_outdated' => $entity->listingOutdated,
265
            'text' => $entity->text,
266
            'text_html' => $entity->textHtml,
267
            'text_htmledit' => $entity->textHtmledit,
268
            'owner_notified' => $entity->ownerNotified,
269
            'picture' => $entity->picture,
270
        ];
271
    }
272
273
    /**
274
     * Prepares database array from properties.
275
     *
276
     * @param array $data
277
     *
278
     * @return GeoCacheLogEntity
279
     */
280
    public function getEntityFromDatabaseArray(array $data)
281
    {
282
        $entity = new GeoCacheLogEntity();
283
        $entity->id = (int) $data['id'];
284
        $entity->uuid = $data['uuid'];
285
        $entity->node = (int) $data['node'];
286
        $entity->dateCreated = new DateTime($data['date_created']);
287
        $entity->entryLastModified = new DateTime($data['entry_last_modified']);
288
        $entity->lastModified = new DateTime($data['last_modified']);
289
        $entity->okapiSyncbase = (int) $data['okapi_syncbase'];
290
        $entity->logLastModified = new DateTime($data['log_last_modified']);
291
        $entity->cacheId = (int) $data['cache_id'];
292
        $entity->userId = (int) $data['user_id'];
293
        $entity->type = (int) $data['type'];
294
        $entity->ocTeamComment = (bool) $data['oc_team_comment'];
295
        $entity->date = new DateTime($data['date']);
296
        $entity->orderDate = new DateTime($data['order_date']);
297
        $entity->needsMaintenance = (bool) $data['needs_maintenance'];
298
        $entity->listingOutdated = (bool) $data['listing_outdated'];
299
        $entity->text = $data['text'];
300
        $entity->textHtml = (bool) $data['text_html'];
301
        $entity->textHtmledit = (bool) $data['text_htmledit'];
302
        $entity->ownerNotified = (bool) $data['owner_notified'];
303
        $entity->picture = (int) $data['picture'];
304
305
        return $entity;
306
    }
307
}
308