Completed
Push — develop ( f5bc45...3ea57c )
by
unknown
49:10 queued 12:24
created

SnapshotTrait::proxyClone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
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();
36
        $this->snapshotEntity       = $source;
0 ignored issues
show
Bug introduced by
The property snapshotEntity 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...
37
    }
38
39
    public function getOriginalEntity()
40
    {
41
        return $this->snapshotEntity;
42
    }
43
44
    public function getSnapshotMeta()
45
    {
46
        return $this->snapshotMeta;
47
    }
48
49
    public function getSnapshotAttributes()
50
    {
51
        /** @noinspection PhpUndefinedFieldInspection */
52
        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...
53
    }
54
55
    /**
56
     *
57
     *
58
     * @param string $method
59
     * @param mixed[]  ...$args Arguments to be passed to proxied method.
60
     *
61
     * @return SnapshotTrait
0 ignored issues
show
Comprehensibility Bug introduced by
The return type SnapshotTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
62
     */
63
    protected function proxy($method, ...$args)
64
    {
65
        $entity   = $this->getOriginalEntity();
66
        $callback = [$entity, $method];
67
68
        if (!is_callable($callback)) {
69
            throw new \BadMethodCallException(sprintf(
70
                'Proxy error: Method "%s" does not exist in proxied "%s"',
71
                 $method, get_class($entity)
72
            ));
73
        }
74
75
        $return = $callback(...$args);
76
77
        return $return === $entity ? $this : $return;
78
    }
79
80
    protected function proxyClone($method, ...$args)
81
    {
82
        $value = $this->proxy($method, ...$args);
83
        $return = is_object($value) ? clone $value : $value;
84
85
        return $return;
86
    }
87
88
    protected function inaccessible($property)
89
    {
90
        throw new \DomainException(sprintf(
91
            'Property "%s" of "%s" must not be accessed directly. Please retrieve from original entity via getSnaphotMeta()->getEntity()',
92
            $property, get_class($this)
93
        ));
94
    }
95
96
    protected function immutable($property)
97
    {
98
        throw new ImmutablePropertyException($property, $this);
99
    }
100
}
101