Completed
Push — master ( 9ac263...875554 )
by Dmitry
02:20
created

Pool::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace Tarantool\Mapper;
4
5
use Exception;
6
7
class Pool
8
{
9
    private $description = [];
10
    private $mappers = [];
11
12
    public function register($name, $handler)
13
    {
14
        if (array_key_exists($name, $this->description)) {
15
            throw new Exception("Mapper $name was registered");
16
        }
17
18
        if ($handler instanceof Mapper) {
19
            $this->description[$name] = $handler;
20
            $this->mappers[$name] = $handler;
21
            return;
22
        }
23
24
        if (!is_callable($handler)) {
25
            throw new Exception("Invalid $name handler");
26
        }
27
28
        $this->description[$name] = $handler;
29
    }
30
31
    public function get($name)
32
    {
33
        if (array_key_exists($name, $this->mappers)) {
34
            return $this->mappers[$name];
35
        }
36
37
        if (!array_key_exists($name, $this->description)) {
38
            throw new Exception("Mapper $name was not registered");
39
        }
40
41
        return $this->mappers[$name] = call_user_func($this->description[$name]);
42
    }
43
}
44