1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chubbyphp\Model; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
|
7
|
|
|
abstract class AbstractDoctrineRepository implements RepositoryInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Connection |
11
|
|
|
*/ |
12
|
|
|
private $connection; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param Connection $connection |
16
|
|
|
*/ |
17
|
|
|
public function __construct(Connection $connection) |
18
|
|
|
{ |
19
|
|
|
$this->connection = $connection; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $id |
24
|
|
|
* |
25
|
|
|
* @return ModelInterface|null |
26
|
|
|
*/ |
27
|
|
|
public function find(string $id) |
28
|
|
|
{ |
29
|
|
|
$qb = $this->connection->createQueryBuilder(); |
30
|
|
|
$qb->select('*')->from($this->getTablename())->where($qb->expr()->eq('id', ':id'))->setParameter('id', $id); |
31
|
|
|
|
32
|
|
|
$row = $qb->execute()->fetch(\PDO::FETCH_ASSOC); |
33
|
|
|
if (false === $row) { |
34
|
|
|
return null; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** @var ModelInterface $modelClass */ |
38
|
|
|
$modelClass = $this->getModelClass(); |
39
|
|
|
|
40
|
|
|
return $modelClass::fromRow($row); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $criteria |
45
|
|
|
* |
46
|
|
|
* @return null|ModelInterface |
47
|
|
|
*/ |
48
|
|
|
public function findOneBy(array $criteria = []) |
49
|
|
|
{ |
50
|
|
|
$qb = $this->connection->createQueryBuilder(); |
51
|
|
|
$qb->select('*')->from($this->getTablename())->setMaxResults(1); |
52
|
|
|
|
53
|
|
View Code Duplication |
foreach ($criteria as $field => $value) { |
|
|
|
|
54
|
|
|
$qb->andWhere($qb->expr()->eq($field, ':'.$field)); |
55
|
|
|
$qb->setParameter($field, $value); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$row = $qb->execute()->fetch(\PDO::FETCH_ASSOC); |
59
|
|
|
if (false === $row) { |
60
|
|
|
return null; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** @var ModelInterface $modelClass */ |
64
|
|
|
$modelClass = $this->getModelClass(); |
65
|
|
|
|
66
|
|
|
return $modelClass::fromRow($row); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $criteria |
71
|
|
|
* |
72
|
|
|
* @return ModelInterface[]|array |
73
|
|
|
*/ |
74
|
|
|
public function findBy(array $criteria = []): array |
75
|
|
|
{ |
76
|
|
|
$qb = $this->connection->createQueryBuilder(); |
77
|
|
|
$qb->select('*')->from($this->getTablename()); |
78
|
|
|
|
79
|
|
View Code Duplication |
foreach ($criteria as $field => $value) { |
|
|
|
|
80
|
|
|
$qb->andWhere($qb->expr()->eq($field, ':'.$field)); |
81
|
|
|
$qb->setParameter($field, $value); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$rows = $qb->execute()->fetchAll(\PDO::FETCH_ASSOC); |
85
|
|
|
|
86
|
|
|
if ([] === $rows) { |
87
|
|
|
return []; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** @var ModelInterface $modelClass */ |
91
|
|
|
$modelClass = $this->getModelClass(); |
92
|
|
|
|
93
|
|
|
$models = []; |
94
|
|
|
foreach ($rows as $row) { |
95
|
|
|
$models[] = $modelClass::fromRow($row); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $models; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param ModelInterface $model |
103
|
|
|
*/ |
104
|
|
|
public function insert(ModelInterface $model) |
105
|
|
|
{ |
106
|
|
|
$this->connection->insert($this->getTablename(), $model->toRow()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param ModelInterface $model |
111
|
|
|
*/ |
112
|
|
|
public function update(ModelInterface $model) |
113
|
|
|
{ |
114
|
|
|
$this->connection->update($this->getTablename(), $model->toRow(), ['id' => $model->getId()]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param ModelInterface $model |
119
|
|
|
*/ |
120
|
|
|
public function delete(ModelInterface $model) |
121
|
|
|
{ |
122
|
|
|
$this->connection->delete($this->getTablename(), ['id' => $model->getId()]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
abstract protected function getTablename(): string; |
129
|
|
|
} |
130
|
|
|
|
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.