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

CacheCoordinatesRepository   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 223
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 8

9 Methods

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