Completed
Push — master ( 450ed3...cc0280 )
by michael
02:49
created

U2fPreRegistrationEvent   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 80
rs 10
c 0
b 0
f 0
ccs 19
cts 19
cp 1
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getAppId() 0 3 1
A getReason() 0 3 1
A __construct() 0 4 1
A abort() 0 5 1
A getUser() 0 3 1
A getName() 0 3 1
A isAborted() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the U2F Security bundle.
5
 *
6
 * (c) Michael Barbey <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mbarbey\U2fSecurityBundle\Event\Registration;
13
14
use Symfony\Component\EventDispatcher\Event;
15
use Mbarbey\U2fSecurityBundle\Model\User\U2fUserInterface;
16
17
/**
18
 * Pre U2F registration event
19
 *
20
 * An event dispatched right before performing a key registration.
21
 *
22
 * This event is the last chance to cancel the registration before it will be asked to the user.
23
 *
24
 * This event contain the user who asked to register a key and the appId (protocol + domain name).
25
 * You can call the "abort" function if you want to cancel the authentication request.
26
 *
27
 * @author Michael Barbey <[email protected]>
28
 */
29
class U2fPreRegistrationEvent extends Event
30
{
31
    private $user;
32
    private $appId;
33
    private $reason;
34
    private $abort = false;
35
36
    /**
37
     * Return the name of the event to use when dispatching this event.
38
     *
39
     * @return string
40
     */
41 1
    public static function getName(): string
42
    {
43 1
        return 'u2f.registration.before';
44
    }
45
46
    /**
47
     * @param string $appId             The appId for the registration
48
     * @param U2fUserInterface $user    The user who will register a security key
49
     */
50 5
    public function __construct(string $appId, U2fUserInterface $user)
51
    {
52 5
        $this->appId = $appId;
53 5
        $this->user = $user;
54 5
    }
55
56
    /**
57
     * Return the user who asked to register a securtiy key
58
     *
59
     * @return U2fUserInterface
60
     */
61 1
    public function getUser(): U2fUserInterface
62
    {
63 1
        return $this->user;
64
    }
65
66
    /**
67
     * Return the appId of the registration.
68
     * The appId *must* be the protocol + your domain name (ex: https://example.com)
69
     *
70
     * @return string
71
     */
72 1
    public function getAppId(): string
73
    {
74 1
        return $this->appId;
75
    }
76
77
    /**
78
     * Cancel the kex registration request. You can attach a reason why it was canceled.
79
     *
80
     * Calling this function stop the propagation of the event.
81
     *
82
     * @param string $reason
83
     */
84 5
    public function abort($reason = null): void
85
    {
86 5
        $this->reason = $reason;
87 5
        $this->abort = true;
88 5
        $this->stopPropagation();
89 5
    }
90
91
    /**
92
     * Return if the key registration request must be aborted or not.
93
     *
94
     * @return bool
95
     */
96 1
    public function isAborted(): bool
97
    {
98 1
        return $this->abort;
99
    }
100
101
    /**
102
     * Return the reason why the key registration request was aborted.
103
     *
104
     * @return string|NULL
105
     */
106 1
    public function getReason(): ?string
107
    {
108 1
        return $this->reason;
109
    }
110
}