Mongo   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 63
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getCollection() 0 4 1
A getValue() 0 17 3
A setValue() 0 19 3
A create() 0 4 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