Completed
Push — master ( 99c403...88729d )
by Dmitry
03:04
created

Mapper::find()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 17

Duplication

Lines 4
Ratio 23.53 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 0
Metric Value
dl 4
loc 17
ccs 11
cts 12
cp 0.9167
rs 9.3888
c 0
b 0
f 0
cc 5
nc 4
nop 2
crap 5.0144
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 1
        $instance = new Entity($this->test, $key);
27 1
        foreach ($params as $k => $v) {
28 1
            $instance->$k = $v;
29
        }
30 1
        return $instance;
31
    }
32
33 2
    public function find(string $space, $params = [])
34
    {
35 2
        $key = $this->serviceName.'.'.$space;
36 2
        if (array_key_exists($key, $this->test->data)) {
37 2
            $data = $this->test->data[$key];
38 2
            foreach ($data as $i => $v) {
39 2 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 2
                    unset($data[$i]);
41 2
                    continue;
42
                }
43 2
                $data[$i] = (object) $v;
44
            }
45 2
            $data = array_values($data);
46 2
            return $data;
47
        }
48
        return [];
49
    }
50
51 2
    public function findOne(string $space, $params = [])
52
    {
53 2
        $key = $this->serviceName.'.'.$space;
54 2
        if (is_numeric($params) || is_string($params)) {
55 1
            $params = [ 'id' => $params ];
56
        }
57 2
        if (array_key_exists($key, $this->test->data)) {
58 2
            foreach ($this->test->data[$key] as $candidate) {
59 2
                if (!count($params) || array_intersect_assoc($params, get_object_vars($candidate)) == $params) {
60 2
                    return (object) $candidate;
61
                }
62
            }
63
        }
64 2
    }
65
66 1
    public function findOrCreate(string $space, $params = [])
67
    {
68 1
        if (!$this->findOne($space, $params)) {
69 1
            $this->create($space, $params)->save();
70
        }
71 1
        return $this->findOne($space, $params);
72
    }
73
74 2
    public function findOrFail(string $space, $params = [])
75
    {
76 2
        $result = $this->findOne($space, $params);
77 2
        if (!$result) {
78 1
            throw new Exception("No ".$space.' found using '.json_encode($params));
79
        }
80 2
        return $result;
81
    }
82
83
    public function getPlugin($class)
84
    {
85
        if ($class == Spy::class) {
86
            return new class {
87
                public function hasChanges()
88
                {
89
                    return false;
90
                }
91
            };
92
        }
93
    }
94
95
    protected $repositores = [];
96
97 1
    public function getRepository($space)
98
    {
99 1
        if (!array_key_exists($space, $this->repositores)) {
100
            $this->repositores[$space] = new Repository($this, $space);
101
        }
102 1
        return $this->repositores[$space];
103
    }
104
105 1
    public function remove($space, $params)
106
    {
107 1
        if (is_object($params)) {
108 1
            $params = get_object_vars($params);
109
        }
110 1
        $key = $this->serviceName.'.'.$space;
111 1
        if (array_key_exists($key, $this->test->data)) {
112 1
            foreach ($this->test->data[$key] as $i => $v) {
113 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...
114 1
                    unset($this->test->data[$key][$i]);
115
                }
116
            }
117
        }
118
    }
119
}