Failed Conditions
Push — newinternal ( 216d62...410e59 )
by Simon
05:28 queued 13s
created

RDnsCache::save()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 25
c 0
b 0
f 0
dl 0
loc 42
ccs 0
cts 34
cp 0
rs 9.2088
cc 5
nc 5
nop 0
crap 30
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\Exceptions\OptimisticLockFailedException;
15
use Waca\PdoDatabase;
16
17
/**
18
 * rDNS Cache data object
19
 */
20
class RDnsCache extends DataObject
21
{
22
    private $address;
23
    private $data;
24
    private $creation;
25
26
    /**
27
     * @param string      $address
28
     * @param PdoDatabase $database
29
     *
30
     * @return RDnsCache|false
31
     */
32
    public static function getByAddress($address, PdoDatabase $database)
33
    {
34
        // @todo add cache invalidation (timestamp?)
35
        $statement = $database->prepare("SELECT * FROM rdnscache WHERE address = :id LIMIT 1;");
36
        $statement->bindValue(":id", $address);
37
38
        $statement->execute();
39
40
        $resultObject = $statement->fetchObject(get_called_class());
41
42
        if ($resultObject != false) {
43
            $resultObject->setDatabase($database);
44
        }
45
46
        return $resultObject;
47
    }
48
49
    public function save()
50
    {
51
        if ($this->isNew()) {
52
            // insert
53
            $statement = $this->dbObject->prepare(<<<SQL
54
INSERT INTO `rdnscache` (address, data) VALUES (:address, :data);
55
SQL
56
            );
57
            $statement->bindValue(":address", $this->address);
58
            $statement->bindValue(":data", $this->data);
59
60
            if ($statement->execute()) {
61
                $this->id = (int)$this->dbObject->lastInsertId();
62
            }
63
            else {
64
                throw new Exception($statement->errorInfo());
0 ignored issues
show
Bug introduced by
$statement->errorInfo() of type array is incompatible with the type string expected by parameter $message of Exception::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
65
            }
66
        }
67
        else {
68
            // update
69
            $statement = $this->dbObject->prepare(<<<SQL
70
UPDATE `rdnscache`
71
SET address = :address, data = :data, updateversion = updateversion + 1
72
WHERE id = :id AND updateversion = :updateversion;
73
SQL
74
            );
75
76
            $statement->bindValue(':id', $this->id);
77
            $statement->bindValue(':updateversion', $this->updateversion);
78
79
            $statement->bindValue(':address', $this->address);
80
            $statement->bindValue(':data', $this->data);
81
82
            if (!$statement->execute()) {
83
                throw new Exception($statement->errorInfo());
84
            }
85
86
            if ($statement->rowCount() !== 1) {
87
                throw new OptimisticLockFailedException();
88
            }
89
90
            $this->updateversion++;
91
        }
92
    }
93
94
    public function getAddress()
95
    {
96
        return $this->address;
97
    }
98
99
    /**
100
     * @param string $address
101
     */
102
    public function setAddress($address)
103
    {
104
        $this->address = $address;
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getData()
111
    {
112
        return unserialize($this->data);
113
    }
114
115
    public function setData($data)
116
    {
117
        $this->data = serialize($data);
118
    }
119
120
    /**
121
     * @return DateTimeImmutable
122
     */
123
    public function getCreation()
124
    {
125
        return new DateTimeImmutable($this->creation);
126
    }
127
}
128