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 ( 3c229d...169a4f )
by François
109:09 queued 100:54
created

Session::setCanary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A Session::domainBinding() 0 4 1
1
<?php
2
/**
3
 *  Copyright (C) 2017 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 DateInterval;
22
use DateTime;
23
use SURFnet\VPN\Common\Http\Exception\HttpException;
24
25
class Session extends Cookie implements SessionInterface
26
{
27
    /** @var array */
28
    private $sessionOptions;
29
30
    /**
31
     * @param array $sessionOptions
32
     */
33
    public function __construct(array $sessionOptions = [])
34
    {
35
        $this->sessionOptions = array_merge(
36
            [
37
                'DomainBinding' => null,       // also bind session to Domain
38
                'PathBinding' => null,         // also bind session to Path
39
            ],
40
            $sessionOptions
41
        );
42
43
        parent::__construct($sessionOptions);
44
45
        if (PHP_SESSION_ACTIVE !== session_status()) {
46
            session_start();
47
        }
48
49
        $this->sessionCanary();
50
        $this->domainBinding();
51
        $this->pathBinding();
52
53
        $this->replace(session_name(), session_id());
54
    }
55
56
    public function regenerate($deleteOldSession = false)
57
    {
58
        session_regenerate_id($deleteOldSession);
59
        $this->replace(session_name(), session_id());
60
    }
61
62
    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...
63
    {
64
        $_SESSION[$key] = $value;
65
    }
66
67
    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...
68
    {
69
        unset($_SESSION[$key]);
70
    }
71
72
    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...
73
    {
74
        return array_key_exists($key, $_SESSION);
75
    }
76
77
    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...
78
    {
79
        if (!$this->has($key)) {
80
            return null;
81
        }
82
83
        return $_SESSION[$key];
84
    }
85
86
    public function destroy()
0 ignored issues
show
Coding Style introduced by
destroy 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...
87
    {
88
        $_SESSION = [];
89
        $this->regenerate(true);
90
    }
91
92
    private function sessionCanary()
0 ignored issues
show
Coding Style introduced by
sessionCanary 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...
93
    {
94
        $dateTime = new DateTime();
95
        if (!array_key_exists('Canary', $_SESSION)) {
96
            $_SESSION = [];
97
            $this->regenerate(true);
98
            $_SESSION['Canary'] = $dateTime->format('Y-m-d H:i:s');
99
        } else {
100
            $canaryDateTime = new DateTime($_SESSION['Canary']);
101
            $canaryDateTime->add(new DateInterval('PT01H'));
102
            if ($canaryDateTime < $dateTime) {
103
                $this->regenerate(true);
104
                $_SESSION['Canary'] = $dateTime->format('Y-m-d H:i:s');
105
            }
106
        }
107
    }
108
109
    private function domainBinding()
110
    {
111
        $this->sessionBinding('DomainBinding');
112
    }
113
114
    private function pathBinding()
115
    {
116
        $this->sessionBinding('PathBinding');
117
    }
118
119
    private function sessionBinding($key)
0 ignored issues
show
Coding Style introduced by
sessionBinding 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...
120
    {
121
        if (!is_null($this->sessionOptions[$key])) {
122
            if (!array_key_exists($key, $_SESSION)) {
123
                $_SESSION[$key] = $this->sessionOptions[$key];
124
            }
125
            if ($this->sessionOptions[$key] !== $_SESSION[$key]) {
126
                throw new HttpException(
127
                    sprintf('session bound to %s "%s", expected "%s"', $key, $_SESSION[$key], $this->sessionOptions[$key]),
128
                    400
129
                );
130
            }
131
        }
132
    }
133
}
134