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:55
created

ClassMetadata::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.1825

Importance

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