Completed
Push — development ( 9736ae...1c21f9 )
by Mirco
24s queued 10s
created

SecurityRoleHierarchyRepository::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\SecurityRoleHierarchyEntity;
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 SecurityRoleHierarchyRepository
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 = 'security_role_hierarchy';
15
16
    /** @var Connection */
17
    private $connection;
18
19
20
    public function __construct(Connection $connection)
21
    {
22
        $this->connection = $connection;
23
    }
24
25
26
    /**
27
     * @return SecurityRoleHierarchyEntity[]
28
     */
29
    public function fetchAll()
30
    {
31
        $statement = $this->connection->createQueryBuilder()
32
            ->select('*')
33
            ->from(self::TABLE)
34
            ->execute();
35
36
        $result = $statement->fetchAll();
37
38
        if ($statement->rowCount() === 0) {
39
            throw new RecordsNotFoundException('No records found');
40
        }
41
42
        $records = [];
43
44
        foreach ($result as $item) {
45
            $records[] = $this->getEntityFromDatabaseArray($item);
46
        }
47
48
        return $records;
49
    }
50
51
52
    /**
53
     * @return SecurityRoleHierarchyEntity
54
     */
55
    public function fetchOneBy(array $where = [])
56
    {
57
        $queryBuilder = $this->connection->createQueryBuilder()
58
            ->select('*')
59
            ->from(self::TABLE)
60
            ->setMaxResults(1);
61
62
        if (count($where) > 0) {
63
            foreach ($where as $column => $value) {
64
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
65
            }
66
        }
67
68
        $statement = $queryBuilder->execute();
69
70
        $result = $statement->fetch();
71
72
        if ($statement->rowCount() === 0) {
73
            throw new RecordNotFoundException('Record with given where clause not found');
74
        }
75
76
        return $this->getEntityFromDatabaseArray($result);
77
    }
78
79
80
    /**
81
     * @return SecurityRoleHierarchyEntity[]
82
     */
83
    public function fetchBy(array $where = [])
84
    {
85
        $queryBuilder = $this->connection->createQueryBuilder()
86
            ->select('*')
87
            ->from(self::TABLE);
88
89
        if (count($where) > 0) {
90
            foreach ($where as $column => $value) {
91
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
92
            }
93
        }
94
95
        $statement = $queryBuilder->execute();
96
97
        $result = $statement->fetchAll();
98
99
        if ($statement->rowCount() === 0) {
100
            throw new RecordsNotFoundException('No records with given where clause found');
101
        }
102
103
        $entities = [];
104
105
        foreach ($result as $item) {
106
            $entities[] = $this->getEntityFromDatabaseArray($item);
107
        }
108
109
        return $entities;
110
    }
111
112
113
    /**
114
     * @return SecurityRoleHierarchyEntity
115
     */
116
    public function create(SecurityRoleHierarchyEntity $entity)
117
    {
118
        if (!$entity->isNew()) {
119
            throw new RecordAlreadyExistsException('The entity does already exist.');
120
        }
121
122
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
123
124
        $this->connection->insert(
125
            self::TABLE,
126
            $databaseArray
127
        );
128
129
        $entity->roleId = (int)$this->connection->lastInsertId();
130
131
        return $entity;
132
    }
133
134
135
    /**
136
     * @return SecurityRoleHierarchyEntity
137
     */
138
    public function update(SecurityRoleHierarchyEntity $entity)
139
    {
140
        if ($entity->isNew()) {
141
            throw new RecordNotPersistedException('The entity does not exist.');
142
        }
143
144
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
145
146
        $this->connection->update(
147
            self::TABLE,
148
            $databaseArray,
149
            ['role_id' => $entity->roleId]
150
        );
151
152
        return $entity;
153
    }
154
155
156
    /**
157
     * @return SecurityRoleHierarchyEntity
158
     */
159
    public function remove(SecurityRoleHierarchyEntity $entity)
160
    {
161
        if ($entity->isNew()) {
162
            throw new RecordNotPersistedException('The entity does not exist.');
163
        }
164
165
        $this->connection->delete(
166
            self::TABLE,
167
            ['role_id' => $entity->roleId]
168
        );
169
170
        $entity->cacheId = null;
0 ignored issues
show
Bug introduced by
The property cacheId does not seem to exist in Oc\Entity\SecurityRoleHierarchyEntity.

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...
171
172
        return $entity;
173
    }
174
175
176
    /**
177
     * @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...
178
     */
179
    public function getDatabaseArrayFromEntity(SecurityRoleHierarchyEntity $entity)
180
    {
181
        return [
182
            'role_id' => $entity->roleId,
183
            'sub_role_id' => $entity->subRoleId,
184
        ];
185
    }
186
187
188
    /**
189
     * @return SecurityRoleHierarchyEntity
190
     */
191
    public function getEntityFromDatabaseArray(array $data)
192
    {
193
        $entity = new SecurityRoleHierarchyEntity();
194
        $entity->roleId = (int)$data['role_id'];
195
        $entity->subRoleId = (int)$data['sub_role_id'];
196
        return $entity;
197
    }
198
}
199