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

LogEngine   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 110
c 0
b 0
f 0
wmc 15
lcom 1
cbo 3
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getId() 0 4 1
A getIterator() 0 8 2
A write() 0 9 2
A inTransaction() 0 4 1
A reset() 0 4 1
A commit() 0 11 2
A has() 0 4 1
A read() 0 4 1
A delete() 0 4 1
A clear() 0 4 1
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