Completed
Push — master ( d8757f...fa9ad1 )
by Matteo
02:59
created

DocumentStore::newDocument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 2
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Mattbit\Flat\Storage;
4
5
use Mattbit\Flat\Document\Document;
6
use Mattbit\Flat\Document\Identifiable;
7
8
class DocumentStore
9
{
10
    /**
11
     * @var Engine
12
     */
13
    protected $engine;
14
15
    /**
16
     * @var EncoderInterface
17
     */
18
    protected $encoder;
19
20
    /**
21
     * @var string
22
     */
23
    protected $namespace;
24
25
    /**
26
     * @var \League\Flysystem\FilesystemInterface
27
     */
28
    protected $filesystem;
29
30
    /**
31
     * Create a RecordStore instance.
32
     *
33
     * @param Engine $engine
34
     * @param string $namespace
35
     */
36 14
    public function __construct(Engine $engine, $namespace)
37
    {
38 14
        $this->engine = $engine;
39 14
        $this->namespace = $namespace;
40 14
        $this->filesystem = $this->engine->getFilesystem();
41 14
        $this->encoder = $this->engine->getEncoder();
42 14
    }
43
44 2
    public function getNamespace()
45
    {
46 2
        return $this->namespace;
47
    }
48
49 1
    public function truncate()
50
    {
51 1
        $files = $this->filesystem->listContents($this->namespace);
52
53 1
        foreach ($files as $file) {
54 1
            $this->filesystem->delete($file['path']);
55 1
        }
56
57 1
        return true;
58
    }
59
60 3
    public function insertDocument(Identifiable $document)
61
    {
62 3
        $id = $document->getId() ?: $this->generateId();
63 3
        $path = $this->path($id);
64
65 3
        if ($this->filesystem->has($this->path($id))) {
66 1
            throw new \Exception("Duplicate _id: {$id}");
67
        }
68
69 2
        $data = $this->encoder->encode($document);
0 ignored issues
show
Documentation introduced by
$document is of type object<Mattbit\Flat\Document\Identifiable>, but the function expects a object<Mattbit\Flat\Document\Encodable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70 2
        $this->filesystem->put($path, $data);
71
72 2
        return $id;
73
    }
74
75 2
    public function updateDocument(Identifiable $document)
76
    {
77 2
        if (!$id = $document->getId()) {
78 1
            throw new \Exception("Cannot update a document without _id!");
79
        }
80
81 1
        $data = $this->encoder->encode($document);
0 ignored issues
show
Documentation introduced by
$document is of type object<Mattbit\Flat\Document\Identifiable>, but the function expects a object<Mattbit\Flat\Document\Encodable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82 1
        $path = $this->path($id);
83
84 1
        return $this->filesystem->put($path, $data);
85
    }
86
87 1
    public function removeDocument($documentId)
88
    {
89 1
        $path = $this->path($documentId);
90
91 1
        return $this->filesystem->delete($path);
92
    }
93
94 1
    public function findDocument($documentId)
95
    {
96 1
        $path = $this->path($documentId);
97 1
        $data = $this->filesystem->read($path);
98
99 1
        return $this->encoder->decode($data);
100
    }
101
102 3
    public function scanDocuments(callable $filter = null, $limit = null)
103
    {
104 3
        $files = $this->filesystem->listContents($this->namespace);
105 3
        $documents = [];
106
107 3
        foreach ($files as $index => $file) {
108 3
            if ($limit && $index >= $limit) {
109 1
                break;
110
            }
111
112 3
            $data = $this->filesystem->read($file['path']);
113 3
            $document = $this->encoder->decode($data);
114
115 3
            if (!$filter || call_user_func($filter, $document)) {
116 3
                $documents[] = $document;
117 3
            }
118 3
        }
119
120 3
        return $documents;
121
    }
122
123 6
    protected function path($id)
124
    {
125 6
        return sprintf('%s/%s.%s', $this->namespace, $id, $this->encoder->getExtension());
126
    }
127
128 1
    protected function generateId()
129
    {
130
        // A simple uniqid should have enough entropy.
131 1
        return uniqid();
132
    }
133
}
134