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

ClassMetadata::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4286
cc 3
eloc 9
nc 3
nop 3
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
    public function __construct(ReflectionClass $reflection, $className, array $properties)
72
    {
73
        $this->reflectionInstance = $reflection;
74
        $this->className = (string) $className;
75
        $this->properties = $properties;
76
77
        foreach (self::$necessaryProperties as $propertyIndex => $alias) {
78
            if (!isset($this->properties[$propertyIndex])) {
79
                throw new \InvalidArgumentException(sprintf(
80
                    'Missing require property "%s"!',
81
                    $alias
82
                ));
83
            }
84
        }
85
    }
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
    public function getPropertyValue($user, $property = self::LOGIN_PROPERTY, $strict = false)
97
    {
98
        if ($this->checkProperty($property, $strict)) {
99
            $oid = spl_object_hash($user);
100
            if (isset($this->lazyValueCache[$oid])) {
101
                if (isset($this->lazyValueCache[$oid][$property])) {
102
                    return $this>$this->lazyValueCache[$oid][$property];
103
                }
104
            } else {
105
                $this->lazyValueCache[$oid] = array();
106
            }
107
108
            $this->properties[$property]->setAccessible(true);
109
            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
    public function getPropertyName($property = self::LOGIN_PROPERTY, $strict = false)
124
    {
125
        if ($this->checkProperty($property, $strict)) {
126
            if (isset($this->lazyPropertyNameCache[$property])) {
127
                return $this->lazyPropertyNameCache[$property];
128
            }
129
130
            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
    public function modifyProperty($user, $newValue, $property = self::LOGIN_PROPERTY)
144
    {
145
        $this->checkProperty($property, true);
146
147
        $propertyObject = $this->properties[$property];
148
        $propertyObject->setAccessible(true);
149
        $propertyObject->setValue($user, $newValue);
150
151
        unset($this->lazyValueCache[spl_object_hash($user)]);
152
    }
153
154
    /**
155
     * Validates a property.
156
     *
157
     * @param int  $property
158
     * @param bool $strict
159
     *
160
     * @return bool
161
     */
162
    private function checkProperty($property = self::LOGIN_PROPERTY, $strict = false)
163
    {
164
        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
        return true;
176
    }
177
}
178