GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#31)
by Maximilian
02:15
created

ClassMetadata::getPropertyValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.7691

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 7
cts 11
cp 0.6364
rs 9.2
cc 4
eloc 11
nc 4
nop 3
crap 4.7691
1
<?php
2
3
/*
4
 * This file is part of the Sententiaregum project.
5
 *
6
 * (c) Maximilian Bosch <[email protected]>
7
 * (c) Ben Bieler <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Ma27\ApiKeyAuthenticationBundle\Model\User;
14
15
use ReflectionClass;
16
use ReflectionProperty;
17
18
/**
19
 * Metadata object that contains the necessary data of the user model.
20
 */
21
class ClassMetadata
22
{
23
    const LOGIN_PROPERTY = 1;
24
    const PASSWORD_PROPERTY = 2;
25
    const API_KEY_PROPERTY = 3;
26
    const LAST_ACTION_PROPERTY = 4;
27
28
    /**
29
     * @var int[]
30
     */
31
    private static $necessaryProperties = array(
32
        self::LOGIN_PROPERTY    => 'login',
33
        self::PASSWORD_PROPERTY => 'password',
34
        self::API_KEY_PROPERTY  => 'apiKey',
35
    );
36
37
    /**
38
     * @var ReflectionClass
39
     */
40
    private $reflectionInstance;
41
42
    /**
43
     * @var string
44
     */
45
    private $className;
46
47
    /**
48
     * @var ReflectionProperty[]
49
     */
50
    private $properties = array();
51
52
    /**
53
     * @var string[]
54
     */
55
    private $lazyPropertyNameCache = array();
56
57
    /**
58
     * @var string[]
59
     */
60
    private $lazyValueCache = array();
61
62
    /**
63
     * Constructor.
64
     *
65
     * @param ReflectionClass      $reflection
66
     * @param string               $className
67
     * @param ReflectionProperty[] $properties
68
     *
69
     * @throws \InvalidArgumentException If one necessary property is missing
70
     */
71 5
    public function __construct(ReflectionClass $reflection, $className, array $properties)
72
    {
73 5
        $this->reflectionInstance = $reflection;
74 5
        $this->className = (string) $className;
75 5
        $this->properties = $properties;
76
77 5
        foreach (self::$necessaryProperties as $propertyIndex => $alias) {
78 5
            if (!isset($this->properties[$propertyIndex])) {
79
                throw new \InvalidArgumentException(sprintf(
80
                    'Missing require property "%s"!',
81
                    $alias
82
                ));
83
            }
84 5
        }
85 5
    }
86
87
    /**
88
     * Gets the value of the given property.
89
     *
90
     * @param object $user
91
     * @param int    $property
92
     * @param bool   $strict
93
     *
94
     * @return mixed
95
     */
96 2
    public function getPropertyValue($user, $property = self::LOGIN_PROPERTY, $strict = false)
97
    {
98 2
        if ($this->checkProperty($property, $strict)) {
99 2
            $oid = spl_object_hash($user);
100 2
            if (isset($this->lazyValueCache[$oid])) {
101
                if (isset($this->lazyValueCache[$oid][$property])) {
102
                    return $this>$this->lazyValueCache[$oid][$property];
103
                }
104
            } else {
105 2
                $this->lazyValueCache[$oid] = array();
106
            }
107
108 2
            $this->properties[$property]->setAccessible(true);
109 2
            return $this->lazyValueCache[$oid][$property] = $this->properties[$property]->getValue($user);
110
        }
111
112
        return null;
113
    }
114
115
    /**
116
     * Gets the name of a specific property by its metadata constant.
117
     *
118
     * @param int  $property
119
     * @param bool $strict
120
     *
121
     * @return null|string
122
     */
123 4
    public function getPropertyName($property = self::LOGIN_PROPERTY, $strict = false)
124
    {
125 4
        if ($this->checkProperty($property, $strict)) {
126 4
            if (isset($this->lazyPropertyNameCache[$property])) {
127 3
                return $this->lazyPropertyNameCache[$property];
128
            }
129
130 4
            return $this->lazyPropertyNameCache[$property] = $this->properties[$property]->getName();
131
        }
132
133
        return null;
134
    }
135
136
    /**
137
     * Modifies a property and clears the cache.
138
     *
139
     * @param object $user
140
     * @param mixed  $newValue
141
     * @param int    $property
142
     */
143 2
    public function modifyProperty($user, $newValue, $property = self::LOGIN_PROPERTY)
144
    {
145 2
        $this->checkProperty($property, true);
146
147 2
        $propertyObject = $this->properties[$property];
148 2
        $propertyObject->setAccessible(true);
149 2
        $propertyObject->setValue($user, $newValue);
150
151 2
        unset($this->lazyValueCache[spl_object_hash($user)]);
152 2
    }
153
154
    /**
155
     * Validates a property.
156
     *
157
     * @param int  $property
158
     * @param bool $strict
159
     *
160
     * @return bool
161
     */
162 4
    private function checkProperty($property = self::LOGIN_PROPERTY, $strict = false)
163
    {
164 4
        if (!isset($this->properties[$property])) {
165
            if ($strict) {
166
                throw new \LogicException(sprintf(
167
                    'Cannot get property "%s"!',
168
                    $property
169
                ));
170
            }
171
172
            return false;
173
        }
174
175 4
        return true;
176
    }
177
}
178