1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Label\ReadModels\Relations\Repository\Doctrine; |
4
|
|
|
|
5
|
|
|
use CultuurNet\UDB3\Label\ReadModels\Doctrine\AbstractDBALRepository; |
6
|
|
|
use CultuurNet\UDB3\Label\ReadModels\Relations\Repository\LabelRelation; |
7
|
|
|
use CultuurNet\UDB3\Label\ReadModels\Relations\Repository\ReadRepositoryInterface; |
8
|
|
|
use CultuurNet\UDB3\Label\ValueObjects\LabelName; |
9
|
|
|
use ValueObjects\StringLiteral\StringLiteral; |
10
|
|
|
|
11
|
|
|
class DBALReadRepository extends AbstractDBALRepository implements ReadRepositoryInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @inheritdoc |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
public function getLabelRelations(LabelName $labelName) |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
$aliases = $this->getAliases(); |
19
|
|
|
$whereLabelName = SchemaConfigurator::LABEL_NAME . ' = ?'; |
20
|
|
|
|
21
|
|
|
$queryBuilder = $this->createQueryBuilder()->select($aliases) |
22
|
|
|
->from($this->getTableName()->toNative()) |
23
|
|
|
->where($whereLabelName) |
24
|
|
|
->setParameters([$labelName->toNative()]); |
25
|
|
|
|
26
|
|
|
$statement = $queryBuilder->execute(); |
27
|
|
|
|
28
|
|
|
while ($row = $statement->fetch(\PDO::FETCH_ASSOC)) { |
29
|
|
|
$labelRelation = LabelRelation::fromRelationalData($row); |
30
|
|
|
yield $labelRelation; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function getLabelRelationsForItem(StringLiteral $relationId) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$aliases = $this->getAliases(); |
40
|
|
|
$whereRelationId = SchemaConfigurator::RELATION_ID . ' = ?'; |
41
|
|
|
|
42
|
|
|
$queryBuilder = $this->createQueryBuilder()->select($aliases) |
43
|
|
|
->from($this->getTableName()->toNative()) |
44
|
|
|
->where($whereRelationId) |
45
|
|
|
->setParameters( |
46
|
|
|
[ |
47
|
|
|
$relationId->toNative(), |
48
|
|
|
] |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$statement = $queryBuilder->execute(); |
52
|
|
|
|
53
|
|
|
$labelRelations = []; |
54
|
|
|
while ($row = $statement->fetch(\PDO::FETCH_ASSOC)) { |
55
|
|
|
$labelRelations[] = LabelRelation::fromRelationalData($row); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $labelRelations; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
private function getAliases() |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
SchemaConfigurator::LABEL_NAME, |
68
|
|
|
SchemaConfigurator::RELATION_TYPE, |
69
|
|
|
SchemaConfigurator::RELATION_ID, |
70
|
|
|
SchemaConfigurator::IMPORTED |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
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.