Completed
Push — master ( df9952...f86661 )
by Hannes
04:17 queued 02:11
created

LogEngine::inTransaction()   A

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\Engine;
6
7
use hanneskod\yaysondb\Exception\LogicException;
8
9
/**
10
 * Simple engine for logging purposes
11
 */
12
class LogEngine implements EngineInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    private $fname;
18
19
    /**
20
     * @var resource Log stream
21
     */
22
    private $stream;
23
24
    /**
25
     * @var DecoderInterface Decoder used to encode and deconde content
26
     */
27
    private $decoder;
28
29
    /**
30
     * @var array List of non-commited documents
31
     */
32
    private $newDocs = [];
33
34
    public function __construct(string $fname, DecoderInterface $decoder = null)
35
    {
36
        $this->fname = $fname;
37
        $this->stream = fopen($fname, 'a+');
38
        $this->decoder = $decoder ?: new SerializingDecoder;
39
    }
40
41
    public function getId(): string
42
    {
43
        return $this->fname;
44
    }
45
46
    public function getIterator(): \Generator
47
    {
48
        rewind($this->stream);
49
50
        while ($line = fgets($this->stream)) {
51
            yield $this->decoder->decode($line);
52
        }
53
    }
54
55
    /**
56
     * @throws LogicException If id is specified
57
     */
58
    public function write(string $id, array $doc): string
59
    {
60
        if ($id) {
61
            throw new LogicException('Specifying a document id at write is not supported by LogEngine');
62
        }
63
64
        $this->newDocs[] = $doc;
65
        return '';
66
    }
67
68
    public function inTransaction(): bool
69
    {
70
        return !empty($this->newDocs);
71
    }
72
73
    public function reset()
74
    {
75
        $this->newDocs = [];
76
    }
77
78
    public function commit()
79
    {
80
        foreach ($this->newDocs as $doc) {
81
            fwrite(
82
                $this->stream,
83
                $this->decoder->encode($doc) . PHP_EOL
84
            );
85
        }
86
87
        $this->reset();
88
    }
89
90
    /**
91
     * @throws LogicException Not supported by LogEngine
92
     */
93
    public function has(string $id): bool
94
    {
95
        throw new LogicException('has() is not supported by LogEngine');
96
    }
97
98
    /**
99
     * @throws LogicException Not supported by LogEngine
100
     */
101
    public function read(string $id): array
102
    {
103
        throw new LogicException('read() is not supported by LogEngine');
104
    }
105
106
    /**
107
     * @throws LogicException Not supported by LogEngine
108
     */
109
    public function delete(string $id): bool
110
    {
111
        throw new LogicException('delete() is not supported by LogEngine');
112
    }
113
114
    /**
115
     * @throws LogicException Not supported by LogEngine
116
     */
117
    public function clear()
118
    {
119
        throw new LogicException('clear() is not supported by LogEngine');
120
    }
121
}
122