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

Database::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

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 6
rs 9.4285
ccs 0
cts 4
cp 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Mattbit\Flat;
4
5
use Mattbit\Flat\Query\Expression\Factory;
6
use Mattbit\Flat\Query\Parser;
7
use Mattbit\Flat\Storage\Engine;
8
9
class Database
10
{
11
    /**
12
     * The storage engine.
13
     *
14
     * @var Engine
15
     */
16
    protected $engine;
17
18
    protected $parser;
19
20
    /**
21
     * An array of the registered collections.
22
     *
23
     * @var array
24
     */
25
    protected $collections;
26
27
    public function __construct(Engine $engine)
28
    {
29
        $this->engine = $engine;
30
31
        $this->initializeParser();
32
    }
33
34
    /**
35
     * Select an existing collection or create a new one.
36
     *
37
     * @param string $name
38
     *
39
     * @return Collection
40
     */
41
    public function collection($name)
42
    {
43
        return $this->getOrCreateCollection($name);
44
    }
45
46
    public function getOrCreateCollection($name)
47
    {
48
        if (isset($this->collections[$name])) {
49
            return $this->collections[$name];
50
        }
51
52
        return $this->createCollection($name);
53
    }
54
55
    public function createCollection($name)
56
    {
57
        $store = $this->engine->createDocumentStore($name);
58
59
        return $this->collections[$name] = new Collection($this, $store, $name);
60
    }
61
62
    public function dropCollection($name)
63
    {
64
        $this->engine->removeCollection($name);
0 ignored issues
show
Bug introduced by
The method removeCollection() does not exist on Mattbit\Flat\Storage\Engine. Did you maybe mean remove()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
65
        unset($this->collections[$name]);
66
67
        return true;
68
    }
69
70
    public function getParser()
71
    {
72
        return $this->parser;
73
    }
74
    
75
    protected function initializeParser()
76
    {
77
        $factory = new Factory();
78
        $this->parser = new Parser($factory);
79
    }
80
}
81