GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SessionTimeProvider::getFormTime()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[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 Core23\AntiSpamBundle\Provider;
13
14
use DateTime;
15
use Symfony\Component\HttpFoundation\Session\Session;
16
17
final class SessionTimeProvider implements TimeProviderInterface
18
{
19
    /**
20
     * @var Session
21
     */
22
    private $session;
23
24
    public function __construct(Session $session)
25
    {
26
        $this->session = $session;
27
    }
28
29
    public function createFormProtection(string $name): void
30
    {
31
        $startTime = new DateTime();
32
        $key       = $this->getSessionKey($name);
33
        $this->session->set($key, $startTime);
34
    }
35
36
    public function isValid(string $name, array $options): bool
37
    {
38
        $startTime = $this->getFormTime($name);
39
40
        if (null === $startTime) {
41
            return false;
42
        }
43
44
        $currentTime = new DateTime();
45
46
        if (\array_key_exists('min', $options) && null !== $options['min']) {
47
            $minTime = clone $startTime;
48
            $minTime->modify(sprintf('+%d seconds', $options['min']));
49
50
            if ($minTime > $currentTime) {
51
                return false;
52
            }
53
        }
54
55
        if (\array_key_exists('max', $options) && null !== $options['max']) {
56
            $maxTime = clone $startTime;
57
            $maxTime->modify(sprintf('+%d seconds', $options['max']));
58
59
            if ($maxTime < $currentTime) {
60
                return false;
61
            }
62
        }
63
64
        return true;
65
    }
66
67
    public function removeFormProtection(string $name): void
68
    {
69
        $key = $this->getSessionKey($name);
70
        $this->session->remove($key);
71
    }
72
73
    /**
74
     * Check if a form has a time protection.
75
     */
76
    private function hasFormProtection(string $name): bool
77
    {
78
        $key = $this->getSessionKey($name);
79
80
        return $this->session->has($key);
81
    }
82
83
    /**
84
     * Gets the form time for specified form.
85
     *
86
     * @param string $name Name of form to get
87
     */
88
    private function getFormTime(string $name): ?DateTime
89
    {
90
        $key = $this->getSessionKey($name);
91
92
        if ($this->hasFormProtection($name)) {
93
            return $this->session->get($key);
94
        }
95
96
        return null;
97
    }
98
99
    private function getSessionKey(string $name): string
100
    {
101
        return 'antispam_'.$name;
102
    }
103
}
104