Completed
Pull Request — development (#829)
by
unknown
05:04
created

CacheLogsArchivedRepository::fetchOneBy()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 23
loc 23
rs 9.552
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\GeoCacheLogsArchivedEntity;
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
/**
14
 * Class CacheLogsArchivedRepository
15
 *
16
 * @package Oc\Repository
17
 */
18 View Code Duplication
class CacheLogsArchivedRepository
0 ignored issues
show
Duplication introduced by
This class 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...
19
{
20
    const TABLE = 'cache_logs_archived';
21
22
    /** @var Connection */
23
    private $connection;
24
25
    /**
26
     * CacheLogsArchivedRepository constructor.
27
     *
28
     * @param Connection $connection
29
     */
30
    public function __construct(Connection $connection)
31
    {
32
        $this->connection = $connection;
33
    }
34
35
    /**
36
     * @return array
37
     * @throws RecordsNotFoundException
38
     */
39
    public function fetchAll()
40
    {
41
        $statement = $this->connection->createQueryBuilder()
42
            ->select('*')
43
            ->from(self::TABLE)
44
            ->execute();
45
46
        $result = $statement->fetchAll();
47
48
        if ($statement->rowCount() === 0) {
49
            throw new RecordsNotFoundException('No records found');
50
        }
51
52
        $records = [];
53
54
        foreach ($result as $item) {
55
            $records[] = $this->getEntityFromDatabaseArray($item);
56
        }
57
58
        return $records;
59
    }
60
61
    /**
62
     * @param array $where
63
     *
64
     * @return GeoCacheLogsArchivedEntity
65
     * @throws RecordNotFoundException
66
     */
67
    public function fetchOneBy(array $where = [])
68
    {
69
        $queryBuilder = $this->connection->createQueryBuilder()
70
            ->select('*')
71
            ->from(self::TABLE)
72
            ->setMaxResults(1);
73
74
        if (count($where) > 0) {
75
            foreach ($where as $column => $value) {
76
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
77
            }
78
        }
79
80
        $statement = $queryBuilder->execute();
81
82
        $result = $statement->fetch();
83
84
        if ($statement->rowCount() === 0) {
85
            throw new RecordNotFoundException('Record with given where clause not found');
86
        }
87
88
        return $this->getEntityFromDatabaseArray($result);
89
    }
90
91
    /**
92
     * @param array $where
93
     *
94
     * @return array
95
     * @throws RecordsNotFoundException
96
     */
97
    public function fetchBy(array $where = [])
98
    {
99
        $queryBuilder = $this->connection->createQueryBuilder()
100
            ->select('*')
101
            ->from(self::TABLE);
102
103
        if (count($where) > 0) {
104
            foreach ($where as $column => $value) {
105
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
106
            }
107
        }
108
109
        $statement = $queryBuilder->execute();
110
111
        $result = $statement->fetchAll();
112
113
        if ($statement->rowCount() === 0) {
114
//            throw new RecordsNotFoundException('No records with given where clause found');
115
        }
116
117
        $entities = [];
118
119
        foreach ($result as $item) {
120
            $entities[] = $this->getEntityFromDatabaseArray($item);
121
        }
122
123
        return $entities;
124
    }
125
126
    /**
127
     * @param GeoCacheLogsArchivedEntity $entity
128
     *
129
     * @return GeoCacheLogsArchivedEntity
130
     * @throws RecordAlreadyExistsException
131
     * @throws \Doctrine\DBAL\DBALException
132
     */
133
    public function create(GeoCacheLogsArchivedEntity $entity)
134
    {
135
        if (!$entity->isNew()) {
136
            throw new RecordAlreadyExistsException('The entity does already exist.');
137
        }
138
139
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
140
141
        $this->connection->insert(
142
            self::TABLE,
143
            $databaseArray
144
        );
145
146
        $entity->id = (int) $this->connection->lastInsertId();
147
148
        return $entity;
149
    }
150
151
    /**
152
     * @param GeoCacheLogsArchivedEntity $entity
153
     *
154
     * @return GeoCacheLogsArchivedEntity
155
     * @throws RecordNotPersistedException
156
     * @throws \Doctrine\DBAL\DBALException
157
     */
158
    public function update(GeoCacheLogsArchivedEntity $entity)
159
    {
160
        if ($entity->isNew()) {
161
            throw new RecordNotPersistedException('The entity does not exist.');
162
        }
163
164
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
165
166
        $this->connection->update(
167
            self::TABLE,
168
            $databaseArray,
169
            ['id' => $entity->id]
170
        );
171
172
        return $entity;
173
    }
174
175
    /**
176
     * @param GeoCacheLogsArchivedEntity $entity
177
     *
178
     * @return GeoCacheLogsArchivedEntity
179
     * @throws RecordNotPersistedException
180
     * @throws \Doctrine\DBAL\DBALException
181
     * @throws \Doctrine\DBAL\Exception\InvalidArgumentException
182
     */
183
    public function remove(GeoCacheLogsArchivedEntity $entity)
184
    {
185
        if ($entity->isNew()) {
186
            throw new RecordNotPersistedException('The entity does not exist.');
187
        }
188
189
        $this->connection->delete(
190
            self::TABLE,
191
            ['id' => $entity->id]
192
        );
193
194
        $entity->cacheId = null;
195
196
        return $entity;
197
    }
198
199
    /**
200
     * @param GeoCacheLogsArchivedEntity $entity
201
     *
202
     * @return array
203
     */
204
    public function getDatabaseArrayFromEntity(GeoCacheLogsArchivedEntity $entity)
205
    {
206
        return [
207
            'id' => $entity->id,
208
            'uuid' => $entity->uuid,
209
            'node' => $entity->node,
210
            'date_created' => $entity->dateCreated,
211
            'entry_last_modified' => $entity->entryLastModified,
212
            'last_modified' => $entity->lastModified,
213
            'okapi_syncbase' => $entity->okapiSyncbase,
214
            'log_last_modified' => $entity->logLastModified,
215
            'cache_id' => $entity->cacheId,
216
            'user_id' => $entity->userId,
217
            'type' => $entity->type,
218
            'oc_team_comment' => $entity->ocTeamComment,
219
            'date' => $entity->date,
220
            'order_date' => $entity->orderDate,
221
            'needs_maintenance' => $entity->needsMaintenance,
222
            'listing_outdated' => $entity->listingOutdated,
223
            'text' => $entity->text,
224
            'text_html' => $entity->textHtml,
225
            'text_htmledit' => $entity->textHtmledit,
226
            'owner_notified' => $entity->ownerNotified,
227
            'picture' => $entity->picture,
228
            'deletion_date' => $entity->deletionDate,
229
            'deleted_by' => $entity->deletedBy,
230
            'restored_by' => $entity->restoredBy,
231
        ];
232
    }
233
234
    /**
235
     * @param array $data
236
     *
237
     * @return GeoCacheLogsArchivedEntity
238
     * @throws \Exception
239
     */
240
    public function getEntityFromDatabaseArray(array $data)
241
    {
242
        $entity = new GeoCacheLogsArchivedEntity();
243
        $entity->id = (int) $data['id'];
244
        $entity->uuid = (string) $data['uuid'];
245
        $entity->node = (int) $data['node'];
246
        $entity->dateCreated = new DateTime($data['date_created']);
247
        $entity->entryLastModified = new DateTime($data['entry_last_modified']);
248
        $entity->lastModified = new DateTime($data['last_modified']);
249
        $entity->okapiSyncbase = (string) $data['okapi_syncbase'];
250
        $entity->logLastModified = new DateTime($data['log_last_modified']);
251
        $entity->cacheId = (int) $data['cache_id'];
252
        $entity->userId = (int) $data['user_id'];
253
        $entity->type = (int) $data['type'];
254
        $entity->ocTeamComment = (int) $data['oc_team_comment'];
255
        $entity->date = new DateTime($data['date']);
256
        $entity->orderDate = new DateTime($data['order_date']);
257
        $entity->needsMaintenance = (int) $data['needs_maintenance'];
258
        $entity->listingOutdated = (int) $data['listing_outdated'];
259
        $entity->text = (string) $data['text'];
260
        $entity->textHtml = (int) $data['text_html'];
261
        $entity->textHtmledit = (int) $data['text_htmledit'];
262
        $entity->ownerNotified = (int) $data['owner_notified'];
263
        $entity->picture = $data['picture'];
264
        $entity->deletionDate = new DateTime($data['deletion_date']);
265
        $entity->deletedBy = (int) $data['deleted_by'];
266
        $entity->restoredBy = (int) $data['restored_by'];
267
268
        return $entity;
269
    }
270
}
271