Completed
Pull Request — master (#305)
by Luc
05:25
created

DBALWriteRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 31.03 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 27
loc 87
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 23 2
A deleteByRelationId() 0 9 1
A deleteByLabelNameAndRelationId() 12 12 1
A deleteImportedByRelationId() 15 15 1
A executeTransactional() 0 6 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\WriteRepositoryInterface;
7
use CultuurNet\UDB3\Label\ValueObjects\LabelName;
8
use CultuurNet\UDB3\Label\ValueObjects\RelationType;
9
use Doctrine\DBAL\Query\QueryBuilder;
10
use ValueObjects\StringLiteral\StringLiteral;
11
12
class DBALWriteRepository extends AbstractDBALRepository implements WriteRepositoryInterface
13
{
14
    /**
15
     * @inheritdoc
16
     */
17
    public function save(
18
        LabelName $labelName,
19
        RelationType $relationType,
20
        StringLiteral $relationId,
21
        $imported
22
    ) {
23
        $queryBuilder = $this->createQueryBuilder()
24
            ->insert($this->getTableName())
25
            ->values([
26
                SchemaConfigurator::LABEL_NAME => '?',
27
                SchemaConfigurator::RELATION_TYPE => '?',
28
                SchemaConfigurator::RELATION_ID => '?',
29
                SchemaConfigurator::IMPORTED => '?',
30
            ])
31
            ->setParameters([
32
                $labelName->toNative(),
33
                $relationType->toNative(),
34
                $relationId->toNative(),
35
                $imported ? 1 : 0,
36
            ]);
37
38
        $this->executeTransactional($queryBuilder);
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44 View Code Duplication
    public function deleteByLabelNameAndRelationId(
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...
45
        LabelName $labelName,
46
        StringLiteral $relationId
47
    ) {
48
        $queryBuilder = $this->createQueryBuilder()
49
            ->delete($this->getTableName())
50
            ->where(SchemaConfigurator::LABEL_NAME . ' = ?')
51
            ->andWhere(SchemaConfigurator::RELATION_ID . ' = ?')
52
            ->setParameters([$labelName->toNative(), $relationId->toNative()]);
53
54
        $this->executeTransactional($queryBuilder);
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function deleteByRelationId(StringLiteral $relationId)
61
    {
62
        $queryBuilder = $this->createQueryBuilder()
63
            ->delete($this->getTableName())
64
            ->where(SchemaConfigurator::RELATION_ID . ' = ?')
65
            ->setParameters([$relationId->toNative()]);
66
67
        $this->executeTransactional($queryBuilder);
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 View Code Duplication
    public function deleteImportedByRelationId(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...
74
    {
75
        $queryBuilder = $this->createQueryBuilder()
76
            ->delete($this->getTableName())
77
            ->where(SchemaConfigurator::RELATION_ID . ' = :relationId')
78
            ->andWhere(SchemaConfigurator::IMPORTED . ' = :imported')
79
            ->setParameters(
80
                [
81
                    ':relationId' => $relationId->toNative(),
82
                    ':imported' => true,
83
                ]
84
            );
85
86
        $this->executeTransactional($queryBuilder);
87
    }
88
89
    /**
90
     * @param QueryBuilder $queryBuilder
91
     */
92
    private function executeTransactional(QueryBuilder $queryBuilder)
93
    {
94
        $this->getConnection()->transactional(function () use ($queryBuilder) {
95
            $queryBuilder->execute();
96
        });
97
    }
98
}
99