Completed
Push — master ( 8f2b16...c80f65 )
by Marcel
03:24
created

U2FRegistrationSubscriber   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 19.05%

Importance

Changes 0
Metric Value
eloc 21
c 0
b 0
f 0
dl 0
loc 38
ccs 4
cts 21
cp 0.1905
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onRegister() 0 19 1
A __construct() 0 3 1
A getSubscribedEvents() 0 4 1
1
<?php
2
3
namespace App\EventSubscriber;
4
5
use App\Entity\U2fKey;
6
use App\Entity\User;
7
use App\Repository\U2fKeyRepositoryInterface;
8
use R\U2FTwoFactorBundle\Event\RegisterEvent;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
12
13
class U2FRegistrationSubscriber implements EventSubscriberInterface {
14
15
    private $repository;
16
    private $urlGenerator;
17
18 7
    public function __construct(U2fKeyRepositoryInterface $repository, UrlGeneratorInterface $urlGenerator) {
19 7
        $this->repository = $repository;
20 7
        $this->urlGenerator = $urlGenerator;
21 7
    }
22
23
    public function onRegister(RegisterEvent $event) {
24
        /** @var User $user */
25
        $user = $event->getUser();
26
        $data = $event->getRegistration();
27
28
        $key = (new U2fKey())
29
            ->setName($event->getKeyName())
30
            ->setPublicKey($data->publicKey)
31
            ->setKeyHandle($data->keyHandle)
32
            ->setCertificate($data->certificate)
33
            ->setCounter($data->counter)
34
            ->setUser($user);
35
36
        $user->addU2FKey($key);
37
38
        $this->repository->persist($key);
39
40
        $response = new RedirectResponse($this->urlGenerator->generate('two_factor'));
41
        $event->setResponse($response);
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public static function getSubscribedEvents() {
48
        return [
49
            'r_u2f_two_factor.register' => [
50
                [ 'onRegister', 0]
51
            ]
52
        ];
53
    }
54
}