Completed
Push — master ( 4b0ebb...4b2239 )
by Dmitry
02:10
created

Pool::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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