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
Push — 3.0 ( 5430f4...07b64a )
by Vermeulen
03:09
created

Form   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 186
rs 10
c 0
b 0
f 0
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A saveToken() 0 4 1
A saveTokenInSession() 0 5 1
A __construct() 0 6 2
A obtainToken() 0 3 1
A getFormId() 0 3 1
A obtainTokenFromSession() 0 16 3
A hasToken() 0 9 2
A createToken() 0 12 1
A checkToken() 0 23 3
1
<?php
2
3
namespace BFW\Helpers;
4
5
use \DateTime;
6
use \Exception;
7
8
/**
9
 * Class to manage html forms
10
 * Only manage form's token.
11
 */
12
class Form
13
{
14
    /**
15
     * @const ERR_NO_TOKEN Exception code if there is no token declared
16
     */
17
    const ERR_NO_TOKEN = 1606001;
18
    
19
    /**
20
     * @const ERR_NO_TOKEN_FOR_FORM_ID Exception code if there is no token for
21
     * the form id.
22
     */
23
    const ERR_NO_TOKEN_FOR_FORM_ID = 1606002;
24
    
25
    /**
26
     * @const ERR_FORM_ID_EMPTY Exception code if the form id is not declared.
27
     */
28
    const ERR_FORM_ID_EMPTY = 1606003;
29
    
30
    /**
31
     * @var string $formId The form id 
32
     */
33
    protected $formId = '';
34
35
    /**
36
     * Constructor
37
     * Define the form's id.
38
     * 
39
     * @param string $formId The form's id
40
     */
41
    public function __construct(string $formId)
42
    {
43
        $this->formId = $formId;
44
        
45
        if (empty($this->formId)) {
46
            throw new Exception('Form id is empty.', $this::ERR_FORM_ID_EMPTY);
47
        }
48
    }
49
    
50
    /**
51
     * Getter accessor to the property formId
52
     * 
53
     * @return string
54
     */
55
    public function getFormId(): string
56
    {
57
        return $this->formId;
58
    }
59
60
    /**
61
     * Save the form's token
62
     * 
63
     * @param object $saveInfos Infos about token (id and expire time)
64
     * 
65
     * @return void
66
     */
67
    protected function saveToken($saveInfos)
68
    {
69
        //Default to session
70
        $this->saveTokenInSession($saveInfos);
71
    }
72
73
    /**
74
     * Save a token in php session
75
     * 
76
     * @global array $_SESSION
77
     * 
78
     * @param object $saveInfos Infos about token (id and expire time)
79
     * 
80
     * @return void
81
     */
82
    protected function saveTokenInSession($saveInfos)
83
    {
84
        global $_SESSION;
85
86
        $_SESSION['formsTokens'][$this->formId] = $saveInfos;
87
    }
88
89
    /**
90
     * Get the token informations
91
     * 
92
     * @return object
93
     */
94
    protected function obtainToken()
95
    {
96
        return $this->obtainTokenFromSession();
97
    }
98
99
    /**
100
     * Get a token from the session
101
     * 
102
     * @global array $_SESSION
103
     * 
104
     * @return object
105
     * 
106
     * @throws \Exception If there are no token
107
     */
108
    protected function obtainTokenFromSession()
109
    {
110
        global $_SESSION;
111
112
        if (!isset($_SESSION['formsTokens'])) {
113
            throw new Exception('no token found', $this::ERR_NO_TOKEN);
114
        }
115
116
        if (!isset($_SESSION['formsTokens'][$this->formId])) {
117
            throw new Exception(
118
                'no token found for the form id '.$this->formId,
119
                $this::ERR_NO_TOKEN_FOR_FORM_ID
120
            );
121
        }
122
123
        return $_SESSION['formsTokens'][$this->formId];
124
    }
125
126
    /**
127
     * Create a token for the form and return the token
128
     * 
129
     * @param int $expire (default 15) time on minute during which the
130
     *  token is valid
131
     * 
132
     * @return string
133
     * 
134
     * @throws \Exception If the form id is undefined
135
     */
136
    public function createToken(int $expire = 15): string
137
    {
138
        $token = uniqid(rand(), true);
139
        
140
        $saveInfos = (object) [
141
            'token' => $token,
142
            'date'  => new DateTime,
143
            'expire' => $expire
144
        ];
145
146
        $this->saveToken($saveInfos);
147
        return $saveInfos->token;
148
    }
149
150
    /**
151
     * Check the token receive with the generated token
152
     * 
153
     * @param string $tokenToCheck The token receive from user
154
     * 
155
     * @throws \Exception If the token not exist
156
     * 
157
     * @return boolean
158
     */
159
    public function checkToken(string $tokenToCheck): bool
160
    {
161
        //Throw Exception
162
        $tokenInfos = $this->obtainToken();
163
164
        $token      = $tokenInfos->token;
165
        $dateCreate = $tokenInfos->date;
166
        $timeExpire = $tokenInfos->expire;
167
168
        if ($token !== $tokenToCheck) {
169
            return false;
170
        }
171
172
        $limitDate = new DateTime;
173
        $limitDate->modify('-'.$timeExpire.' minutes');
174
        
175
        unset($_SESSION['formsTokens'][$this->formId]);
176
        
177
        if ($dateCreate < $limitDate) {
178
            return false;
179
        }
180
181
        return true;
182
    }
183
    
184
    /**
185
     * Check if the form has a token
186
     * 
187
     * @return boolean
188
     */
189
    public function hasToken(): bool
190
    {
191
        try {
192
            $this->obtainToken();
193
        } catch (Exception $e) {
194
            return false;
195
        }
196
        
197
        return true;
198
    }
199
}
200