Completed
Pull Request — master (#392)
by Luc
51:23 queued 32:05
created

UDB3SavedSearchRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
namespace CultuurNet\UDB3\SavedSearches;
4
5
use Broadway\UuidGenerator\UuidGeneratorInterface;
6
use CultuurNet\UDB3\SavedSearches\Doctrine\SchemaConfigurator;
7
use CultuurNet\UDB3\SavedSearches\Properties\QueryString;
8
use CultuurNet\UDB3\SavedSearches\ReadModel\SavedSearch;
9
use CultuurNet\UDB3\SavedSearches\ReadModel\SavedSearchRepositoryInterface as SavedSearchReadModelRepositoryInterface;
10
use CultuurNet\UDB3\SavedSearches\WriteModel\SavedSearchRepositoryInterface as SavedSearchWriteModelRepositoryInterface;
11
use Doctrine\DBAL\Connection;
12
use ValueObjects\StringLiteral\StringLiteral;
13
14
class UDB3SavedSearchRepository implements SavedSearchReadModelRepositoryInterface, SavedSearchWriteModelRepositoryInterface
15
{
16
    /**
17
     * @var Connection
18
     */
19
    private $connection;
20
21
    /**
22
     * @var StringLiteral
23
     */
24
    private $tableName;
25
26
    /**
27
     * @var UuidGeneratorInterface
28
     */
29
    private $uuidGenerator;
30
31
    /**
32
     * @var StringLiteral
33
     */
34
    private $userId;
35
36
    /**
37
     * @param Connection $connection
38
     * @param StringLiteral $tableName
39
     * @param UuidGeneratorInterface $uuidGenerator
40
     * @param StringLiteral $userId
41
     */
42
    public function __construct(
43
        Connection $connection,
44
        StringLiteral $tableName,
45
        UuidGeneratorInterface $uuidGenerator,
46
        StringLiteral $userId
47
    ) {
48
        $this->connection = $connection;
49
        $this->tableName = $tableName;
50
        $this->uuidGenerator = $uuidGenerator;
51
        $this->userId = $userId;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function write(
58
        StringLiteral $userId,
59
        StringLiteral $name,
60
        QueryString $queryString
61
    ): void {
62
        $queryBuilder = $this->connection->createQueryBuilder()
63
            ->insert($this->tableName->toNative())
64
            ->values(
65
                [
66
                    SchemaConfigurator::ID => '?',
67
                    SchemaConfigurator::USER => '?',
68
                    SchemaConfigurator::NAME => '?',
69
                    SchemaConfigurator::QUERY => '?',
70
                ]
71
            )
72
            ->setParameters(
73
                [
74
                    $this->uuidGenerator->generate(),
75
                    $userId->toNative(),
76
                    $name->toNative(),
77
                    $queryString->toNative(),
78
                ]
79
            );
80
81
        $queryBuilder->execute();
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function delete(
88
        StringLiteral $userId,
89
        StringLiteral $searchId
90
    ): void {
91
        $queryBuilder = $this->connection->createQueryBuilder()
92
            ->delete($this->tableName->toNative())
93
            ->where(SchemaConfigurator::USER . ' = ?')
94
            ->andWhere(SchemaConfigurator::ID . ' = ?')
95
            ->setParameters(
96
                [
97
                    $userId->toNative(),
98
                    $searchId->toNative(),
99
                ]
100
            );
101
102
        $queryBuilder->execute();
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function ownedByCurrentUser(): array
109
    {
110
        $queryBuilder = $this->connection->createQueryBuilder()
111
            ->select('*')
112
            ->from($this->tableName->toNative())
113
            ->where(SchemaConfigurator::USER . ' = ?')
114
            ->setParameters(
115
                [
116
                    $this->userId->toNative(),
117
                ]
118
            );
119
120
        $statement = $queryBuilder->execute();
121
122
        $savedSearches = [];
123
124
        while ($row = $statement->fetch(\PDO::FETCH_ASSOC)) {
125
            $savedSearches[] = new SavedSearch(
126
                new StringLiteral($row[SchemaConfigurator::NAME]),
127
                new QueryString($row[SchemaConfigurator::QUERY]),
128
                new StringLiteral($row[SchemaConfigurator::ID])
129
            );
130
        }
131
132
133
        return $savedSearches;
134
    }
135
}
136