Failed Conditions
Push — newinternal ( b66232...216d62 )
by Simon
16:33 queued 06:35
created

AntiSpoofCache::getTimestamp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
crap 2
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\DataObjects;
10
11
use DateTimeImmutable;
12
use Exception;
13
use Waca\DataObject;
14
use Waca\PdoDatabase;
15
16
/**
17
 * AntiSpoofCache data object
18
 */
19
class AntiSpoofCache extends DataObject
20
{
21
    /** @var string */
22
    protected $username;
23
    /** @var string */
24
    protected $data;
25
    /** @var string */
26
    protected $timestamp;
27
28
    /**
29
     * @param   string    $username
30
     * @param PdoDatabase $database
31
     *
32
     * @return AntiSpoofCache|false
33
     */
34 View Code Duplication
    public static function getByUsername($username, PdoDatabase $database)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        $statement = $database->prepare(<<<SQL
37
SELECT *
38
FROM antispoofcache
39
WHERE username = :id AND timestamp > date_sub(now(), INTERVAL 3 HOUR)
40
LIMIT 1
41
SQL
42
        );
43
        $statement->bindValue(":id", $username);
44
45
        $statement->execute();
46
47
        $resultObject = $statement->fetchObject(get_called_class());
48
49
        if ($resultObject != false) {
50
            $resultObject->setDatabase($database);
51
        }
52
53
        return $resultObject;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getUsername()
60
    {
61
        return $this->username;
62
    }
63
64
    /**
65
     * @param string $username
66
     */
67
    public function setUsername($username)
68
    {
69
        $this->username = $username;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getData()
76
    {
77
        return $this->data;
78
    }
79
80
    /**
81
     * @param string $data
82
     */
83
    public function setData($data)
84
    {
85
        $this->data = $data;
86
    }
87
88
    /**
89
     * @return DateTimeImmutable
90
     */
91
    public function getTimestamp()
92
    {
93
        return new DateTimeImmutable($this->timestamp);
94
    }
95
96
    /**
97
     * @throws Exception
98
     */
99 View Code Duplication
    public function save()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        if ($this->isNew()) {
102
            // insert
103
            // clear old data first
104
            $this->dbObject->exec("DELETE FROM antispoofcache WHERE timestamp < date_sub(now(), INTERVAL 3 HOUR);");
105
106
            $statement = $this->dbObject->prepare("INSERT INTO antispoofcache (username, data) VALUES (:username, :data);");
107
            $statement->bindValue(":username", $this->username);
108
            $statement->bindValue(":data", $this->data);
109
110
            if ($statement->execute()) {
111
                $this->id = (int)$this->dbObject->lastInsertId();
112
            }
113
            else {
114
                throw new Exception($statement->errorInfo());
115
            }
116
        }
117
    }
118
}
119