Completed
Push — master ( 9a9a39...cf3017 )
by Maximilian
03:39
created

AuthenticationAttempt::unserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sententiaregum project.
5
 *
6
 * (c) Maximilian Bosch <[email protected]>
7
 * (c) Ben Bieler <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace AppBundle\Model\User;
14
15
use Doctrine\ORM\Mapping as ORM;
16
use Ramsey\Uuid\Uuid;
17
18
/**
19
 * Model that contains data of an authentication attempt.
20
 *
21
 * @author Maximilian Bosch <[email protected]>
22
 *
23
 * @ORM\Entity
24
 * @ORM\Table(name="authentication_attempt", indexes={
25
 *     @ORM\Index(name="auth_attempt_count", columns={"attempt_count"})
26
 * })
27
 */
28
class AuthenticationAttempt implements \Serializable
29
{
30
    /**
31
     * @var string
32
     *
33
     * @ORM\Id
34
     * @ORM\GeneratedValue(strategy="NONE")
35
     * @ORM\Column(name="id", type="guid")
36
     */
37
    private $id;
38
39
    /**
40
     * @var string
41
     *
42
     * @ORM\Column(name="ip", type="string", length=255, unique=true)
43
     */
44
    private $ip;
45
46
    /**
47
     * @var int
48
     *
49
     * @ORM\Column(name="attempt_count", type="integer")
50
     */
51
    private $attemptCount = 0;
52
53
    /**
54
     * @var \DateTime[]
55
     *
56
     * @ORM\Column(name="last_date_time_range", type="date_time_array")
57
     */
58
    private $lastDateTimeRange = [];
59
60
    /**
61
     * @var \DateTime
62
     *
63
     * @ORM\Column(name="latest_date_time", type="datetime")
64
     */
65
    private $latestDateTime;
66
67
    /**
68
     * Constructor.
69
     */
70
    public function __construct()
71
    {
72
        $this->id = Uuid::uuid4()->toString();
73
    }
74
75
    /**
76
     * Get id.
77
     *
78
     * @return string
79
     */
80
    public function getId()
81
    {
82
        return $this->id;
83
    }
84
85
    /**
86
     * Get ip.
87
     *
88
     * @return string
89
     */
90
    public function getIp()
91
    {
92
        return $this->ip;
93
    }
94
95
    /**
96
     * Set ip.
97
     *
98
     * @param string $ip
99
     *
100
     * @return $this
101
     */
102
    public function setIp($ip)
103
    {
104
        $this->ip = (string) $ip;
105
106
        return $this;
107
    }
108
109
    /**
110
     * Get attempt count.
111
     *
112
     * @return int
113
     */
114
    public function getAttemptCount()
115
    {
116
        return $this->attemptCount;
117
    }
118
119
    /**
120
     * Increase attempt count.
121
     *
122
     * @return $this
123
     */
124
    public function increaseAttemptCount()
125
    {
126
        ++$this->attemptCount;
127
128
        $now                  = new \DateTime();
129
        $this->latestDateTime = $now;
130
131
        if (count($this->lastDateTimeRange) === (User::MAX_FAILED_ATTEMPTS_FROM_IP + 1)) {
132
            array_pop($this->lastDateTimeRange);
133
        }
134
135
        array_unshift($this->lastDateTimeRange, $now);
136
137
        return $this;
138
    }
139
140
    /**
141
     * Getter for the last datetime of a failed auth attempt.
142
     *
143
     * @return \DateTime
144
     */
145
    public function getLatestFailedAttemptTime()
146
    {
147
        return $this->latestDateTime;
148
    }
149
150
    /**
151
     * Getter for the last datetime items.
152
     *
153
     * @return \DateTime[]
154
     */
155
    public function getLastFailedAttemptTimesInRange()
156
    {
157
        return $this->lastDateTimeRange;
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function serialize()
164
    {
165
        return serialize([
166
            $this->id,
167
            $this->ip,
168
            $this->attemptCount,
169
            $this->lastDateTimeRange,
170
            $this->latestDateTime,
171
        ]);
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function unserialize($serialized)
178
    {
179
        list($this->id, $this->ip, $this->attemptCount, $this->lastDateTimeRange, $this->latestDateTime) = unserialize($serialized);
180
    }
181
}
182