Completed
Push — 3.7 ( 8061e7...a7d511 )
by
unknown
99:34 queued 52:50
created

ParameterConfirmationToken::genToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 11
rs 9.9
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ParameterConfirmationToken::getName() 0 3 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
require_once(dirname(__FILE__).'/AbstractConfirmationToken.php');
4
require_once(dirname(dirname(dirname(__FILE__))).'/view/TemplateGlobalProvider.php');
5
require_once(dirname(dirname(dirname(__FILE__))).'/control/Director.php');
6
7
/**
8
 * This is used to protect dangerous GET parameters that need to be detected early in the request
9
 * lifecycle by generating a one-time-use token & redirecting with that token included in the
10
 * redirected URL
11
 *
12
 * @internal This class is designed specifically for use pre-startup and may change without warning
13
 */
14
class ParameterConfirmationToken extends AbstractConfirmationToken {
15
16
	/**
17
	 * The name of the parameter
18
	 *
19
	 * @var string
20
	 */
21
	protected $parameterName = null;
22
23
	/**
24
	 * The parameter given
25
	 *
26
	 * @var string|null The string value, or null if not provided
27
	 */
28
	protected $parameter = null;
29
30
	/**
31
	 * Create a new ParameterConfirmationToken
32
	 *
33
	 * @param string $parameterName Name of the querystring parameter to check
34
	 */
35
	public function __construct($parameterName) {
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_GET 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...
36
		// Store the parameter name
37
		$this->parameterName = $parameterName;
38
39
		// Store the parameter value
40
		$this->parameter = isset($_GET[$parameterName]) ? $_GET[$parameterName] : null;
41
42
		// If the token provided is valid, mark it as such
43
		$token = isset($_GET[$parameterName.'token']) ? $_GET[$parameterName.'token'] : null;
44
		if ($this->checkToken($token)) {
45
			$this->token = $token;
46
		}
47
	}
48
49
	/**
50
	 * Get the name of this token
51
	 *
52
	 * @return string
53
	 */
54
	public function getName() {
55
		return $this->parameterName;
56
	}
57
58
	public function parameterProvided() {
59
		return $this->parameter !== null;
60
	}
61
62
	public function reloadRequired() {
63
		return $this->parameterProvided() && !$this->tokenProvided();
64
	}
65
66
	public function suppress() {
0 ignored issues
show
Coding Style introduced by
suppress uses the super-global variable $_GET 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...
67
		unset($_GET[$this->parameterName]);
68
	}
69
70
	public function params($includeToken = true) {
71
		$params = array(
72
			$this->parameterName => $this->parameter,
73
		);
74
		if ($includeToken) {
75
			$params[$this->parameterName . 'token'] = $this->genToken();
76
		}
77
		return $params;
78
	}
79
80
	public function getRedirectUrlBase() {
81
		return (!$this->parameterProvided()) ? Director::baseURL() : $this->currentAbsoluteURL();
82
	}
83
84
	public function getRedirectUrlParams() {
0 ignored issues
show
Coding Style introduced by
getRedirectUrlParams uses the super-global variable $_GET 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...
85
		$params = (!$this->parameterProvided())
86
			? $this->params()
87
			: array_merge($_GET, $this->params());
88
89
		if (isset($params['url'])) {
90
			unset($params['url']);
91
		}
92
93
		return $params;
94
	}
95
96
	protected function redirectURL() {
97
		$query = http_build_query($this->getRedirectUrlParams());
98
		return $this->getRedirectUrlBase() . '?' . $query;
99
	}
100
}
101