Completed
Branch newinternal (113bb8)
by Michael
05:12
created

RDnsCache::save()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 43
ccs 0
cts 35
cp 0
rs 9.1928
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;");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal SELECT * FROM rdnscache ... address = :id LIMIT 1; does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
36
        $statement->bindValue(":id", $address);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :id does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
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
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getByAddress()
Loading history...
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);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :address does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
58
            $statement->bindValue(":data", $this->data);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :data does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
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
LIMIT 1;
74
SQL
75
            );
76
77
            $statement->bindValue(':id', $this->id);
78
            $statement->bindValue(':updateversion', $this->updateversion);
79
80
            $statement->bindValue(':address', $this->address);
81
            $statement->bindValue(':data', $this->data);
82
83
            if (!$statement->execute()) {
84
                throw new Exception($statement->errorInfo());
85
            }
86
87
            if ($statement->rowCount() !== 1) {
88
                throw new OptimisticLockFailedException();
89
            }
90
91
            $this->updateversion++;
92
        }
93
    }
0 ignored issues
show
Coding Style introduced by
Expected //end save()
Loading history...
94
95
    public function getAddress()
96
    {
97
        return $this->address;
98
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getAddress()
Loading history...
99
100
    /**
101
     * @param string $address
102
     */
103
    public function setAddress($address)
104
    {
105
        $this->address = $address;
106
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setAddress()
Loading history...
107
108
    /**
109
     * @return string
110
     */
111
    public function getData()
112
    {
113
        return unserialize($this->data);
114
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getData()
Loading history...
115
116
    public function setData($data)
117
    {
118
        $this->data = serialize($data);
119
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setData()
Loading history...
120
121
    /**
122
     * @return DateTimeImmutable
123
     */
124
    public function getCreation()
125
    {
126
        return new DateTimeImmutable($this->creation);
127
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getCreation()
Loading history...
128
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
129