Completed
Push — master ( 13dce9...b41dd5 )
by Dmitry
03:01
created

Manager::getClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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