|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: jonas |
|
5
|
|
|
* Date: 27.10.15 |
|
6
|
|
|
* Time: 14:04 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace CultuurNet\UDB3\Offer\ReadModel\Permission\Doctrine; |
|
10
|
|
|
|
|
11
|
|
|
use CultuurNet\UDB3\Offer\ReadModel\Permission\PermissionQueryInterface; |
|
12
|
|
|
use Doctrine\DBAL\Connection; |
|
13
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
|
14
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
15
|
|
|
use CultuurNet\UDB3\Offer\ReadModel\Permission\PermissionRepositoryInterface; |
|
16
|
|
|
use ValueObjects\String\String; |
|
17
|
|
|
|
|
18
|
|
|
class DBALRepository implements PermissionRepositoryInterface, PermissionQueryInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Connection |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $connection; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param String $tableName |
|
27
|
|
|
* @param Connection $connection |
|
28
|
|
|
* @param String $idField |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(String $tableName, Connection $connection, String $idField) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->tableName = $tableName; |
|
|
|
|
|
|
33
|
|
|
$this->connection = $connection; |
|
34
|
|
|
$this->idField = $idField; |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inheritdoc |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getEditableOffers(String $uitId) |
|
41
|
|
|
{ |
|
42
|
|
|
$q = $this->connection->createQueryBuilder(); |
|
43
|
|
|
$q->select($this->idField->toNative()) |
|
44
|
|
|
->from($this->tableName->toNative()) |
|
45
|
|
|
->where('user_id = :userId') |
|
46
|
|
|
->setParameter(':userId', $uitId->toNative()); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
$results = $q->execute(); |
|
49
|
|
|
|
|
50
|
|
|
$events = array(); |
|
51
|
|
|
while ($id = $results->fetchColumn(0)) { |
|
52
|
|
|
$events[] = new String($id); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $events; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritdoc |
|
60
|
|
|
*/ |
|
61
|
|
|
public function markOfferEditableByUser(String $eventId, String $uitId) |
|
62
|
|
|
{ |
|
63
|
|
|
try { |
|
64
|
|
|
$this->connection->insert( |
|
65
|
|
|
$this->tableName->toNative(), |
|
66
|
|
|
[ |
|
67
|
|
|
$this->idField->toNative() => $eventId->toNative(), |
|
|
|
|
|
|
68
|
|
|
'user_id' => $uitId->toNative() |
|
|
|
|
|
|
69
|
|
|
] |
|
70
|
|
|
); |
|
71
|
|
|
} catch (UniqueConstraintViolationException $e) { |
|
72
|
|
|
// Intentionally catching database exception occurring when the |
|
73
|
|
|
// permission record is already in place. |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: