Completed
Push — master ( 242ecf...24efff )
by Dmitry
03:19
created

Mapper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 83.78%

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 72
ccs 31
cts 37
cp 0.8378
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ hasChanges() 0 4 1
A __construct() 0 5 1
A find() 0 17 5
A findOne() 0 11 5
A findOrFail() 0 8 2
A getPlugin() 0 11 2
A getRepository() 0 7 2
1
<?php
2
3
namespace Basis\Test;
4
5
use Basis\Test;
6
use Exception;
7
use Tarantool\Mapper\Plugin\Spy;
8
9
class Mapper
10
{
11
    protected $test;
12
    public $serviceName;
13
14 2
    public function __construct(Test $test, $service)
15
    {
16 2
        $this->test = $test;
17 2
        $this->serviceName = $service;
18 2
    }
19
20 1
    public function find(string $space, $params = [])
21
    {
22 1
        $key = $this->serviceName.'.'.$space;
23 1
        if (array_key_exists($key, $this->test->data)) {
24 1
            $data = $this->test->data[$key];
25 1
            foreach ($data as $i => $v) {
26 1
                if (count($params) && array_intersect_assoc($params, $v) != $params) {
27 1
                    unset($data[$i]);
28 1
                    continue;
29
                }
30 1
                $data[$i] = (object) $v;
31
            }
32 1
            $data = array_values($data);
33 1
            return $data;
34
        }
35
        return [];
36
    }
37
38 1
    public function findOne(string $space, $params = [])
39
    {
40 1
        $key = $this->serviceName.'.'.$space;
41 1
        if (array_key_exists($key, $this->test->data)) {
42 1
            foreach ($this->test->data[$key] as $candidate) {
43 1
                if (!count($params) || array_intersect_assoc($params, $candidate) == $params) {
44 1
                    return (object) $candidate;
45
                }
46
            }
47
        }
48 1
    }
49
50 1
    public function findOrFail(string $space, $params = [])
51
    {
52 1
        $result = $this->findOne($space, $params);
53 1
        if (!$result) {
54 1
            throw new Exception("No ".$space.' found using '.json_encode($params));
55
        }
56 1
        return $result;
57
    }
58
59
    public function getPlugin($class)
60
    {
61
        if ($class == Spy::class) {
62
            return new class {
63
                public function hasChanges()
64
                {
65
                    return false;
66
                }
67
            };
68
        }
69
    }
70
71
    protected $repositores = [];
72
73 1
    public function getRepository($space)
74
    {
75 1
        if (!array_key_exists($space, $this->repositores)) {
76 1
            $this->repositores[$space] = new Repository($this, $space);
77
        }
78 1
        return $this->repositores[$space];
79
    }
80
}
81