|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of byrokrat\giroapp. |
|
5
|
|
|
* |
|
6
|
|
|
* byrokrat\giroapp is free software: you can redistribute it and/or |
|
7
|
|
|
* modify it under the terms of the GNU General Public License as published |
|
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* byrokrat\giroapp is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU General Public License |
|
17
|
|
|
* along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>. |
|
18
|
|
|
* |
|
19
|
|
|
* Copyright 2016-21 Hannes Forsgård |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
declare(strict_types=1); |
|
23
|
|
|
|
|
24
|
|
|
namespace byrokrat\giroapp\Db\Json; |
|
25
|
|
|
|
|
26
|
|
|
use byrokrat\giroapp\Db\DonorEventStoreInterface; |
|
27
|
|
|
use byrokrat\giroapp\Db\DonorEventEntry; |
|
28
|
|
|
use hanneskod\yaysondb\CollectionInterface; |
|
29
|
|
|
use hanneskod\yaysondb\Operators as y; |
|
30
|
|
|
|
|
31
|
|
|
final class JsonDonorEventStore implements DonorEventStoreInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var CollectionInterface&iterable<array> */ |
|
34
|
|
|
private $collection; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param CollectionInterface&iterable<array> $collection |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(CollectionInterface $collection) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->collection = $collection; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function addDonorEventEntry(DonorEventEntry $entry): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->collection->insert( |
|
47
|
|
|
[ |
|
48
|
|
|
'key' => $entry->getMandateKey(), |
|
49
|
|
|
'type' => $entry->getType(), |
|
50
|
|
|
'time' => $entry->getDateTime()->format(\DateTime::W3C), |
|
51
|
|
|
'data' => $entry->getData() |
|
52
|
|
|
] |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function readEntriesForMandateKey(string $mandateKey): iterable |
|
57
|
|
|
{ |
|
58
|
|
|
foreach ($this->collection->find(y::doc(['key' => y::equals($mandateKey)])) as $data) { |
|
59
|
|
|
yield new DonorEventEntry( |
|
60
|
|
|
$data['key'] ?? '', |
|
61
|
|
|
$data['type'] ?? '', |
|
62
|
|
|
new \DateTimeImmutable($data['time'] ?? ''), |
|
63
|
|
|
$data['data'] ?? [] |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function readAllEntries(): iterable |
|
69
|
|
|
{ |
|
70
|
|
|
foreach ($this->collection as $data) { |
|
71
|
|
|
yield new DonorEventEntry( |
|
72
|
|
|
$data['key'] ?? '', |
|
73
|
|
|
$data['type'] ?? '', |
|
74
|
|
|
new \DateTimeImmutable($data['time'] ?? ''), |
|
75
|
|
|
$data['data'] ?? [] |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|