TimeoutSeconds::validate()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 5
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 11
rs 9.6111
1
<?php
2
/**
3
 * Copyright (c) 2019. Volodymyr Hryvinskyi.  All rights reserved.
4
 * @author: <mailto:[email protected]>
5
 * @github: <https://github.com/hryvinskyi>
6
 */
7
8
declare(strict_types=1);
9
10
namespace Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Validators;
11
12
use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\Response;
13
use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\VerifyReCaptcha;
14
15
/**
16
 * Class TimeoutSeconds
17
 */
18
class TimeoutSeconds implements ValidatorInterface
19
{
20
    /**
21
     * Challenge timeout
22
     *
23
     * @const string
24
     */
25
    const E_CHALLENGE_TIMEOUT = 'challenge-timeout';
26
27
    /**
28
     * @param VerifyReCaptcha $verify
29
     * @param Response $response
30
     *
31
     * @return string|null
32
     */
33
    public function validate(VerifyReCaptcha $verify, Response $response): ?string
34
    {
35
        if ($verify->getChallengeTimeout() && $response->getChallengeTs()) {
36
            $challengeTs = strtotime($response->getChallengeTs());
37
38
            if ($challengeTs > 0 && time() - $challengeTs > $verify->getChallengeTimeout()) {
39
                return self::E_CHALLENGE_TIMEOUT;
40
            }
41
        }
42
43
        return null;
44
    }
45
}
46