BadTokenLengthTrait::checkBadTokenLength()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 2
nop 1
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of the Linna Csrf Guard.
7
 *
8
 * @author Sebastian Rapetti <[email protected]>
9
 * @copyright (c) 2020, Sebastian Rapetti
10
 * @license http://opensource.org/licenses/MIT MIT License
11
 */
12
13
namespace Linna\CsrfGuard\Exception;
14
15
/**
16
 * Bad token length trait.
17
 * <p>Provide a method to check conditions to throw BadTokenLengthException.</p>
18
 */
19
trait BadTokenLengthTrait
20
{
21
    /**
22
     * Check for a wrong token length.
23
     *
24
     * @param int $tokenLength Token length in bytes.
25
     *
26
     * @return void
27
     *
28
     * @throws BadTokenLengthException If $tokenLength is less than <code>ExceptionBoundary::TOKEN_LENGTH_MIN</code>
29
     *                                 and greater than <code>ExceptionBoundary::TOKEN_LENGTH_MAX</code>.
30
     */
31
    protected function checkBadTokenLength(int $tokenLength): void
32
    {
33
        if ($tokenLength < ExceptionBoundary::TOKEN_LENGTH_MIN || $tokenLength > ExceptionBoundary::TOKEN_LENGTH_MAX) {
34
            throw new BadTokenLengthException(
35
                \sprintf(
36
                    "Token length must be between %d and %d",
37
                    ExceptionBoundary::TOKEN_LENGTH_MIN,
38
                    ExceptionBoundary::TOKEN_LENGTH_MAX
39
                )
40
            );
41
        }
42
    }
43
}
44