Completed
Push — develop ( bf5641...87bb83 )
by Kevin
12:46 queued 01:21
created

Mongo::getCollection()   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 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Magium\Configuration\Config\Storage;
4
5
use Magium\Configuration\Config\Repository\ConfigurationRepository;
6
use MongoDB\Collection;
7
8
9
class Mongo implements StorageInterface
10
{
11
    const TABLE = 'magium_configuration_values';
12
13
    private $mongo;
14
15 6
    public function __construct(
16
        Collection $mongo
17
    )
18
    {
19 6
        $this->mongo = $mongo;
20 6
    }
21
22 2
    public function getCollection()
23
    {
24 2
        return $this->mongo;
25
    }
26
27 2
    public function getValue($path, $context = ConfigurationRepository::CONTEXT_DEFAULT)
28
    {
29 2
        $document = $this->mongo->findOne([
30 2
            'context' => $context
31
        ]);
32 2
        $paths = explode('/', $path);
33 2
        if ($document === null) {
34 1
            $document = [];
35
        }
36 2
        if (isset($document[$paths[0]][$paths[1]][$paths[2]])) {
37 1
            return $document[$paths[0]][$paths[1]][$paths[2]];
38
        }
39 1
        return null;
40
    }
41
42 2
    public function setValue($path, $value, $context = ConfigurationRepository::CONTEXT_DEFAULT)
43
    {
44 2
        $document = $this->mongo->findOne([
45 2
            'context' => $context
46
        ]);
47 2
        $paths = explode('/', $path);
48 2
        if ($document === null) {
49 1
            $document = [];
50
        }
51 2
        $document[$paths[0]][$paths[1]][$paths[2]] = $value;
52 2
        if (isset($document['_id'])) {
53 1
            $this->mongo->replaceOne(['_id' => $document['_id']], $document);
54 1
            return;
55
        }
56 1
        $this->mongo->insertOne($document);
57 1
    }
58
59
    public function create()
60
    {
61
        // Not necessary
62
    }
63
64
65
}
66