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

DBALWriteRepository::deleteImportedByRelationId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
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