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

DocumentManager::readEntries()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
cc 4
nc 6
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
namespace EmberDb;
25
26
use EmberDb\Collection\Collection;
27
28
/**
29
 * The class DocumentManager is responsible for acting as an interface to the database.
30
 */
31
class DocumentManager
32
{
33
    private $config = array();
34
35
    private $collections = [];
36
37
38
39
    public function getDatabasePath()
40
    {
41
        try {
42
            if (!array_key_exists('database', $this->config)) throw new Exception('The config has no entry with key "database".');
43
            if (!array_key_exists('path', $this->config['database'])) throw new Exception('The config has no entry with key "database/path".');
44
        }
45
        catch (Exception $exception) {
46
            throw new Exception('Missing config: '.$exception->getMessage());
47
        }
48
49
        return $this->config['database']['path'];
50
    }
51
52
53
54
    public function setDatabasePath($path)
55
    {
56
        $this->config['database']['path'] = $path;
57
    }
58
59
60
61
    public function insert($collectionName, $document)
62
    {
63
        $collection = $this->getCollection($collectionName);
64
        $collection->insert($document);
65
    }
66
67
68
69
    public function insertMany($collectionName, $documents)
70
    {
71
        $collection = $this->getCollection($collectionName);
72
        $collection->insertMany($documents);
73
    }
74
75
76
77
    /**
78
     * @param string $collectionName
79
     * @param array $filter
80
     * @return Document[]
81
     */
82
    public function find(string $collectionName, array $filter = array()): array
83
    {
84
        return $this->getCollection($collectionName)->find($filter);
85
    }
86
87
88
89
    public function remove($collectionName)
90
    {
91
        $this->getCollection($collectionName)->remove();
92
    }
93
94
95
96
    private function getCollection(string $collectionName): Collection
97
    {
98
        if (!array_key_exists($collectionName, $this->collections)) {
99
            $collection = new Collection($collectionName, $this->getDatabasePath());
100
            $this->collections[$collectionName] = $collection;
101
        }
102
103
        return $this->collections[$collectionName];
104
    }
105
}
106