Completed
Push — master ( e53887...4fea40 )
by Alexander
02:15
created

Collection::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Ember Db - An embeddable document database for php.
4
 * Copyright (C) 2016 Alexander During
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * @link      http://github.com/alexanderduring/php-ember-db
20
 * @copyright Copyright (C) 2016 Alexander During
21
 * @license   http://www.gnu.org/licenses GNU General Public License v3.0
22
 */
23
24
declare(strict_types=1);
25
26
namespace EmberDb\Collection;
27
28
use EmberDb\Document;
29
use EmberDb\Exception;
30
use EmberDb\Filter\Filter;
31
32
class Collection
33
{
34
    /** @var MetaData */
35
    private $metaData;
36
37
    /** @var string */
38
    private $name;
39
40
    /** @var string */
41
    private $path;
42
43
44
45
    public function __construct(string $name, string $path)
46
    {
47
        $this->name = $name;
48
        $this->path = $path;
49
        $this->metaData = new MetaData($path . '/' . $name . 'meta.edb');
50
    }
51
52
53
54
    public function insert($document)
55
    {
56
        $this->writeEntries(array($document));
57
    }
58
59
60
61
    public function insertMany($documents)
62
    {
63
        $this->writeEntries($documents);
64
    }
65
66
67
68
    public function find(array $filter = []): array
69
    {
70
        $documents = [];
71
72
        $entries = $this->readEntries($filter);
73
        foreach ($entries as $entry) {
74
            $documents[] = new Document($entry);
75
        }
76
77
        return $documents;
78
    }
79
80
81
82
    public function remove()
83
    {
84
        $filePath = $this->getCollectionFilePath();
85
        if (file_exists($filePath)) {
86
            unlink($filePath);
87
        }
88
    }
89
90
91
92
    private function readEntries(array $filterArray)
93
    {
94
        $entries = array();
95
96
        $filter = new Filter($filterArray);
97
98
        // Open file for reading
99
        $collectionFilePath = $this->getCollectionFilePath();
100
        $file = fopen($collectionFilePath, 'r');
101
102
        // Read file line by line
103
        while (($buffer = fgets($file)) !== false) {
104
            $entry = json_decode(trim($buffer), true);
105
            // Match entry against filter
106
            if ($filter->matchesEntry($entry)) {
107
                $entries[] = $entry;
108
            }
109
        }
110
111
        if (!feof($file)) {
112
            throw new Exception('File pointer does not point to end of file.');
113
        }
114
115
        // Close file
116
        fclose($file);
117
118
        return $entries;
119
    }
120
121
122
123
    private function writeEntries($documents)
124
    {
125
        // Open or create file for writing
126
        $collectionFilePath = $this->getCollectionFilePath();
127
        $collectionFileHandle = fopen($collectionFilePath, 'a');
128
129
        // Add entries to end of file
130
        foreach ($documents as $document) {
131
            fwrite($collectionFileHandle, json_encode($document)."\n");
132
        }
133
134
        // Close file
135
        fclose($collectionFileHandle);
136
    }
137
138
139
140
    private function getCollectionFilePath()
141
    {
142
        return $this->path . '/' . $this->name . '.edb';
143
    }
144
}
145