Completed
Push — master ( 39aa7c...b6b4c6 )
by Dmitry
01:51
created

Pool::drop()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tarantool\Mapper;
6
7
use Exception;
8
9
class Pool
10
{
11
    private $description = [];
12
    private $mappers = [];
13
    private $resolvers = [];
14
    private $repositories = [];
15
16
    public function register($name, $handler)
17
    {
18
        if (array_key_exists($name, $this->description)) {
19
            throw new Exception("Mapper $name was registered");
20
        }
21
22
        if ($handler instanceof Mapper) {
23
            $this->description[$name] = $handler;
24
            $this->mappers[$name] = $handler;
25
            return;
26
        }
27
28
        if (!is_callable($handler)) {
29
            throw new Exception("Invalid $name handler");
30
        }
31
32
        $this->description[$name] = $handler;
33
        return $this;
34
    }
35
36
    public function registerResolver($resolver)
37
    {
38
        $this->resolvers[] = $resolver;
39
        return $this;
40
    }
41
42
    public function get($name)
43
    {
44
        return $this->getMapper($name);
45
    }
46
47
    public function drop($name)
48
    {
49
        if (array_key_exists($name, $this->mappers)) {
50
            unset($this->mappers[$name]);
51
        }
52
    }
53
54
    public function getMapper($name)
55
    {
56
        if (array_key_exists($name, $this->mappers)) {
57
            return $this->mappers[$name];
58
        }
59
60
        if (array_key_exists($name, $this->description)) {
61
            return $this->mappers[$name] = call_user_func($this->description[$name]);
62
        }
63
64
        foreach ($this->resolvers as $resolver) {
65
            $mapper = call_user_func($resolver, $name);
66
            if ($mapper) {
67
                return $this->mappers[$name] = $mapper;
68
            }
69
        }
70
71
        throw new Exception("Mapper $name is not registered");
72
    }
73
74
    public function getMappers()
75
    {
76
        return array_values($this->mappers);
77
    }
78
79
    public function getRepository($space)
80
    {
81
        if (!array_key_exists($space, $this->repositories)) {
82
            $parts = explode('.', $space);
83
            if (count($parts) !== 2) {
84
                throw new Exception("Invalid pool space name: $space");
85
            }
86
            $this->repositories[$space] = $this->getMapper($parts[0])->getRepository($parts[1]);
87
        }
88
        return $this->repositories[$space];
89
    }
90
91
    public function create(string $space, $data)
92
    {
93
        return $this->getRepository($space)->create($data)->save();
94
    }
95
96
    public function findOne(string $space, $params = [])
97
    {
98
        return $this->getRepository($space)->findOne($params);
99
    }
100
101
    public function findOrCreate(string $space, $params = [], $data = [])
102
    {
103
        return $this->getRepository($space)->findOrCreate($params, $data)->save();
104
    }
105
106
    public function findOrFail(string $space, $params = [])
107
    {
108
        return $this->getRepository($space)->findOrFail($params);
109
    }
110
111
    public function find(string $space, $params = [])
112
    {
113
        return $this->getRepository($space)->find($params);
114
    }
115
}
116