Completed
Push — master ( b3acba...04c2f9 )
by Matteo
02:23
created

DocumentStore::scan()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
ccs 12
cts 12
cp 1
cc 6
eloc 10
nc 3
nop 2
crap 6
1
<?php
2
3
namespace Mattbit\Flat\Storage;
4
5
use Mattbit\Flat\Model\DocumentInterface;
6
use Mattbit\Flat\Storage\Filesystem\FilesystemCursor;
7
8
use Mattbit\Flat\Storage\EncoderInterface;
9
10
use Mattbit\Flat\Exception\DuplicateKeyException;
11
12
class DocumentStore implements \IteratorAggregate
13
{
14
    /**
15
     * @var EngineInterface
16
     */
17
    protected $engine;
18
19
    /**
20
     * @var string
21
     */
22
    protected $namespace;
23
24
    /**
25
     * Create a DocumentStore instance.
26
     *
27
     * @param EngineInterface $engine
28
     * @param string $namespace
29
     */
30 13
    public function __construct(EngineInterface $engine, $namespace)
31
    {
32 13
        $this->engine = $engine;
33 13
        $this->namespace = $namespace;
34 13
    }
35
36 1
    public function getNamespace()
37
    {
38 1
        return $this->namespace;
39
    }
40
41
    public function getEngine()
42
    {
43
        return $this->engine;
44
    }
45
46 1
    public function truncate()
47
    {
48 1
        return $this->engine->clear($this->namespace);
49
    }
50
51 3
    public function insert(DocumentInterface $document)
52
    {
53 3
        if (!$id = $document->getId()) {
54 1
            $id = $this->generateId();
55 1
            $document->setId($id);
56 1
        }
57
58 3
        if ($this->engine->has($id, $this->namespace)) {
59 1
            throw new DuplicateKeyException("Cannot insert document with duplicate key: {$id}");
60
        }
61
62 2
        $this->engine->put($document, $id, $this->namespace);
63
64 2
        return $id;
65
    }
66
67 2
    public function update(DocumentInterface $document)
68
    {
69 2
        if (!$id = $document->getId()) {
70 1
            throw new \Exception("Cannot update a document without _id!");
71
        }
72
73 1
        return $this->engine->put($document, $id, $this->namespace);
74
    }
75
76 1
    public function remove($id)
77
    {
78 1
        return $this->engine->delete($id, $this->namespace);
79
    }
80
81 1
    public function find($id)
82
    {
83 1
        return $this->engine->get($id, $this->namespace);
84
    }
85
86 3
    public function scan(callable $filter = null, $limit = null)
87
    {
88 3
        $documents = [];
89
90 3
        $index = 0;
91 3
        foreach ($this->engine->all($this->namespace) as $document) {
92 3
            if ($limit && $index >= $limit) {
93 1
                break;
94
            }
95 3
            $index += 1;
96
97 3
            if (!$filter || call_user_func($filter, $document)) {
98 3
                $documents[] = $document;
99 3
            }
100 3
        }
101
102 3
        return $documents;
103
    }
104
105
    public function insertDocument($namespace) {}
0 ignored issues
show
Unused Code introduced by
The parameter $namespace is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
    public function removeDocument($id) {}
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
    public function createCollection($collection) {}
0 ignored issues
show
Unused Code introduced by
The parameter $collection is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
    public function dropCollection($collection) {}
0 ignored issues
show
Unused Code introduced by
The parameter $collection is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
109
110 1
    protected function generateId()
111
    {
112
        // A simple uniqid should have enough entropy.
113
        // @todo: evaluate the usage of ramsey/uuid
114 1
        return uniqid();
115
    }
116
117
    public function getIterator()
118
    {
119
        return $this->engine->createCursor($this);
0 ignored issues
show
Bug introduced by
The method createCursor() does not seem to exist on object<Mattbit\Flat\Storage\EngineInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
    }
121
}
122