Completed
Push — develop ( 62651e...ddc457 )
by
unknown
09:17
created

SnapshotTrait::getSnapshotAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Entity;
12
13
use Core\Entity\Collection\ArrayCollection;
14
use Core\Exception\ImmutablePropertyException;
15
use Doctrine\Common\Collections\Collection;
16
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
17
18
/**
19
 * ${CARET}
20
 * 
21
 * @author Mathias Gelhausen <[email protected]>
22
 * @todo write test 
23
 */
24
trait SnapshotTrait
25
{
26
    /**
27
     *
28
     * @ODM\EmbedOne(targetDocument="Core\Entity\SnapshotMeta")
29
     * @var SnapshotMeta
30
     */
31
    protected $snapshotMeta;
32
33
    public function __construct(EntityInterface $source)
34
    {
35
        $this->snapshotMeta = new SnapshotMeta($source);
36
    }
37
38
    public function getSnapshotMeta()
39
    {
40
        return $this->snapshotMeta;
41
    }
42
43
    public function getSnapshotAttributes()
44
    {
45
        /** @noinspection PhpUndefinedFieldInspection */
46
        return property_exists($this, 'snapshotAttributes') ? $this->snapshotAttributes : [];
0 ignored issues
show
Bug introduced by
The property snapshotAttributes 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...
47
    }
48
49
    protected function proxy()
50
    {
51
        /* @var SnapshotMeta $meta */
52
        $args     = func_get_args();
53
        $method   = array_shift($args);
54
        $meta     = $this->getSnapshotMeta();
55
        $entity   = $meta->getEntity();
56
        $callback = [$entity, $method];
57
58
        if (!is_callable($callback)) {
59
            throw new \BadMethodCallException(sprintf(
60
                'Proxy error: Method "%s" does not exist in proxied "%s"',
61
                 $method, get_class($entity)
62
            ));
63
        }
64
65
        $return = call_user_func_array($callback, $args);
66
67
        return $return === $entity ? $this : $return;
68
    }
69
70
    protected function inaccessible($property)
71
    {
72
        throw new \DomainException(sprintf(
73
            'Property "%s" of "%s" must not be accessed directly. Please retrieve from original entity via getSnaphotMeta()->getEntity()',
74
            $property, get_class($this)
75
        ));
76
    }
77
78
    protected function immutable($property)
79
    {
80
        throw new ImmutablePropertyException($property, $this);
81
    }
82
}