Completed
Push — master ( 5b35ba...648bc7 )
by Dmitry
02:58
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 as TarantoolClient;
6
use Tarantool\Mapper\Schema\Schema;
7
use Tarantool\Mapper\Schema\Meta;
8
use LogicException;
9
10
class Manager implements Contracts\Manager
11
{
12
    protected $meta;
13
    protected $schema;
14
    protected $client;
15
    protected $repositores = [];
16
17 1
    public function __construct(TarantoolClient $client)
18
    {
19 1
        $this->client = $client;
20 1
    }
21
22
    /**
23
     * @return Contracts\Repository|Contracts\Entity
24
     */
25
    public function get($type, $id = null)
26
    {
27
        if (!array_key_exists($type, $this->repositores)) {
28
            $this->repositores[$type] = new Repository($this->getMeta()->get($type));
29
        }
30
31
        if ($id) {
32
            return $this->repositores[$type]->find($id);
33
        }
34
35
        return $this->repositores[$type];
36
    }
37
38
    public function forgetRepository($type)
39
    {
40
        if (array_key_exists($type, $this->repositores)) {
41
            unset($this->repositores[$type]);
42
        }
43
    }
44
45
    /**
46
     * @return Contracts\Entity
47
     */
48
    public function save(Contracts\Entity $entity)
49
    {
50
        return $this->findRepository($entity)->save($entity);
51
    }
52
53
    /**
54
     * @return Contracts\Entity
55
     */
56
    public function remove(Contracts\Entity $entity)
57
    {
58
        return $this->findRepository($entity)->remove($entity);
59
    }
60
61
    public function findRepository(Contracts\Entity $entity)
62
    {
63
        foreach ($this->repositores as $repository) {
64
            if ($repository->knows($entity)) {
65
                return $repository;
66
            }
67
        }
68
        throw new LogicException('Entity should be related with repository');
69
    }
70
71
    /**
72
     * @return Contracts\Entity
73
     */
74
    public function create($type, $data = null)
75
    {
76
        return $this->save($this->get($type)->create($data));
0 ignored issues
show
Bug introduced by
The method create does only exist in Tarantool\Mapper\Contracts\Repository, but not in Tarantool\Mapper\Contracts\Entity.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
77
    }
78
79
    /**
80
     * @return Client
81
     */
82 1
    public function getClient()
83
    {
84 1
        return $this->client;
85
    }
86
87
    /**
88
     * @return Schema
89
     */
90
    public function getSchema()
91
    {
92
        if (!isset($this->schema)) {
93
            $this->schema = new Schema($this->getClient());
94
        }
95
96
        return $this->schema;
97
    }
98
99
    /**
100
     * @return Meta
101
     */
102
    public function getMeta()
103
    {
104
        if (!isset($this->meta)) {
105
            $this->meta = new Meta($this);
106
        }
107
108
        return $this->meta;
109
    }
110
}
111