Completed
Pull Request — master (#294)
by Luc
09:31 queued 05:01
created

DBALReadRepository::getLabelRelationsForItem()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 2
nop 2
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 CultuurNet\UDB3\Label\ValueObjects\RelationType;
10
use ValueObjects\StringLiteral\StringLiteral;
11
12
class DBALReadRepository extends AbstractDBALRepository implements ReadRepositoryInterface
13
{
14
    /**
15
     * @inheritdoc
16
     */
17
    public function getLabelRelations(LabelName $labelName)
18
    {
19
        $aliases = $this->getAliases();
20
        $whereLabelName = SchemaConfigurator::LABEL_NAME . ' = ?';
21
22
        $queryBuilder = $this->createQueryBuilder()->select($aliases)
23
            ->from($this->getTableName()->toNative())
24
            ->where($whereLabelName)
25
            ->setParameters([$labelName->toNative()]);
26
27
        $statement = $queryBuilder->execute();
28
29
        while ($row = $statement->fetch(\PDO::FETCH_ASSOC)) {
30
            $labelRelation = LabelRelation::fromRelationalData($row);
31
            yield $labelRelation;
32
        }
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function getLabelRelationsForItem(
39
        RelationType $relationType,
40
        StringLiteral $relationId
41
    ) {
42
        $aliases = $this->getAliases();
43
        $whereRelationType = SchemaConfigurator::RELATION_TYPE . ' = ?';
44
        $whereRelationId = SchemaConfigurator::RELATION_ID . ' = ?';
45
46
        $queryBuilder = $this->createQueryBuilder()->select($aliases)
47
            ->from($this->getTableName()->toNative())
48
            ->where($whereRelationType)
49
            ->andWhere($whereRelationId)
50
            ->setParameters(
51
                [
52
                    $relationType->toNative(),
53
                    $relationId->toNative(),
54
                ]
55
            );
56
57
        $statement = $queryBuilder->execute();
58
59
        $labelRelations = [];
60
        while ($row = $statement->fetch(\PDO::FETCH_ASSOC)) {
61
            $labelRelations[] = LabelRelation::fromRelationalData($row);
62
        }
63
64
        return $labelRelations;
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    private function getAliases()
71
    {
72
        return [
73
            SchemaConfigurator::LABEL_NAME,
74
            SchemaConfigurator::RELATION_TYPE,
75
            SchemaConfigurator::RELATION_ID,
76
            SchemaConfigurator::IMPORTED
77
        ];
78
    }
79
}
80