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

CsrfTokenFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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