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

CacheReportReasonsRepository::fetchBy()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 28

Duplication

Lines 28
Ratio 100 %

Importance

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

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