1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oc\Page; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class BlockRepository |
10
|
|
|
* |
11
|
|
|
* @package Oc\Page |
12
|
|
|
* @author Nick Lubisch <[email protected]> |
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
|
|
|
* @return BlockEntity[] |
44
|
|
|
*/ |
45
|
|
|
public function fetchBy(array $where = []) |
46
|
|
|
{ |
47
|
|
|
$queryBuilder = $this->connection->createQueryBuilder() |
48
|
|
|
->select('*') |
49
|
|
|
->from(self::TABLE); |
50
|
|
|
|
51
|
|
View Code Duplication |
if (count($where) > 0) { |
52
|
|
|
foreach ($where as $column => $value) { |
53
|
|
|
$queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value)); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$statement = $queryBuilder->execute(); |
58
|
|
|
|
59
|
|
|
$result = $statement->fetchAll(); |
60
|
|
|
|
61
|
|
|
if ($result === false) { |
62
|
|
|
return []; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$blocks = []; |
66
|
|
|
|
67
|
|
|
foreach ($result as $item) { |
68
|
|
|
$blocks[] = (new BlockEntity())->fromDatabaseArray($item); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $blocks; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Creates a block in the database. |
76
|
|
|
* |
77
|
|
|
* @param BlockEntity $entity |
78
|
|
|
* |
79
|
|
|
* @return BlockEntity |
80
|
|
|
*/ |
81
|
|
|
public function create(BlockEntity $entity) |
82
|
|
|
{ |
83
|
|
|
if (!$entity->isNew()) { |
84
|
|
|
throw new InvalidArgumentException('The entity does already exist.'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->connection->insert( |
88
|
|
|
self::TABLE, |
89
|
|
|
$entity->toDatabaseArray() |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$entity->id = (int) $this->connection->lastInsertId(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Update a page in the database. |
97
|
|
|
* |
98
|
|
|
* @param BlockEntity $entity |
99
|
|
|
* |
100
|
|
|
* @return BlockEntity |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function update(BlockEntity $entity) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
if ($entity->isNew()) { |
105
|
|
|
throw new \InvalidArgumentException('The entity does not exist.'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->connection->update( |
109
|
|
|
self::TABLE, |
110
|
|
|
$entity->toDatabaseArray(), |
111
|
|
|
['id' => $entity->id] |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$entity->id = (int) $this->connection->lastInsertId(); |
115
|
|
|
|
116
|
|
|
return $entity; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Removes a page from the database. |
121
|
|
|
* |
122
|
|
|
* @param BlockEntity $entity |
123
|
|
|
* |
124
|
|
|
* @return BlockEntity |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function remove(BlockEntity $entity) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
if ($entity->isNew()) { |
129
|
|
|
throw new \InvalidArgumentException('The entity does not exist.'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$this->connection->delete( |
133
|
|
|
self::TABLE, |
134
|
|
|
$entity->toDatabaseArray(), |
135
|
|
|
['id' => $entity->id] |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
$entity->id = null; |
139
|
|
|
|
140
|
|
|
return $entity; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
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.