Completed
Push — master ( ead5e5...fe31fe )
by Yann
02:34
created

TokenFactory::create()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 15
cts 15
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 3
nop 3
crap 4
1
<?php
2
3
namespace Yokai\SecurityTokenBundle\Factory;
4
5
use Yokai\SecurityTokenBundle\Configuration\TokenConfigurationRegistry;
6
use Yokai\SecurityTokenBundle\Entity\Token;
7
use Yokai\SecurityTokenBundle\InformationGuesser\InformationGuesserInterface;
8
use Yokai\SecurityTokenBundle\Manager\UserManagerInterface;
9
use Yokai\SecurityTokenBundle\Repository\TokenRepositoryInterface;
10
11
/**
12
 * @author Yann Eugoné <[email protected]>
13
 */
14
class TokenFactory implements TokenFactoryInterface
15
{
16
    /**
17
     * @var TokenConfigurationRegistry
18
     */
19
    private $registry;
20
21
    /**
22
     * @var InformationGuesserInterface
23
     */
24
    private $informationGuesser;
25
26
    /**
27
     * @var UserManagerInterface
28
     */
29
    private $userManager;
30
31
    /**
32
     * @var TokenRepositoryInterface
33
     */
34
    private $repository;
35
36
    /**
37
     * @param TokenConfigurationRegistry  $registry
38
     * @param InformationGuesserInterface $informationGuesser
39
     * @param UserManagerInterface        $userManager
40
     * @param TokenRepositoryInterface    $repository
41
     */
42 1
    public function __construct(
43
        TokenConfigurationRegistry $registry,
44
        InformationGuesserInterface $informationGuesser,
45
        UserManagerInterface $userManager,
46
        TokenRepositoryInterface $repository
47
    ) {
48 1
        $this->registry = $registry;
49 1
        $this->informationGuesser = $informationGuesser;
50 1
        $this->userManager = $userManager;
51 1
        $this->repository = $repository;
52 1
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 1
    public function create($user, $purpose, array $payload = [])
58
    {
59 1
        $configuration = $this->registry->get($purpose);
60
61 1
        $userClass = $this->userManager->getClass($user);
62 1
        $userId = $this->userManager->getId($user);
63
64 1
        if ($configuration->isUnique()) {
65 1
            $token = $this->repository->findExisting($userClass, $userId, $purpose);
66
67 1
            if ($token instanceof Token) {
68 1
                return $token;
69
            }
70
        }
71
72
        do {
73 1
            $value = $configuration->getGenerator()->generate();
74 1
        } while ($this->repository->exists($value, $purpose));
75
76 1
        $duration = $configuration->getDuration();
77 1
        $keep = $configuration->getKeep();
78 1
        $usages = $configuration->getUsages();
79 1
        $information = $this->informationGuesser->get();
80
81 1
        return new Token($userClass, $userId, $value, $purpose, $duration, $keep, $usages, $payload, $information);
82
    }
83
}
84