Completed
Push — 2.0 ( 9c7316...737838 )
by Hannes
03:08
created

Yaysondb::reset()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\yaysondb;
6
7
/**
8
 * Handle to a defined set of collections
9
 */
10
class Yaysondb implements TransactableInterface
11
{
12
    /**
13
     * @var Collection[] Loaded collections
14
     */
15
    private $collections;
16
17
    /**
18
     * Load collections
19
     *
20
     * @param CollectionInterface[] $collections Map of identifiers to collections
21
     */
22
    public function __construct(array $collections)
23
    {
24
        $this->collections = $collections;
0 ignored issues
show
Documentation Bug introduced by
It seems like $collections of type array<integer,object<han...b\CollectionInterface>> is incompatible with the declared type array<integer,object<han...d\yaysondb\Collection>> of property $collections.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
25
    }
26
27
    /**
28
     * Get collection
29
     *
30
     * @throws Exception\LogicException If id is not defined
31
     */
32
    public function collection(string $id): CollectionInterface
33
    {
34
        if (!isset($this->collections[$id])) {
35
            throw new Exception\LogicException("Trying to access undefined collection $id");
36
        }
37
38
        return $this->collections[$id];
39
    }
40
41
    /**
42
     * Magic method to allow collection access through property name
43
     */
44
    public function __get(string $id): CollectionInterface
45
    {
46
        return $this->collection($id);
47
    }
48
49
    public function commit()
50
    {
51
        foreach ($this->collections as $collection) {
52
            if ($collection->inTransaction()) {
53
                $collection->commit();
54
            }
55
        }
56
    }
57
58
    public function inTransaction(): bool
59
    {
60
        foreach ($this->collections as $collection) {
61
            if ($collection->inTransaction()) {
62
                return true;
63
            }
64
        }
65
66
        return false;
67
    }
68
69
    public function reset()
70
    {
71
        foreach ($this->collections as $collection) {
72
            if ($collection->inTransaction()) {
73
                $collection->reset();
74
            }
75
        }
76
    }
77
}
78