EntityProxy::getUnderlyingObject()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Analogue\ORM\System\Proxies;
4
5
class EntityProxy extends Proxy
6
{
7
    /**
8
     * Underlying entity
9
     *
10
     * @var mixed
11
     */
12
    protected $entity;
13
14
    /**
15
     * Load the underlying relation
16
     *
17
     * @return void
18
     */
19
    protected function loadOnce()
20
    {
21
        $this->entity = $this->load();
22
    }
23
24
    /**
25
     * Return the actual Entity
26
     *
27
     * @return mixed
28
     */
29
    public function getUnderlyingObject()
30
    {
31
        if (!$this->isLoaded()) {
32
            $this->loadOnce();
33
        }
34
35
        return $this->entity;
36
    }
37
38
    /**
39
     * Transparently passes get operation to underlying entity
40
     *
41
     * @param  string $attribute
42
     * @return mixed
43
     */
44
    public function __get($attribute)
45
    {
46
        if (!$this->isLoaded()) {
47
            $this->loadOnce();
48
        }
49
50
        return $this->entity->$attribute;
51
    }
52
53
    /**
54
     * Transparently passes set operation to underlying entity
55
     *
56
     * @param  string $attribute [description]
57
     * @param  mixed
58
     * @return void
59
     */
60
    public function __set($attribute, $value)
61
    {
62
        if (!$this->isLoaded()) {
63
            $this->loadOnce();
64
        }
65
66
        $this->entity->$attribute = $value;
67
    }
68
69
    /**
70
     * Transparently Redirect non overrided calls to the lazy loaded Entity
71
     *
72
     * @param  string $method
73
     * @param  array  $parameters
74
     * @return mixed
75
     */
76
    public function __call($method, $parameters)
77
    {
78
        if (!$this->isLoaded()) {
79
            $this->loadOnce();
80
        }
81
82
        return call_user_func_array([$this->entity, $method], $parameters);
83
    }
84
}
85