Completed
Push — master ( 1f9a78...118fca )
by Emily
02:07
created

PropertyAccessTrait::initPropertyAccessTrait()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of the Composite Utils package.
4
 *
5
 * (c) Emily Shepherd <[email protected]>
6
 *
7
 * For the full copyright and licence information, please view the
8
 * LICENSE.md file that was distributed with this source code.
9
 *
10
 * @package spaark/composite-utils
11
 * @author Emily Shepherd <emily@emilyshepherd>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Traits;
16
17
use Spaark\CompositeUtils\Service\ConditionalPropertyAccessor;
18
use Spaark\CompositeUtils\Factory\Reflection\ReflectionCompositeFactory;
19
20
trait PropertyAccessTrait
21
{
22
    protected static $reflectionComposite;
23
24 4
    protected static function getReflectionComposite()
25
    {
26 4
        if (!static::$reflectionComposite)
27
        {
28
            static::$reflectionComposite =
29 1
                ReflectionCompositeFactory::fromClassName
30
                (
31
                    get_called_class()
32
                )
33 1
                ->build();
34
        }
35
36 4
        return static::$reflectionComposite;
37
    }
38
39
    /**
40
     * @var ConditionalPropertyAccessor
41
     */
42
    protected $accessor;
43
44 4
    public function __construct()
45
    {
46 4
        $this->initPropertyAccessTrait();
47 4
    }
48
49 4
    protected function initPropertyAccessTrait()
50
    {
51 4
        $this->accessor = new ConditionalPropertyAccessor
52
        (
53
            $this,
54 4
            self::getReflectionComposite()
55
        );
56 4
    }
57
58 2
    public function __get($property)
59
    {
60 2
        return $this->accessor->getValue($property);
61
    }
62
63 2
    public function __set($property, $value)
64
    {
65 2
        $this->accessor->setValue($property, $value);
66 1
    }
67
}
68