RawPropertyAccessor   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 113
ccs 30
cts 30
cp 1
rs 10
c 2
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setRawValue() 0 9 1
A getRawValue() 0 10 1
A rawAddToValue() 0 9 1
A getPropertyOrFail() 0 19 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 license 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 <[email protected]>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Service;
16
17
use Spaark\CompositeUtils\Exception\CannotWritePropertyException;
18
use Spaark\CompositeUtils\Exception\CannotReadPropertyException;
19
use \ReflectionClass;
20
use \ReflectionProperty;
21
use \ReflectionException;
22
23
/**
24
 * This class is used to access the properties of a class
25
 */
26
class RawPropertyAccessor
27
{
28
    /**
29
     * The object being accessed
30
     *
31
     * @var object
32
     */
33
    protected $object;
34
35
    /**
36
     * A PHP Reflector for access
37
     *
38
     * @var ReflectionClass
39
     */
40
    protected $reflector;
41
42
    /**
43
     * Creates a RawPropertyAccessor for the given object
44
     *
45
     * @param object $object The object to access
46
     */
47 68
    public function __construct($object)
48
    {
49 68
        $this->object = $object;
50 68
        $this->reflector = new ReflectionClass($object);
51 68
    }
52
53
    /**
54
     * Access a property and sets it with the given value, irrespective
55
     * of access permissions
56
     *
57
     * @param string $key The property to write to
58
     * @param mixed $value The value to set
59
     * @throws CannotWritePropertyException In the event the property
60
     *     does not exist
61
     */
62 37
    public function setRawValue($key, $value)
63
    {
64 37
        $this->getPropertyOrFail
65
        (
66 37
            $key,
67 37
            CannotWritePropertyException::class
68
        )
69 37
        ->setValue($this->object, $value);
70 37
    }
71
72
    /**
73
     * Access a property and returns its value, irresepective of access
74
     * permissions
75
     *
76
     * @param string $key The property to read
77
     * @return mixed The property's value
78
     * @throws CannotReadPropertyException In the event the property
79
     *     does not exist
80
     */
81 75
    public function getRawValue($key)
82
    {
83
        return
84 75
            $this->getPropertyOrFail
85
            (
86 75
                $key,
87 75
                CannotReadPropertyException::class
88
            )
89 74
            ->getValue($this->object);
90
    }
91
92
    /**
93
     * Adds a value to a property, irrespective of access permissions
94
     *
95
     * @param string $key The property to write to
96
     * @param mixed $value The value to add
97
     * @deprecated
98
     * @throws CannotWritePropertyException In the event the property
99
     *    does not exist
100
     */ 
101 21
    public function rawAddToValue($key, $value)
102
    {
103 21
        $property = $this->getPropertyOrFail
104
        (
105 21
            $key,
106 21
            CannotWritePropertyException::class
107
        );
108 21
        $property->getValue($this->object)->add($value);
109 21
    }
110
111
    /**
112
     * Checks if a property exists and returns its ReflectionProperty
113
     *
114
     * @param string $key The property to access
115
     * @param string $class The name of the exception to throw on error
116
     * @throws $class If the property does not exist
117
     * @return ReflectionProperty
118
     */
119 77
    private function getPropertyOrFail($key, $class)
120
    {
121
        try
122
        {
123 77
            $prop = $this->reflector->getProperty($key);
124 74
            $prop->setAccessible(true);
125
126 74
            return $prop;
127
        }
128 3
        catch (ReflectionException $e)
129
        {
130 3
            throw new $class
131
            (
132 3
                get_class($this->object),
133 3
                $key,
134 3
                $e
135
            );
136
        }
137
    }
138
}
139