CsrfTokenFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 12 2
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\Authentication\Csrf;
6
7
use Nette\SmartObject;
8
use Nette\Http\Session;
9
use Nette\Utils\Random;
10
11
final class CsrfTokenFactory implements CsrfTokenFactoryInterface
12
{
13
	use SmartObject;
14
15
	/** @var \Nette\Http\Session  */
16
	private $session;
17
18
	/**
19
	 * @param \Nette\Http\Session $session
20
	 */
21
	public function __construct(Session $session)
22
	{
23
		$this->session = $session;
24
	}
25
26
	/**
27
	 * {@inheritdoc}
28
	 */
29
	public function create(string $component = ''): string
30
	{
31
		$section = $this->session->getSection(__CLASS__);
32
33
		if (!isset($section['token'])) {
34
			$section['token'] = 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