Completed
Push — next ( 01f675...c3ca23 )
by Thomas
05:17
created

LanguagesListDefaultRepository::fetchOneBy()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 23
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
use Doctrine\DBAL\Connection;
4
use Oc\Repository\Exception\RecordAlreadyExistsException;
5
use Oc\Repository\Exception\RecordNotFoundException;
6
use Oc\Repository\Exception\RecordNotPersistedException;
7
use Oc\Repository\Exception\RecordsNotFoundException;
8
9 View Code Duplication
class LanguagesListDefaultRepository
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...
10
{
11
    const TABLE = 'languages_list_default';
12
13
    /** @var Connection */
14
    private $connection;
15
16
    public function __construct(Connection $connection)
17
    {
18
        $this->connection = $connection;
19
    }
20
21
    /**
22
     * @return LanguagesListDefaultEntity[]
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
     * @param array $where
48
     * @return LanguagesListDefaultEntity
49
     */
50
    public function fetchOneBy(array $where = [])
51
    {
52
        $queryBuilder = $this->connection->createQueryBuilder()
53
            ->select('*')
54
            ->from(self::TABLE)
55
            ->setMaxResults(1);
56
57
        if (count($where) > 0) {
58
            foreach ($where as $column => $value) {
59
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
60
            }
61
        }
62
63
        $statement = $queryBuilder->execute();
64
65
        $result = $statement->fetch();
66
67
        if ($statement->rowCount() === 0) {
68
            throw new RecordNotFoundException('Record with given where clause not found');
69
        }
70
71
        return $this->getEntityFromDatabaseArray($result);
72
    }
73
74
    /**
75
     * @param array $where
76
     * @return LanguagesListDefaultEntity[]
77
     */
78
    public function fetchBy(array $where = [])
79
    {
80
        $queryBuilder = $this->connection->createQueryBuilder()
81
            ->select('*')
82
            ->from(self::TABLE);
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->fetchAll();
93
94
        if ($statement->rowCount() === 0) {
95
            throw new RecordsNotFoundException('No records with given where clause found');
96
        }
97
98
        $entities = [];
99
100
        foreach ($result as $item) {
101
            $entities[] = $this->getEntityFromDatabaseArray($item);
102
        }
103
104
        return $entities;
105
    }
106
107
    /**
108
     * @param LanguagesListDefaultEntity $entity
109
     * @return LanguagesListDefaultEntity
110
     */
111
    public function create(LanguagesListDefaultEntity $entity)
112
    {
113
        if (!$entity->isNew()) {
114
            throw new RecordAlreadyExistsException('The entity does already exist.');
115
        }
116
117
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
118
119
        $this->connection->insert(
120
            self::TABLE,
121
            $databaseArray
122
        );
123
124
        $entity->lang = (int) $this->connection->lastInsertId();
0 ignored issues
show
Documentation Bug introduced by
The property $lang was declared of type string, but (int) $this->connection->lastInsertId() is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
125
126
        return $entity;
127
    }
128
129
    /**
130
     * @param LanguagesListDefaultEntity $entity
131
     * @return LanguagesListDefaultEntity
132
     */
133
    public function update(LanguagesListDefaultEntity $entity)
134
    {
135
        if ($entity->isNew()) {
136
            throw new RecordNotPersistedException('The entity does not exist.');
137
        }
138
139
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
140
141
        $this->connection->update(
142
            self::TABLE,
143
            $databaseArray,
144
            ['lang' => $entity->lang]
145
        );
146
147
        return $entity;
148
    }
149
150
    /**
151
     * @param LanguagesListDefaultEntity $entity
152
     * @return LanguagesListDefaultEntity
153
     */
154
    public function remove(LanguagesListDefaultEntity $entity)
155
    {
156
        if ($entity->isNew()) {
157
            throw new RecordNotPersistedException('The entity does not exist.');
158
        }
159
160
        $this->connection->delete(
161
            self::TABLE,
162
            ['lang' => $entity->lang]
163
        );
164
165
        $entity->cacheId = null;
0 ignored issues
show
Bug introduced by
The property cacheId does not seem to exist in LanguagesListDefaultEntity.

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...
166
167
        return $entity;
168
    }
169
170
    /**
171
     * @param LanguagesListDefaultEntity $entity
172
     * @return []
0 ignored issues
show
Documentation introduced by
The doc-type [] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
173
     */
174
    public function getDatabaseArrayFromEntity(LanguagesListDefaultEntity $entity)
175
    {
176
        return [
177
            'lang' => $entity->lang,
178
            'show' => $entity->show,
179
        ];
180
    }
181
182
    /**
183
     * @param array $data
184
     * @return LanguagesListDefaultEntity
185
     */
186
    public function getEntityFromDatabaseArray(array $data)
187
    {
188
        $entity = new LanguagesListDefaultEntity();
189
        $entity->lang = (string) $data['lang'];
190
        $entity->show = (string) $data['show'];
191
192
        return $entity;
193
    }
194
}
195