Completed
Push — master ( 9540af...12fe6d )
by Dmitry
01:44
created

Entity::getRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Tarantool\Mapper;
4
5
use BadMethodCallException;
6
7
class Entity
8
{
9
    private $_repository;
10
11
    public function __construct(Repository $repository)
12
    {
13
        $this->_repository = $repository;
14
    }
15
16
    public function getRepository()
17
    {
18
        return $this->_repository;
19
    }
20
21
    public function save()
22
    {
23
        return $this->getRepository()->save($this);
24
    }
25
26
    public function __call($name, $arguments)
27
    {
28
        if (strpos($name, 'get') === 0) {
29
            $property = strtolower(substr($name, 3));
30
            if (property_exists($this, $property)) {
31
                $reference = $this->getRepository()->getSpace()->getReference($property);
32
                if ($reference) {
33
                    return $this->getRepository()->getMapper()
34
                        ->findOrFail($reference, [
35
                            'id' => $this->$property,
36
                        ]);
37
                }
38
            }
39
        }
40
        throw new BadMethodCallException("Call to undefined method ". get_class($this).'::'.$name);
41
    }
42
43
    public function __debugInfo()
44
    {
45
        $info = get_object_vars($this);
46
47
        unset($info['_repository']);
48
49
        if (array_key_exists('app', $info) && is_object($info['app'])) {
50
            unset($info['app']);
51
        }
52
53
        return $info;
54
    }
55
}
56