Completed
Push — master ( fa9ad1...efd659 )
by Matteo
02:46
created

Collection::insert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Mattbit\Flat;
4
5
use Mattbit\Flat\Query\Matcher;
6
use Mattbit\Flat\Document\Identifiable;
7
use Mattbit\Flat\Query\Parser;
8
use Mattbit\Flat\Storage\DocumentStore;
9
use Mattbit\Flat\Query\Expression\ExpressionInterface;
10
11
class Collection
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $name;
17
18
    /**
19
     * @var DocumentStore
20
     */
21
    protected $store;
22
23
    /**
24
     * @var Database
25
     */
26
    protected $database;
27
28
    /**
29
     * @var Parser
30
     */
31
    protected $parser;
32
33
    /**
34
     * Construct a new collection.
35
     *
36
     * @param Database      $database
37
     * @param DocumentStore $store
38
     * @param string        $name
39
     */
40 6
    public function __construct(Database $database, DocumentStore $store, $name)
41
    {
42 6
        $this->name = $name;
43 6
        $this->store = $store;
44 6
        $this->database = $database;
45 6
        $this->parser = $database->getParser();
46 6
    }
47
48
    /**
49
     * Drop the collection.
50
     *
51
     * @return bool
52
     */
53 1
    public function drop()
54
    {
55 1
        return $this->database->dropCollection($this->name);
56
    }
57
58
    /**
59
     * Remove all the documents from the collection.
60
     *
61
     * @return bool
62
     */
63 1
    public function truncate()
64
    {
65 1
        $this->store->truncate();
66 1
    }
67
68
    /**
69
     * Insert a new document.
70
     *
71
     * @param Identifiable $document
72
     *
73
     * @return bool
74
     */
75 1
    public function insert(Identifiable $document)
76
    {
77 1
        return $this->store->insertDocument($document);
78
    }
79
80
    /**
81
     * Update existing documents.
82
     *
83
     * @param array        $criteria
84
     * @param Identifiable $updated
85
     * @param bool         $multiple
86
     *
87
     * @return int The count of the documents updated.
88
     */
89
    public function update($criteria, Identifiable $updated, $multiple = false)
90
    {
91
        $limit = $multiple ? 1 : null;
92
        $documents = $this->onMatch($criteria, $limit);
93
94
        foreach ($documents as $document) {
95
            $this->store->updateDocument($updated);
96
        }
97
    }
98
99
    /**
100
     * Remove documents from the collection.
101
     *
102
     * @param mixed $criteria
103
     * @param bool  $multiple
104
     *
105
     * @return int The count of the document deleted.
106
     */
107 1
    public function remove($criteria, $multiple = false)
108
    {
109 1
        $limit = $multiple ? 1 : null;
110
111 1
        $documents = $this->onMatch($criteria, $limit);
112
        
113 1
        foreach ($documents as $document) {
114 1
            $this->store->removeDocument($document->getId());
115 1
        }
116
117 1
        return true;
118
    }
119
120
    /**
121
     * Find documents in the collection.
122
     *
123
     * @param array $criteria
124
     *
125
     * @return array The array of results.
126
     */
127 1
    public function find($criteria)
128
    {
129 1
        return $this->onMatch($criteria);
130
    }
131
132 2
    protected function onMatch($criteria, $limit = null)
133
    {
134 2
        $expression = $this->parser->parse($criteria);
135 2
        $matcher = $this->newMatcher($expression);
136
137 2
        $documents = $this->store->scanDocuments([$matcher, 'match'], $limit);
138
139 2
        return $documents;
140
    }
141
142 2
    protected function newMatcher(ExpressionInterface $expression)
143
    {
144 2
        return new Matcher($expression);
145
    }
146
}
147