Mongo::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
            $document = [
35 1
                'context' => $context,
36
                'document'=> []
37
            ];
38
        }
39 2
        if (isset($document['document'][$paths[0]][$paths[1]][$paths[2]])) {
40 1
            return $document['document'][$paths[0]][$paths[1]][$paths[2]];
41
        }
42 1
        return null;
43
    }
44
45 2
    public function setValue($path, $value, $context = ConfigurationRepository::CONTEXT_DEFAULT)
46
    {
47 2
        $document = $this->mongo->findOne([
48 2
            'context' => $context
49
        ]);
50 2
        $paths = explode('/', $path);
51 2
        if ($document === null) {
52
            $document = [
53 1
                'context' => $context,
54
                'document'=> []
55
            ];
56
        }
57 2
        $document['document'][$paths[0]][$paths[1]][$paths[2]] = $value;
58 2
        if (isset($document['_id'])) {
59 1
            $this->mongo->replaceOne(['_id' => $document['_id']], $document);
60 1
            return;
61
        }
62 1
        $this->mongo->insertOne($document);
63 1
    }
64
65
    public function create()
66
    {
67
        // Not necessary
68
    }
69
70
71
}
72