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
Branch develop (4efcd8)
by Maximilian
04:45
created

ClassMetadata::checkProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

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