Completed
Push — master ( d3bbed...3b8a52 )
by Dmitry
01:36
created

Entity   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 17
c 3
b 0
f 1
lcom 1
cbo 3
dl 0
loc 69
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRepository() 0 4 1
A save() 0 4 1
C __call() 0 36 11
A __debugInfo() 0 12 3
1
<?php
2
3
namespace Tarantool\Mapper;
4
5
use BadMethodCallException;
6
use Tarantool\Mapper\Plugin\Annotation;
7
8
class Entity
9
{
10
    private $_repository;
11
12
    public function __construct(Repository $repository)
13
    {
14
        $this->_repository = $repository;
15
    }
16
17
    public function getRepository()
18
    {
19
        return $this->_repository;
20
    }
21
22
    public function save()
23
    {
24
        return $this->getRepository()->save($this);
25
    }
26
27
    public function __call($name, $arguments)
28
    {
29
        if (strpos($name, 'get') === 0) {
30
            $property = lcfirst(substr($name, 3));
31
            $mapper = $this->getRepository()->getMapper();
32
            if (property_exists($this, $property)) {
33
                $reference = $this->getRepository()->getSpace()->getReference($property);
34
                if ($reference) {
35
                    return $mapper->findOrFail($reference, [
36
                        'id' => $this->$property,
37
                    ]);
38
                }
39
            } else if(strpos($property, 'Collection') !== false) {
40
                $property = substr($property, 0, -10);
41
                $targetSpace = $mapper->getSchema()->toUnderscore($property);
42
                if ($mapper->getSchema()->hasSpace($targetSpace)) {
43
                    $localSpace = $this->getRepository()->getSpace()->getName();
44
                    $candidates = [];
45
                    foreach ($mapper->getSchema()->getSpace($targetSpace)->getFormat() as $row) {
46
                        if (array_key_exists('reference', $row) && $row['reference'] == $localSpace) {
47
                            $candidates[] = $row['name'];
48
                        }
49
                    }
50
                    if (count($candidates) == 1) {
51
                        return $mapper->find($targetSpace, [
52
                            $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...
53
                        ]);
54
                    }
55
                    if (count($candidates) > 1) {
56
                        throw new Exception("Multiple references from $targetSpace to $localSpace");
57
                    }
58
                }
59
            }
60
        }
61
        throw new BadMethodCallException("Call to undefined method ". get_class($this).'::'.$name);
62
    }
63
64
    public function __debugInfo()
65
    {
66
        $info = get_object_vars($this);
67
68
        unset($info['_repository']);
69
70
        if (array_key_exists('app', $info) && is_object($info['app'])) {
71
            unset($info['app']);
72
        }
73
74
        return $info;
75
    }
76
}
77