Completed
Push — master ( 5a0a25...383c62 )
by Matteo
02:38
created

DocumentStore::extractId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
ccs 0
cts 4
cp 0
cc 2
eloc 3
nc 2
nop 1
crap 6
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
     * Create a RecordStore instance.
27
     *
28
     * @param Engine $engine
29
     * @param string $namespace
30
     */
31
    public function __construct(Engine $engine, $namespace)
32
    {
33
        $this->engine = $engine;
34
        $this->namespace = $namespace;
35
        $this->filesystem = $this->engine->getFilesystem();
0 ignored issues
show
Bug introduced by
The property filesystem does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
        $this->encoder = $this->engine->getEncoder();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->engine->getEncoder() of type object<Mattbit\Flat\Storage\Encoder> is incompatible with the declared type object<Mattbit\Flat\Storage\EncoderInterface> of property $encoder.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
    }
38
39
    public function truncate()
40
    {
41
        $files = $this->filesystem->listContents($this->namespace);
42
43
        foreach ($files as $file) {
44
            $this->filesystem->delete($file['path']);
45
        }
46
47
        return true;
48
    }
49
50
    public function insertDocument(Identifiable $document)
51
    {
52
        $id = $document->getId() ?: $this->generateId();
53
        $path = $this->path($id);
54
        $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...
55
56
        if ($this->filesystem->has($this->path($id))) {
57
            throw new \Exception("Duplicate _id: {$id}");
58
        }
59
60
61
        $this->filesystem->put($path, $data);
62
63
        return $id;
64
    }
65
66
    public function insertDocuments(array $documents)
67
    {
68
        $ids = [];
69
70
        foreach ($documents as $document) {
71
            $ids[] = $this->insertDocument($document);
72
        }
73
74
        return $ids;
75
    }
76
77
    public function updateDocument(Identifiable $document)
78
    {
79
        if (!$id = $document->getId()) {
80
            throw new \Exception("Cannot update a document without _id!");
81
        }
82
83
        $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...
84
        $path = $this->path($id);
85
86
        return $this->filesystem->put($path, $data);
87
    }
88
89
    public function removeDocument($documentId)
90
    {
91
        $path = $this->path($documentId);
92
93
        return $this->filesystem->delete($path);
94
    }
95
96
    public function removeDocuments(array $documentIds)
97
    {
98
        foreach ($documentIds as $documentId) {
99
            $this->removeDocument($documentId);
100
        }
101
102
        return true;
103
    }
104
105
    public function findDocument($documentId)
106
    {
107
        $path = $this->path($documentId);
108
        $data = $this->filesystem->read($path);
109
110
        return $this->encoder->decode($data);
111
    }
112
113
    public function scanDocuments(callable $filter = null, $limit = null)
114
    {
115
        $files = $this->filesystem->listContents($this->namespace);
116
        $documents = [];
117
118
        foreach ($files as $index => $file) {
119
            if ($limit && $index > $limit) {
120
                break;
121
            }
122
123
            $data = $this->filesystem->read($file['path']);
124
            $document = $this->encoder->decode($data);
125
126
            if (!$filter || call_user_func($filter, $document)) {
127
                $documents[] = $document;
128
            }
129
        }
130
131
        return $documents;
132
    }
133
134
    protected function path($id)
135
    {
136
        return sprintf('%s/%s.%s', $this->namespace, $id, $this->encoder->getExtension());
137
    }
138
139
    protected function extractId($record)
140
    {
141
        if (isset($record['_id'])) {
142
            return $record['_id'];
143
        }
144
    }
145
146
    protected function generateId()
147
    {
148
        // A simple uniqid should have enough entropy.
149
        return uniqid();
150
    }
151
152
    protected function newDocument($attributes)
153
    {
154
        return new Document($attributes);
155
    }
156
}
157