JsonDriver   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 51
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDonorRepository() 0 7 1
A getDonorEventStore() 0 3 1
A rollback() 0 8 2
A getImportHistory() 0 5 1
A __construct() 0 3 1
A commit() 0 8 2
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\DriverInterface;
27
use byrokrat\giroapp\Db\DriverEnvironment;
28
use byrokrat\giroapp\Db\DonorEventStoreInterface;
29
use byrokrat\giroapp\Db\DonorRepositoryInterface;
30
use byrokrat\giroapp\Db\ImportHistoryInterface;
31
use hanneskod\yaysondb\Yaysondb;
32
33
final class JsonDriver implements DriverInterface
34
{
35
    /** @var Yaysondb */
36
    private $db;
37
38
    public function __construct(Yaysondb $db)
39
    {
40
        $this->db = $db;
41
    }
42
43
    public function getDonorEventStore(DriverEnvironment $environment): DonorEventStoreInterface
44
    {
45
        return new JsonDonorEventStore($this->db->collection('donor_events'));
46
    }
47
48
    public function getDonorRepository(DriverEnvironment $environment): DonorRepositoryInterface
49
    {
50
        return new JsonDonorRepository(
51
            $this->db->collection('donors'),
52
            $environment->getDonorFactory(),
53
            $environment->getClock(),
54
            $environment->getMoneyFormatter()
55
        );
56
    }
57
58
    public function getImportHistory(DriverEnvironment $environment): ImportHistoryInterface
59
    {
60
        return new JsonImportHistory(
61
            $this->db->collection('imports'),
62
            $environment->getClock()
63
        );
64
    }
65
66
    public function commit(): bool
67
    {
68
        if ($this->db->inTransaction()) {
69
            $this->db->commit();
70
            return true;
71
        }
72
73
        return false;
74
    }
75
76
    public function rollback(): bool
77
    {
78
        if ($this->db->inTransaction()) {
79
            $this->db->reset();
80
            return true;
81
        }
82
83
        return false;
84
    }
85
}
86