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

CacheStatusModifiedRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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