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 ( c4e24d...f6700a )
by Vermeulen
02:03
created

Form::hasToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace BFW;
4
5
use \DateTime;
6
use \Exception;
7
use \stdClass;
8
9
/**
10
 * Class to manage html form
11
 * Actuely, only manage token for form.
12
 */
13
class Form
14
{
15
    /**
16
     * @var string $formId The form id 
17
     */
18
    protected $formId = '';
19
20
    /**
21
     * Constructor
22
     * Define form's id.
23
     * 
24
     * @param string $formId The form id
25
     */
26
    public function __construct($formId)
27
    {
28
        $this->formId = (string) $formId;
29
    }
30
31
    /**
32
     * Save the form's token
33
     * 
34
     * @param \stdClass $saveInfos Infos about token (id and expire time)
35
     * 
36
     * @return void
37
     */
38
    protected function saveToken($saveInfos)
39
    {
40
        //Default to session
41
        $this->saveTokenInSession($saveInfos);
42
    }
43
44
    /**
45
     * Save a token in php session
46
     * 
47
     * @global array $_SESSION
48
     * 
49
     * @param \stdClass $saveInfos Infos about token (id and expire time)
50
     * 
51
     * @return void
52
     */
53
    protected function saveTokenInSession($saveInfos)
54
    {
55
        global $_SESSION;
56
57
        $_SESSION['token'][$this->formId] = $saveInfos;
58
    }
59
60
    /**
61
     * Get the token informations
62
     * 
63
     * @return \stdClass
64
     */
65
    protected function getToken()
66
    {
67
        //Default from session
68
        return $this->getTokenFromSession();
69
    }
70
71
    /**
72
     * Get a token from the session
73
     * 
74
     * @global array $_SESSION
75
     * 
76
     * @return \stdClass
77
     * 
78
     * @throws Exception If there are no token
79
     */
80
    protected function getTokenFromSession()
81
    {
82
        global $_SESSION;
83
84
        if (!isset($_SESSION['token'])) {
85
            throw new Exception('no token found');
86
        }
87
88
        if (!isset($_SESSION['token'][$this->formId])) {
89
            throw new Exception('no token found for form id '.$this->formId);
90
        }
91
92
        return $_SESSION['token'][$this->formId];
93
    }
94
95
    /**
96
     * Create a token for the form and return the token
97
     * 
98
     * @return string
99
     * 
100
     * @throws Exception If the form id is undefined
101
     */
102
    public function createToken()
103
    {
104
        if (empty($this->formId)) {
105
            throw new Exception('Form id is undefined.');
106
        }
107
108
        $token    = uniqid(rand(), true);
109
        $datetime = new DateTime;
110
111
        $saveInfos        = new stdClass;
112
        $saveInfos->token = $token;
113
        $saveInfos->date  = $datetime;
114
115
        $this->saveToken($saveInfos);
116
        return $token;
117
    }
118
119
    /**
120
     * Check the token receive with the generated token
121
     * 
122
     * @param string $tokenToCheck The token receive from user
123
     * @param int $timeExp (default 15) time on minute which the token is valid
124
     * 
125
     * @return boolean
126
     */
127
    public function checkToken($tokenToCheck, $timeExp = 15)
128
    {
129
        //Throw Exception
130
        $tokenInfos = $this->getToken();
131
132
        $token      = $tokenInfos->token;
133
        $dateCreate = $tokenInfos->date;
134
135
        if ($token !== $tokenToCheck) {
136
            return false;
137
        }
138
139
        $limitDate = new DateTime;
140
        $limitDate->modify('-'.(int) $timeExp.' minutes');
141
142
        if ($dateCreate < $limitDate) {
143
            return false;
144
        }
145
146
        unset($_SESSION['token'][$this->formId]);
147
        return true;
148
    }
149
    
150
    /**
151
     * Check if the form has a token
152
     * 
153
     * @return boolean
154
     */
155
    public function hasToken()
156
    {
157
        try {
158
            $this->getToken();
159
        } catch (Exception $e) {
160
            return false;
161
        }
162
        
163
        return true;
164
    }
165
}
166