Completed
Push — master ( ef3cb7...1add5f )
by Dmitry
09:13
created

Mapper.php$0 ➔ hasChanges()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
crap 1
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 1
    public function __construct(Test $test, $service)
15
    {
16 1
        $this->test = $test;
17 1
        $this->serviceName = $service;
18 1
    }
19
20 1
    public function create($space, $params)
21
    {
22 1
        $key = $this->serviceName.'.'.$space;
23 1
        if (!array_key_exists($key, $this->test->data)) {
24
            throw new Exception("No data container for $key");
25
        }
26
        $instance = new Entity($this->test, $key);
27
        foreach ($params as $k => $v) {
28 1
            $instance->$k = $v;
29
        }
30 1
        return $instance;
31 1
    }
32 1
33 1
    public function find(string $space, $params = [])
34 1
    {
35
        $key = $this->serviceName.'.'.$space;
36 1
        if (array_key_exists($key, $this->test->data)) {
37 1
            $data = $this->test->data[$key];
38 1
            foreach ($data as $i => $v) {
39 1 View Code Duplication
                if (count($params) && array_intersect_assoc($params, get_object_vars($v)) != $params) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
                    unset($data[$i]);
41
                    continue;
42 1
                }
43 1
                $data[$i] = (object) $v;
44
            }
45
            $data = array_values($data);
46
            return $data;
47
        }
48
        return [];
49 2
    }
50
51 2
    public function findOne(string $space, $params = [])
52 2
    {
53 2
        $key = $this->serviceName.'.'.$space;
54 2
        if (is_numeric($params) || is_string($params)) {
55 2
            $params = [ 'id' => $params ];
56 2
        }
57 2
        if (array_key_exists($key, $this->test->data)) {
58
            foreach ($this->test->data[$key] as $candidate) {
59 2
                if (!count($params) || array_intersect_assoc($params, get_object_vars($candidate)) == $params) {
60
                    return (object) $candidate;
61 2
                }
62 2
            }
63
        }
64
    }
65
66
    public function findOrFail(string $space, $params = [])
67 2
    {
68
        $result = $this->findOne($space, $params);
69 2
        if (!$result) {
70 2
            throw new Exception("No ".$space.' found using '.json_encode($params));
71 1
        }
72
        return $result;
73 2
    }
74 2
75 2
    public function getPlugin($class)
76 2
    {
77
        if ($class == Spy::class) {
78
            return new class {
79
                public function hasChanges()
80 2
                {
81
                    return false;
82 2
                }
83
            };
84 2
        }
85 2
    }
86 1
87
    protected $repositores = [];
88 2
89
    public function getRepository($space)
90
    {
91
        if (!array_key_exists($space, $this->repositores)) {
92
            $this->repositores[$space] = new Repository($this, $space);
93
        }
94
        return $this->repositores[$space];
95
    }
96
97
    public function remove($space, $params)
98
    {
99
        if (is_object($params)) {
100
            $params = get_object_vars($params);
101
        }
102
        $key = $this->serviceName.'.'.$space;
103
        if (array_key_exists($key, $this->test->data)) {
104
            foreach ($this->test->data[$key] as $i => $v) {
105 1 View Code Duplication
                if (count($params) && array_intersect_assoc($params, get_object_vars($v)) == $params) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
                    unset($this->test->data[$key][$i]);
107 1
                }
108
            }
109
        }
110
    }
111
}