Completed
Push — master ( c57d80...dcba90 )
by Dmitry
03:15
created

Manager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Tarantool\Mapper;
4
5
use Tarantool\Client;
6
use Tarantool\Mapper\Contracts;
7
use Tarantool\Mapper\Schema\Schema;
8
use Tarantool\Mapper\Schema\Meta;
9
use LogicException;
10
11
class Manager implements Contracts\Manager
12
{
13
    protected $meta;
14
    protected $schema;
15
    protected $client;
16
    protected $repositores = [];
17
18 3
    public function __construct(Client $client)
19
    {
20 3
        $this->client = $client;
21 3
    }
22
23
    /**
24
     * @return Contracts\Repository
25
     */
26 3
    public function get($type)
27
    {
28 3
        if (!array_key_exists($type, $this->repositores)) {
29 3
            $this->repositores[$type] = new Repository($this->getMeta()->get($type));
30
        }
31 3
        return $this->repositores[$type];
32
    }
33
34
    /**
35
     * @return Contracts\Entity
36
     */
37 3
    public function save(Contracts\Entity $entity)
38
    {
39 3
        foreach ($this->repositores as $repository) {
40 3
            if ($repository->knows($entity)) {
41 3
                return $repository->save($entity);
42
            }
43
        }
44
45
        throw new LogicException("Entity should be related with repository");
46
    }
47
48
    /**
49
     * @return Client
50
     */
51 3
    public function getClient()
52
    {
53 3
        return $this->client;
54
    }
55
56
    /**
57
     * @return Schema
58
     */
59 3
    public function getSchema()
60
    {
61 3
        if (!isset($this->schema)) {
62 3
            $this->schema = new Schema($this->getClient());
63
        }
64 3
        return $this->schema;
65
    }
66
67
    /**
68
     * @return Meta
69
     */
70 3
    public function getMeta()
71
    {
72 3
        if (!isset($this->meta)) {
73 3
            $this->meta = new Meta($this);
74
        }
75 3
        return $this->meta;
76
    }
77
}
78