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

Engine::createDocumentStore()   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\Query\Parser;
6
use Mattbit\Flat\Query\Matcher;
7
use Mattbit\Flat\Document\Document;
8
use League\Flysystem\FilesystemInterface;
9
10
class Engine
11
{
12
    /**
13
     * @var FilesystemInterface
14
     */
15
    protected $filesystem;
16
17
    /**
18
     * @var Encoder
19
     */
20
    protected $encoder;
21
    
22
    public function __construct(FilesystemInterface $filesystem, JsonEncoder $encoder)
23
    {
24
        $this->filesystem = $filesystem;
25
        $this->encoder = $encoder;
0 ignored issues
show
Documentation Bug introduced by
It seems like $encoder of type object<Mattbit\Flat\Storage\JsonEncoder> is incompatible with the declared type object<Mattbit\Flat\Storage\Encoder> 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...
26
    }
27
28
    /**
29
     * Get the Filesystem instance.
30
     *
31
     * @return FilesystemInterface
32
     */
33
    public function getFilesystem()
34
    {
35
        return $this->filesystem;
36
    }
37
38
39
    /**
40
     * Get the EncoderInterface instance.
41
     *
42
     * @return EncoderInterface
43
     */
44
    public function getEncoder()
45
    {
46
        return $this->encoder;
47
    }
48
49
    /**
50
     * Create a new DocumentStore.
51
     *
52
     * @param string $namespace
53
     * @return DocumentStore
54
     */
55
    public function createDocumentStore($namespace)
56
    {
57
        return new DocumentStore($this, $namespace);
58
    }
59
60
    public function dropCollection($collection)
61
    {
62
        $this->filesystem->deleteDir($collection);
63
    }
64
65
    public function createCollection($collection)
66
    {
67
        $this->filesystem->createDir($collection);
68
    }
69
70
    public function truncateCollection($collection)
71
    {
72
        $this->dropCollection($collection);
73
        $this->createCollection($collection);
74
    }
75
76
    public function update($collection, array $criteria, array $updates, $multiple = false)
0 ignored issues
show
Unused Code introduced by
The parameter $multiple 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...
77
    {
78
        return $this->onMatch($collection, $criteria, function ($document) use ($updates) {
79
            $document = array_merge($document, $updates);
80
81
            return $this->insert($document);
0 ignored issues
show
Bug introduced by
The method insert() does not seem to exist on object<Mattbit\Flat\Storage\Engine>.

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...
82
        });
83
    }
84
85
    public function remove($collection, array $criteria, $multiple = false)
0 ignored issues
show
Unused Code introduced by
The parameter $multiple 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...
86
    {
87
        return $this->onMatch($collection, $criteria, function ($document, $path) {
88
            return $this->filesystem->delete($path);
89
        });
90
    }
91
92
    public function find($collection, array $criteria)
93
    {
94
        $results = [];
95
96
        $this->onMatch($collection, $criteria, function ($document) use (&$results) {
97
            $results[] = $document;
98
        }, true);
99
100
        return $results;
101
    }
102
103
    public function all($collection)
104
    {
105
        $results = [];
106
107
        foreach ($this->filesystem->listContents($collection) as $meta) {
108
            $data = $this->filesystem->read($meta['path']);
109
            $results[] = new Document($this->encoder->decode($data));
110
        }
111
112
        return $results;
113
    }
114
115
    protected function path($collection, $id)
116
    {
117
        return sprintf('%s/%s.%s', $collection, $id, Encoder::EXTENSION);
118
    }
119
120
    protected function onMatch($collection, $criteria, \Closure $closure, $multiple = false)
121
    {
122
        $count = 0;
123
124
        $expression = $this->parser->parse($criteria);
0 ignored issues
show
Bug introduced by
The property parser 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...
125
        $matcher = new Matcher($expression);
126
127
        foreach ($this->filesystem->listContents($collection) as $meta) {
128
            $data = $this->filesystem->read($meta['path']);
129
            $document = new Document($this->encoder->decode($data));
130
131
            if ($matcher->match($document)) {
132
                $count += (int) call_user_func_array($closure, [$document, $meta]);
133
134
                if (!$multiple) {
135
                    break;
136
                }
137
            }
138
        };
139
140
        return $count;
141
    }
142
}
143