Collection::reset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\yaysondb;
6
7
use hanneskod\yaysondb\Engine\EngineInterface;
8
use hanneskod\yaysondb\Expression\ExpressionInterface;
9
10
/**
11
 * Access class for a collection of documents
12
 */
13
class Collection extends Filterable implements CollectionInterface
14
{
15
    /**
16
     * @var EngineInterface
17
     */
18
    private $engine;
19
20
    public function __construct(EngineInterface $engine)
21
    {
22
        parent::__construct($engine);
23
        $this->engine = $engine;
24
    }
25
26
    public function has(string $id): bool
27
    {
28
        return $this->engine->has($id);
29
    }
30
31
    public function read(string $id): array
32
    {
33
        if (!$this->has($id)) {
34
            throw new Exception\LogicException("Document $id does not exist, did you call has()?");
35
        }
36
37
        return $this->engine->read($id);
38
    }
39
40
    public function insert(array $document): string
41
    {
42
        return $this->engine->write('', $document);
43
    }
44
45
    public function update(ExpressionInterface $expression, array $newDocument): int
46
    {
47
        $count = 0;
48
49
        foreach ($this->find($expression) as $id => $oldDocument) {
50
            $this->engine->write((string)$id, array_replace_recursive($oldDocument, $newDocument));
51
            $count++;
52
        }
53
54
        return $count;
55
    }
56
57
    public function delete(ExpressionInterface $expression): int
58
    {
59
        $count = 0;
60
61
        foreach ($this->find($expression) as $id => $document) {
62
            if ($this->engine->delete((string)$id)) {
63
                $count++;
64
            }
65
        }
66
67
        return $count;
68
    }
69
70
    public function commit()
71
    {
72
        return $this->engine->commit();
73
    }
74
75
    public function inTransaction(): bool
76
    {
77
        return $this->engine->inTransaction();
78
    }
79
80
    public function reset()
81
    {
82
        return $this->engine->reset();
83
    }
84
}
85