Test Failed
Push — dev ( 17f6d8...392257 )
by Janko
10:17 queued 12s
created

SessionStringFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 20
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createSessionString() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Session;
6
7
use Stu\Module\Control\StuTime;
8
use Stu\Orm\Entity\UserInterface;
9
use Stu\Orm\Repository\SessionStringRepositoryInterface;
10
11
class SessionStringFactory implements SessionStringFactoryInterface
12
{
13
    public function __construct(
14
        private SessionStringRepositoryInterface $sessionStringRepository,
15
        private StuTime $stuTime
16
    ) {}
17
18
    public function createSessionString(UserInterface $user): string
19
    {
20
        $string = bin2hex(random_bytes(15));
21
22
        $sessionString =
23
            $this->sessionStringRepository->prototype()
24
            ->setUser($user)
25
            ->setDate($this->stuTime->dateTime())
26
            ->setSessionString($string);
27
28
        $this->sessionStringRepository->save($sessionString);
29
30
        return $string;
31
    }
32
}
33