Completed
Pull Request — master (#1)
by Karsten
03:23
created

ScopedPropertyAccess   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 95.24%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 83
c 2
b 0
f 0
wmc 8
lcom 1
cbo 0
ccs 20
cts 21
cp 0.9524
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 13 2
A __construct() 0 1 1
A __wakeup() 0 7 2
A init() 0 6 1
A get() 0 6 1
A set() 0 5 1
1
<?php
2
/**
3
 * Created by gerk on 13.11.17 05:50
4
 */
5
6
namespace PeekAndPoke\Component\PropertyAccess;
7
8
/**
9
 * This implementation accesses properties through function call scoping.
10
 *
11
 * This is the slowest accessor with the fewest limitation. Consider using others when possible.
12
 *
13
 *
14
 * What can it do?
15
 *
16
 * - access public properties
17
 *  -> you should use PublicPropertyAccess for performance
18
 *
19
 * - access protected properties that are visible on the subject class
20
 *
21
 * - access private properties declared on the subject class
22
 *  -> use ReflectionPropertyAccess for performance
23
 *
24
 * - access private properties that are declared on a base class of the subject class
25
 *
26
 *
27
 * @author Karsten J. Gerber <[email protected]>
28
 */
29
class ScopedPropertyAccess implements PropertyAccess
30
{
31
    /** @var string */
32
    private $scopeClass;
33
    /** @var string */
34
    private $propertyName;
35
36
    /** @var bool */
37
    private static $initialized = false;
38
    /** @var \Closure */
39
    private static $getAccess;
40
    /** @var \Closure */
41
    private static $setAccess;
42
43
    /**
44
     * @param string $scopeClass   Fqcn of the scope that declares the property
45
     * @param string $propertyName The name of the property
46
     *
47
     * @return ScopedPropertyAccess
48
     */
49 4
    public static function create($scopeClass, $propertyName)
50
    {
51 4
        $ret               = new self;
52 4
        $ret->scopeClass   = $scopeClass;
53 4
        $ret->propertyName = $propertyName;
54
55
        // we need to at least initialize this class ones
56 4
        if (self::$initialized === false) {
57 1
            $ret->init();
58
        }
59
60 4
        return $ret;
61
    }
62
63
    /**
64
     * Make use of the static create method
65
     */
66
    private function __construct() { }
67
68
    /**
69
     * Called on unserialze
70
     */
71 1
    public function __wakeup()
72
    {
73
        // we need to at least initialize this class ones
74 1
        if (self::$initialized === false) {
75
            $this->init();
76
        }
77 1
    }
78
79 1
    private function init()
80
    {
81 1
        self::$initialized = true;
82
        self::$getAccess   = function ($prop) { return $this->$prop; };
83
        self::$setAccess   = function ($prop, $value) { $this->$prop = $value; };
84 1
    }
85
86
    /**
87
     * Get the value of the property
88
     *
89
     * @param mixed $subject The object to get the value from
90
     *
91
     * @return mixed
92
     */
93 2
    public function get($subject)
94
    {
95 2
        $func = self::$getAccess->bindTo($subject, $this->scopeClass);
96
97 2
        return $func($this->propertyName);
98
    }
99
100
    /**
101
     * Set the value of the property
102
     *
103
     * @param mixed $subject The object to set the value to
104
     * @param mixed $value
105
     */
106 2
    public function set($subject, $value)
107
    {
108 2
        $func = self::$setAccess->bindTo($subject, $this->scopeClass);
109 2
        $func($this->propertyName, $value);
110 2
    }
111
}
112