1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Basis\Test; |
4
|
|
|
|
5
|
|
|
use Basis\Test; |
6
|
|
|
use Exception; |
7
|
|
|
use Tarantool\Mapper\Plugin\Spy; |
8
|
|
|
use Tarantool\Mapper\Entity as MapperEntity; |
9
|
|
|
|
10
|
|
|
class Mapper |
11
|
|
|
{ |
12
|
|
|
protected $test; |
13
|
|
|
public $serviceName; |
14
|
|
|
|
15
|
1 |
|
public function __construct(Test $test, $service) |
16
|
|
|
{ |
17
|
1 |
|
$this->test = $test; |
18
|
1 |
|
$this->serviceName = $service; |
19
|
1 |
|
} |
20
|
|
|
|
21
|
2 |
|
public function create($space, $params) |
22
|
|
|
{ |
23
|
2 |
|
$key = $this->serviceName.'.'.$space; |
24
|
2 |
|
if (!array_key_exists($key, $this->test->data)) { |
25
|
|
|
throw new Exception("No data container for $key"); |
26
|
|
|
} |
27
|
2 |
|
$instance = new Entity($this->test, $key); |
28
|
2 |
|
foreach ($params as $k => $v) { |
29
|
2 |
|
$instance->$k = $v instanceof MapperEntity ? $v->id : $v; |
30
|
|
|
} |
31
|
2 |
|
return $instance; |
32
|
|
|
} |
33
|
|
|
|
34
|
3 |
|
public function find(string $space, $params = []) |
35
|
|
|
{ |
36
|
3 |
|
$key = $this->serviceName.'.'.$space; |
37
|
3 |
|
if (array_key_exists($key, $this->test->data)) { |
38
|
3 |
|
$data = $this->test->data[$key]; |
39
|
3 |
|
foreach ($data as $i => $v) { |
40
|
3 |
View Code Duplication |
if (count($params) && array_intersect_assoc($params, get_object_vars($v)) != $params) { |
|
|
|
|
41
|
3 |
|
unset($data[$i]); |
42
|
3 |
|
continue; |
43
|
|
|
} |
44
|
3 |
|
$data[$i] = (object) $v; |
45
|
|
|
} |
46
|
3 |
|
$data = array_values($data); |
47
|
3 |
|
return $data; |
48
|
|
|
} |
49
|
|
|
return []; |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
public function findOne(string $space, $params = []) |
53
|
|
|
{ |
54
|
2 |
|
$key = $this->serviceName.'.'.$space; |
55
|
2 |
|
if (is_numeric($params) || is_string($params)) { |
56
|
1 |
|
$params = [ 'id' => $params ]; |
57
|
|
|
} |
58
|
2 |
|
if (array_key_exists($key, $this->test->data)) { |
59
|
2 |
|
foreach ($this->test->data[$key] as $candidate) { |
60
|
2 |
|
if (!count($params) || array_intersect_assoc($params, get_object_vars($candidate)) == $params) { |
61
|
2 |
|
return (object) $candidate; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
2 |
|
} |
66
|
|
|
|
67
|
1 |
|
public function findOrCreate(string $space, $params = [], $data = []) |
68
|
|
|
{ |
69
|
1 |
|
if (!$this->findOne($space, $params)) { |
70
|
1 |
|
$this->create($space, $data ?: $params)->save(); |
71
|
|
|
} |
72
|
1 |
|
return $this->findOne($space, $params); |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
public function findOrFail(string $space, $params = []) |
76
|
|
|
{ |
77
|
2 |
|
$result = $this->findOne($space, $params); |
78
|
2 |
|
if (!$result) { |
79
|
1 |
|
throw new Exception("No ".$space.' found using '.json_encode($params)); |
80
|
|
|
} |
81
|
2 |
|
return $result; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getPlugin($class) |
85
|
|
|
{ |
86
|
|
|
if ($class == Spy::class) { |
87
|
|
|
return new class { |
88
|
|
|
public function hasChanges() |
89
|
|
|
{ |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
}; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected $repositores = []; |
97
|
|
|
|
98
|
2 |
|
public function getRepository($space) |
99
|
|
|
{ |
100
|
2 |
|
if (!array_key_exists($space, $this->repositores)) { |
101
|
1 |
|
$this->repositores[$space] = new Repository($this, $space); |
102
|
|
|
} |
103
|
2 |
|
return $this->repositores[$space]; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
public function remove($space, $params) |
107
|
|
|
{ |
108
|
1 |
|
if (is_object($params)) { |
109
|
1 |
|
$params = get_object_vars($params); |
110
|
|
|
} |
111
|
1 |
|
$key = $this->serviceName.'.'.$space; |
112
|
1 |
|
if (array_key_exists($key, $this->test->data)) { |
113
|
1 |
|
foreach ($this->test->data[$key] as $i => $v) { |
114
|
1 |
View Code Duplication |
if (count($params) && array_intersect_assoc($params, get_object_vars($v)) == $params) { |
|
|
|
|
115
|
1 |
|
unset($this->test->data[$key][$i]); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
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.