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

CacheReportStatusRepository::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 15
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\Repository;
4
5
use Doctrine\DBAL\Connection;
6
use Oc\Entity\GeoCacheReportStatusEntity;
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 View Code Duplication
class CacheReportStatusRepository
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...
13
{
14
    const TABLE = 'cache_report_status';
15
16
    /**
17
     * @var Connection
18
     */
19
    private $connection;
20
21
    /**
22
     * CacheReportStatusRepository constructor.
23
     *
24
     * @param Connection $connection
25
     */
26
    public function __construct(Connection $connection)
27
    {
28
        $this->connection = $connection;
29
    }
30
31
    /**
32
     * @return array
33
     * @throws RecordsNotFoundException
34
     */
35
    public function fetchAll()
36
    {
37
        $statement = $this->connection->createQueryBuilder()
38
            ->select('*')
39
            ->from(self::TABLE)
40
            ->execute();
41
42
        $result = $statement->fetchAll();
43
44
        if ($statement->rowCount() === 0) {
45
            throw new RecordsNotFoundException('No records found');
46
        }
47
48
        $records = [];
49
50
        foreach ($result as $item) {
51
            $records[] = $this->getEntityFromDatabaseArray($item);
52
        }
53
54
        return $records;
55
    }
56
57
    /**
58
     * @param array $where
59
     *
60
     * @return GeoCacheReportStatusEntity
61
     * @throws RecordNotFoundException
62
     */
63
    public function fetchOneBy(array $where = [])
64
    {
65
        $queryBuilder = $this->connection->createQueryBuilder()
66
            ->select('*')
67
            ->from(self::TABLE)
68
            ->setMaxResults(1);
69
70
        if (count($where) > 0) {
71
            foreach ($where as $column => $value) {
72
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
73
            }
74
        }
75
76
        $statement = $queryBuilder->execute();
77
78
        $result = $statement->fetch();
79
80
        if ($statement->rowCount() === 0) {
81
            throw new RecordNotFoundException('Record with given where clause not found');
82
        }
83
84
        return $this->getEntityFromDatabaseArray($result);
85
    }
86
87
    /**
88
     * @param array $where
89
     *
90
     * @return array
91
     * @throws RecordsNotFoundException
92
     */
93
    public function fetchBy(array $where = [])
94
    {
95
        $queryBuilder = $this->connection->createQueryBuilder()
96
            ->select('*')
97
            ->from(self::TABLE);
98
99
        if (count($where) > 0) {
100
            foreach ($where as $column => $value) {
101
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
102
            }
103
        }
104
105
        $statement = $queryBuilder->execute();
106
107
        $result = $statement->fetchAll();
108
109
        if ($statement->rowCount() === 0) {
110
            throw new RecordsNotFoundException('No records with given where clause found');
111
        }
112
113
        $entities = [];
114
115
        foreach ($result as $item) {
116
            $entities[] = $this->getEntityFromDatabaseArray($item);
117
        }
118
119
        return $entities;
120
    }
121
122
    /**
123
     * @param GeoCacheReportStatusEntity $entity
124
     *
125
     * @return GeoCacheReportStatusEntity
126
     * @throws RecordAlreadyExistsException
127
     * @throws \Doctrine\DBAL\DBALException
128
     */
129
    public function create(GeoCacheReportStatusEntity $entity)
130
    {
131
        if (!$entity->isNew()) {
132
            throw new RecordAlreadyExistsException('The entity does already exist.');
133
        }
134
135
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
136
137
        $this->connection->insert(
138
            self::TABLE,
139
            $databaseArray
140
        );
141
142
        $entity->id = (int) $this->connection->lastInsertId();
143
144
        return $entity;
145
    }
146
147
    /**
148
     * @param GeoCacheReportStatusEntity $entity
149
     *
150
     * @return GeoCacheReportStatusEntity
151
     * @throws RecordNotPersistedException
152
     * @throws \Doctrine\DBAL\DBALException
153
     */
154
    public function update(GeoCacheReportStatusEntity $entity)
155
    {
156
        if ($entity->isNew()) {
157
            throw new RecordNotPersistedException('The entity does not exist.');
158
        }
159
160
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
161
162
        $this->connection->update(
163
            self::TABLE,
164
            $databaseArray,
165
            ['id' => $entity->id]
166
        );
167
168
        return $entity;
169
    }
170
171
    /**
172
     * @param GeoCacheReportStatusEntity $entity
173
     *
174
     * @return GeoCacheReportStatusEntity
175
     * @throws RecordNotPersistedException
176
     * @throws \Doctrine\DBAL\DBALException
177
     * @throws \Doctrine\DBAL\Exception\InvalidArgumentException
178
     */
179
    public function remove(GeoCacheReportStatusEntity $entity)
180
    {
181
        if ($entity->isNew()) {
182
            throw new RecordNotPersistedException('The entity does not exist.');
183
        }
184
185
        $this->connection->delete(
186
            self::TABLE,
187
            ['id' => $entity->id]
188
        );
189
190
        $entity->cacheId = null;
0 ignored issues
show
Bug introduced by
The property cacheId does not seem to exist in Oc\Entity\GeoCacheReportStatusEntity.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
191
192
        return $entity;
193
    }
194
195
    /**
196
     * @param GeoCacheReportStatusEntity $entity
197
     *
198
     * @return array
199
     */
200
    public function getDatabaseArrayFromEntity(GeoCacheReportStatusEntity $entity)
201
    {
202
        return [
203
            'id' => $entity->id,
204
            'name' => $entity->name,
205
            'trans_id' => $entity->transId,
206
        ];
207
    }
208
209
    /**
210
     * @param array $data
211
     *
212
     * @return GeoCacheReportStatusEntity
213
     */
214
    public function getEntityFromDatabaseArray(array $data)
215
    {
216
        $entity = new GeoCacheReportStatusEntity();
217
        $entity->id = (int) $data['id'];
218
        $entity->name = (string) $data['name'];
219
        $entity->transId = (int) $data['trans_id'];
220
221
        return $entity;
222
    }
223
}
224