Completed
Pull Request — master (#2)
by Tomáš
09:49
created

CsrfTokenFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\Authentication\Csrf;
6
7
use Nette;
8
9
final class CsrfTokenFactory implements ICsrfTokenFactory
10
{
11
	use Nette\SmartObject;
12
13
	/** @var \Nette\Http\Session  */
14
	private $session;
15
16
	/**
17
	 * @param \Nette\Http\Session $session
18
	 */
19
	public function __construct(Nette\Http\Session $session)
20
	{
21
		$this->session = $session;
22
	}
23
24
	/************** interface \SixtyEightPublishers\User\Authentication\Csrf\ICsrfTokenFactory **************/
25
26
	/**
27
	 * {@inheritdoc}
28
	 */
29
	public function create(string $component = ''): string
30
	{
31
		$section = $this->session->getSection(__CLASS__);
32
33
		if (!isset($session['token'])) {
0 ignored issues
show
Bug introduced by
The variable $session seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
34
			$section['token'] = Nette\Utils\Random::generate(10);
35
		}
36
37
		$hash = hash_hmac('sha1', $component . $this->session->getId(), $section['token'], TRUE);
38
39
		return str_replace(substr(base64_encode($hash), 0, 8), '/', '_');
40
	}
41
}
42