Passed
Pull Request — develop (#295)
by Peter
04:30
created

CookieValue::authenticationTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * Copyright 2022 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupGateway\GatewayBundle\Sso2fa\ValueObject;
20
21
use DateTime;
22
use function strtolower;
23
use function strtotime;
24
25
class CookieValue implements CookieValueInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CookieValue
Loading history...
26
{
27
    private $tokenId;
0 ignored issues
show
Coding Style introduced by
Private member variable "tokenId" must be prefixed with an underscore
Loading history...
28
    private $identityId;
0 ignored issues
show
Coding Style introduced by
Private member variable "identityId" must be prefixed with an underscore
Loading history...
29
    private $loa;
0 ignored issues
show
Coding Style introduced by
Private member variable "loa" must be prefixed with an underscore
Loading history...
30
    private $authenticationTime;
0 ignored issues
show
Coding Style introduced by
Private member variable "authenticationTime" must be prefixed with an underscore
Loading history...
31
32
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $identityId should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $secondFactorId should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $loa should have a doc-comment as per coding-style.
Loading history...
33
     * The cookie value consists of:
34
     * - Token used: SecondFactorId from SecondFactor
35
     * - Identifier: IdentityId from SecondFactor
36
     * - The resolved LoA: LoA (resolved using Loa resolution service)
37
     * - Authentication time (Atom formatted date time string)
38
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
39
    public static function from(string $identityId, string $secondFactorId, float $loa): self
40
    {
41
        $cookieValue = new self;
42
        $cookieValue->tokenId = $secondFactorId;
43
        $cookieValue->identityId = $identityId;
44
        $cookieValue->loa = $loa;
45
        $dateTime = new DateTime();
46
        $cookieValue->authenticationTime = $dateTime->format(DATE_ATOM);
47
        return $cookieValue;
48
    }
49
50
    public static function deserialize(string $serializedData): CookieValueInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function deserialize()
Loading history...
51
    {
52
        $data = json_decode($serializedData, true);
53
        $cookieValue = new self;
54
        $cookieValue->tokenId = $data['tokenId'];
55
        $cookieValue->identityId = $data['identityId'];
56
        $cookieValue->loa = (float) $data['loa'];
57
        $cookieValue->authenticationTime = $data['authenticationTime'];
58
59
        return $cookieValue;
60
    }
61
62
    public function serialize(): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function serialize()
Loading history...
63
    {
64
        return json_encode([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
65
            'tokenId' => $this->tokenId,
66
            'identityId' => $this->identityId,
67
            'loa' => $this->loa,
68
            'authenticationTime' => $this->authenticationTime,
69
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
70
    }
71
72
    public function meetsRequiredLoa(float $requiredLoa): bool
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function meetsRequiredLoa()
Loading history...
73
    {
74
        return $this->loa >= $requiredLoa;
75
    }
76
77
    public function getLoa(): float
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getLoa()
Loading history...
78
    {
79
        return $this->loa;
80
    }
81
82
    public function getIdentityId(): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getIdentityId()
Loading history...
83
    {
84
        return $this->identityId;
85
    }
86
87
    public function secondFactorId(): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function secondFactorId()
Loading history...
88
    {
89
        return $this->tokenId;
90
    }
91
92
    public function issuedTo(string $identityNameId): bool
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function issuedTo()
Loading history...
93
    {
94
        return strtolower($identityNameId) === strtolower($this->identityId);
95
    }
96
97
    public function authenticationTime(): int
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function authenticationTime()
Loading history...
98
    {
99
        return strtotime($this->authenticationTime);
100
    }
101
}
102