Passed
Push — master ( 8ff4e3...ffee1f )
by Samuel
03:00
created

SecurityException   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 14
c 1
b 0
f 0
dl 0
loc 38
ccs 19
cts 19
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRemainingDelay() 0 3 1
A getSecurityType() 0 3 1
A __construct() 0 6 1
A getPublicMessage() 0 15 2
1
<?php
2
3
namespace App\Domain\Security\Exception;
4
5
use App\Domain\Security\Enum\SecurityType;
6
7
/**
8
 * Security throttle exception.
9
 */
10
class SecurityException extends \RuntimeException
11
{
12 22
    public function __construct(
13
        private readonly int|string $remainingDelay,
14
        private readonly SecurityType $securityType,
15
        string $message = 'Security check failed.'
16
    ) {
17 22
        parent::__construct($message);
18
    }
19
20
    /**
21
     * @return int|string int or 'captcha'
22
     */
23 22
    public function getRemainingDelay(): int|string
24
    {
25 22
        return $this->remainingDelay;
26
    }
27
28 22
    public function getSecurityType(): SecurityType
29
    {
30 22
        return $this->securityType;
31
    }
32
33 3
    public function getPublicMessage(): string
34
    {
35 3
        $userThrottleMessage = is_numeric($this->remainingDelay) ?
36 2
            sprintf(__('wait %s'), '<span class="throttle-time-span">' . $this->remainingDelay . '</span>s')
37 1
            : __('fill out the captcha');
38
39 3
        return match ($this->getSecurityType()) {
40 3
            SecurityType::USER_LOGIN, SecurityType::USER_EMAIL => sprintf(
41 3
                __('It looks like you are doing this too much.<br> Please %s and try again.', $userThrottleMessage)
42 3
            ),
43 3
            SecurityType::GLOBAL_LOGIN, SecurityType::GLOBAL_EMAIL => __(
44 3
                'The site is under a too high request load. 
45 3
            <br> Therefore, a general throttling is in place. Please fill out the captcha and try again.'
46 3
            ),
47 3
            default => 'Please wait or fill out the captcha and repeat the action.',
48 3
        };
49
    }
50
}
51