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 — master ( 6f77c2...a92bfc )
by François
02:32
created

Session::startSession()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
rs 8.439
cc 6
eloc 13
nc 13
nop 0
1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace SURFnet\VPN\Common\Http;
20
21
use SURFnet\VPN\Common\Http\Exception\HttpException;
22
23
class Session implements SessionInterface
24
{
25
    /** @var string */
26
    private $serverName;
27
28
    /** @var string */
29
    private $requestRoot;
30
31
    /** @var bool */
32
    private $secureOnly;
33
34
    public function __construct($serverName, $requestRoot, $secureOnly)
35
    {
36
        $this->serverName = $serverName;
37
        $this->requestRoot = $requestRoot;
38
        $this->secureOnly = $secureOnly;
39
    }
40
41
    public function set($key, $value)
0 ignored issues
show
Coding Style introduced by
set uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
42
    {
43
        $this->startSession();
44
        $_SESSION[$key] = $value;
45
    }
46
47
    public function delete($key)
0 ignored issues
show
Coding Style introduced by
delete uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
48
    {
49
        $this->startSession();
50
        if ($this->has($key)) {
51
            unset($_SESSION[$key]);
52
        }
53
    }
54
55
    public function has($key)
0 ignored issues
show
Coding Style introduced by
has uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
56
    {
57
        $this->startSession();
58
59
        return array_key_exists($key, $_SESSION);
60
    }
61
62
    public function get($key)
0 ignored issues
show
Coding Style introduced by
get uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
63
    {
64
        $this->startSession();
65
        if ($this->has($key)) {
66
            return $_SESSION[$key];
67
        }
68
    }
69
70
    public function destroy()
71
    {
72
        if ('' !== session_id()) {
73
            // session already started
74
            return;
75
        }
76
        session_destroy();
77
    }
78
79
    private function startSession()
0 ignored issues
show
Coding Style introduced by
startSession uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
80
    {
81
        if ('' !== session_id()) {
82
            // session already started
83
            return;
84
        }
85
86
        session_set_cookie_params(0, $this->requestRoot, $this->serverName, $this->secureOnly, true);
87
        session_start();
88
89
        // Make sure we have a canary set
90
        if (!isset($_SESSION['canary'])) {
91
            $this->setCanary($this->serverName, $this->requestRoot);
92
        }
93
        // Regenerate session ID every five minutes:
94
        if ($_SESSION['canary'] < time() - 300) {
95
            $this->setCanary($this->serverName, $this->requestRoot);
96
        }
97
98
        if ($this->serverName !== $_SESSION['serverName']) {
99
            throw new HttpException('session error (serverName)', 400);
100
        }
101
102
        if ($this->requestRoot !== $_SESSION['requestRoot']) {
103
            throw new HttpException('session error (requestRoot)', 400);
104
        }
105
    }
106
107
    private function setCanary($serverName, $requestRoot)
0 ignored issues
show
Coding Style introduced by
setCanary uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
108
    {
109
        session_regenerate_id(true);
110
        $_SESSION['canary'] = time();
111
        $_SESSION['serverName'] = $serverName;
112
        $_SESSION['requestRoot'] = $requestRoot;
113
    }
114
}
115