Completed
Branch master (0bf65c)
by Dmitry
02:31
created

Manager::save()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

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