Completed
Push — master ( cc5af7...6c15a2 )
by Dmitry
02:05
created

Entity::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tarantool\Mapper;
6
7
use BadMethodCallException;
8
use Exception;
9
use Tarantool\Mapper\Plugin\Annotation;
10
11
class Entity
12
{
13
    private $_repository;
14
15
    public function __construct(Repository $repository)
16
    {
17
        $this->_repository = $repository;
18
    }
19
20
    public function getRepository() : Repository
21
    {
22
        return $this->_repository;
23
    }
24
25
    public function save() : Entity
26
    {
27
        return $this->getRepository()->save($this);
28
    }
29
30
    public function __call($name, $arguments)
31
    {
32
        if (strpos($name, 'get') === 0) {
33
            $property = lcfirst(substr($name, 3));
34
            $mapper = $this->getRepository()->getMapper();
35
            if (property_exists($this, $property)) {
36
                $reference = $this->getRepository()->getSpace()->getReference($property);
37
                if ($reference) {
38
                    return $mapper->findOrFail($reference, [
39
                        'id' => $this->$property,
40
                    ]);
41
                }
42
            } else if(strpos($property, 'Collection') !== false) {
43
                $property = substr($property, 0, -10);
44
                $targetSpace = $mapper->getSchema()->toUnderscore($property);
45
                if ($mapper->getSchema()->hasSpace($targetSpace)) {
46
                    $localSpace = $this->getRepository()->getSpace()->getName();
47
                    $candidates = [];
48
                    foreach ($mapper->getSchema()->getSpace($targetSpace)->getFormat() as $row) {
49
                        if (array_key_exists('reference', $row) && $row['reference'] == $localSpace) {
50
                            $candidates[] = $row['name'];
51
                        }
52
                    }
53
                    if (count($candidates) == 1) {
54
                        return $mapper->find($targetSpace, [
55
                            $candidates[0] => $this->id
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
                        ]);
57
                    }
58
                    if (count($candidates) > 1) {
59
                        throw new Exception("Multiple references from $targetSpace to $localSpace");
60
                    }
61
                }
62
            }
63
        }
64
        throw new BadMethodCallException("Call to undefined method ". get_class($this).'::'.$name);
65
    }
66
67
    public function sync() : Entity
68
    {
69
        return $this->getRepository()->sync($this->id);
70
    }
71
72
    public function __debugInfo()
73
    {
74
        $info = get_object_vars($this);
75
76
        unset($info['_repository']);
77
78
        if (array_key_exists('app', $info) && is_object($info['app'])) {
79
            unset($info['app']);
80
        }
81
82
        return $info;
83
    }
84
}
85