Completed
Pull Request — master (#294)
by Luc
05:40
created

DBALReadRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 63.49 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 40
loc 63
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLabelRelations() 17 17 2
A getLabelRelationsForItem() 23 23 2
A getAliases() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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