Code Duplication    Length = 223-253 lines in 2 locations

htdocs_symfony/src/Repository/CacheLogsArchivedRepository.php 1 location

@@ 18-270 (lines=253) @@
15
 *
16
 * @package Oc\Repository
17
 */
18
class CacheLogsArchivedRepository
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

local/devel/Repositories/CacheLogsArchivedRepository.php 1 location

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