Completed
Push — master ( 5a3952...8a8d0e )
by Alexander
01:24
created

Collection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 $document)
55
    {
56
        $this->insertEntries(array($document));
57
    }
58
59
60
61
    public function insertMany($documents)
62
    {
63
        $this->insertEntries($documents);
64
    }
65
66
67
68
    public function find(Filter $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(Filter $filter)
93
    {
94
        $entries = array();
95
96
        // Open file for reading
97
        $collectionFilePath = $this->getCollectionFilePath();
98
        $file = fopen($collectionFilePath, 'r');
99
100
        // Read file line by line
101
        while (($buffer = fgets($file)) !== false) {
102
            $entry = json_decode(trim($buffer), true);
103
            // Match entry against filter
104
            if ($filter->matchesEntry($entry)) {
105
                $entries[] = $entry;
106
            }
107
        }
108
109
        if (!feof($file)) {
110
            throw new Exception('File pointer does not point to end of file.');
111
        }
112
113
        // Close file
114
        fclose($file);
115
116
        return $entries;
117
    }
118
119
120
121
    private function insertEntries($documents)
122
    {
123
        // Open or create file for writing
124
        $collectionFilePath = $this->getCollectionFilePath();
125
        $collectionFileHandle = fopen($collectionFilePath, 'a');
126
127
        // Add entries to end of file
128
        foreach ($documents as $document) {
129
            $document->setId($this->createId());
130
            fwrite($collectionFileHandle, json_encode($document)."\n");
131
        }
132
133
        // Close file
134
        fclose($collectionFileHandle);
135
    }
136
137
138
139
    private function removeEntries(array $filterArray)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Unused Code introduced by
The parameter $filterArray 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...
140
    {
141
        // Do the same like readEntries does, but copy all non matching
142
        // entries into new file and remove the old one.
143
    }
144
145
146
147
148
    private function getCollectionFilePath()
149
    {
150
        return $this->path . '/' . $this->name . '.edb';
151
    }
152
153
154
155
    private function createId(): string
156
    {
157
        return $id = time() . '-' . mt_rand(1000, 9999);
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
158
    }
159
}
160