These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Oc\Page; |
||
4 | |||
5 | use DateTime; |
||
6 | use Doctrine\DBAL\Connection; |
||
7 | use Oc\Repository\Exception\RecordAlreadyExistsException; |
||
8 | use Oc\Repository\Exception\RecordNotPersistedException; |
||
9 | use Oc\Repository\Exception\RecordsNotFoundException; |
||
10 | |||
11 | /** |
||
12 | * Class BlockRepository |
||
13 | */ |
||
14 | class BlockRepository |
||
15 | { |
||
16 | /** |
||
17 | * Database table name that this repository maintains. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | const TABLE = 'page_block'; |
||
22 | |||
23 | /** |
||
24 | * @var Connection |
||
25 | */ |
||
26 | private $connection; |
||
27 | |||
28 | /** |
||
29 | * BlockRepository constructor. |
||
30 | * |
||
31 | * @param Connection $connection |
||
32 | */ |
||
33 | public function __construct(Connection $connection) |
||
34 | { |
||
35 | $this->connection = $connection; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Fetches all blocks of a page. |
||
40 | * |
||
41 | * @param array $where |
||
42 | * |
||
43 | * @throws RecordsNotFoundException Thrown when no records are found |
||
44 | * @return BlockEntity[] |
||
45 | */ |
||
46 | View Code Duplication | public function fetchBy(array $where = []) |
|
47 | { |
||
48 | $queryBuilder = $this->connection->createQueryBuilder() |
||
49 | ->select('*') |
||
50 | ->from(self::TABLE); |
||
51 | |||
52 | if (count($where) > 0) { |
||
53 | foreach ($where as $column => $value) { |
||
54 | $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value)); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | $statement = $queryBuilder->execute(); |
||
59 | |||
60 | $result = $statement->fetchAll(); |
||
61 | |||
62 | if ($statement->rowCount() === 0) { |
||
63 | throw new RecordsNotFoundException('No records with given where clause found'); |
||
64 | } |
||
65 | |||
66 | $blocks = []; |
||
67 | |||
68 | foreach ($result as $item) { |
||
69 | $blocks[] = $this->getEntityFromDatabaseArray($item); |
||
70 | } |
||
71 | |||
72 | return $blocks; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Creates a block in the database. |
||
77 | * |
||
78 | * @param BlockEntity $entity |
||
79 | * |
||
80 | * @throws RecordAlreadyExistsException |
||
81 | * @return BlockEntity |
||
82 | */ |
||
83 | View Code Duplication | public function create(BlockEntity $entity) |
|
0 ignored issues
–
show
|
|||
84 | { |
||
85 | if (!$entity->isNew()) { |
||
86 | throw new RecordAlreadyExistsException('The entity does already exist.'); |
||
87 | } |
||
88 | |||
89 | $databaseArray = $this->getDatabaseArrayFromEntity($entity); |
||
90 | |||
91 | $this->connection->insert( |
||
92 | self::TABLE, |
||
93 | $databaseArray |
||
94 | ); |
||
95 | |||
96 | $entity->id = (int) $this->connection->lastInsertId(); |
||
97 | |||
98 | return $entity; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Update a page in the database. |
||
103 | * |
||
104 | * @param BlockEntity $entity |
||
105 | * |
||
106 | * @throws RecordNotPersistedException |
||
107 | * @return BlockEntity |
||
108 | */ |
||
109 | View Code Duplication | public function update(BlockEntity $entity) |
|
110 | { |
||
111 | if ($entity->isNew()) { |
||
112 | throw new RecordNotPersistedException('The entity does not exist.'); |
||
113 | } |
||
114 | |||
115 | $databaseArray = $this->getDatabaseArrayFromEntity($entity); |
||
116 | |||
117 | $this->connection->update( |
||
118 | self::TABLE, |
||
119 | $databaseArray, |
||
120 | ['id' => $entity->id] |
||
121 | ); |
||
122 | |||
123 | $entity->id = (int) $this->connection->lastInsertId(); |
||
124 | |||
125 | return $entity; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Removes a page from the database. |
||
130 | * |
||
131 | * @param BlockEntity $entity |
||
132 | * |
||
133 | * @throws RecordNotPersistedException |
||
134 | * @return BlockEntity |
||
135 | */ |
||
136 | View Code Duplication | public function remove(BlockEntity $entity) |
|
137 | { |
||
138 | if ($entity->isNew()) { |
||
139 | throw new RecordNotPersistedException('The entity does not exist.'); |
||
140 | } |
||
141 | |||
142 | $this->connection->delete( |
||
143 | self::TABLE, |
||
144 | ['id' => $entity->id] |
||
145 | ); |
||
146 | |||
147 | $entity->id = null; |
||
148 | |||
149 | return $entity; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Maps the given entity to the database array. |
||
154 | * |
||
155 | * @param BlockEntity $entity |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | View Code Duplication | public function getDatabaseArrayFromEntity(BlockEntity $entity) |
|
160 | { |
||
161 | return [ |
||
162 | 'id' => $entity->id, |
||
163 | 'page_id' => $entity->pageId, |
||
164 | 'title' => $entity->title, |
||
165 | 'html' => $entity->html, |
||
166 | 'position' => (int) $entity->position, |
||
167 | 'updated_at' => $entity->updatedAt->format(DateTime::ATOM), |
||
168 | 'active' => $entity->active, |
||
169 | ]; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Prepares database array from properties. |
||
174 | * |
||
175 | * @param array $data |
||
176 | * |
||
177 | * @return BlockEntity |
||
178 | */ |
||
179 | View Code Duplication | public function getEntityFromDatabaseArray(array $data) |
|
180 | { |
||
181 | $entity = new BlockEntity(); |
||
182 | $entity->id = (int) $data['id']; |
||
183 | $entity->pageId = (int) $data['page_id']; |
||
184 | $entity->title = $data['title']; |
||
185 | $entity->html = $data['html']; |
||
186 | $entity->position = (int) $data['position']; |
||
187 | $entity->updatedAt = new DateTime($data['updated_at']); |
||
188 | $entity->active = (bool) $data['active']; |
||
189 | |||
190 | return $entity; |
||
191 | } |
||
192 | } |
||
193 |
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.