Completed
Push — master ( addaf0...c57d80 )
by Dmitry
03:20
created

Manager::save()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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