Issues (186)

Branch: oauth-creation-featureflag

includes/DataObjects/GeoLocation.php (1 issue)

Labels
Severity
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 * ACC Development Team. Please see team.json for a list of contributors.     *
5
 *                                                                            *
6
 * This is free and unencumbered software released into the public domain.    *
7
 * Please see LICENSE.md for the full licencing statement.                    *
8
 ******************************************************************************/
9
10
namespace Waca\DataObjects;
11
12
use DateTimeImmutable;
13
use Exception;
14
use Waca\DataObject;
15
use Waca\Exceptions\OptimisticLockFailedException;
16
use Waca\PdoDatabase;
17
18
/**
19
 * Geolocation data object
20
 *
21
 * This is a cache.
22
 */
23
class GeoLocation extends DataObject
24
{
25
    private $address;
26
    private $data;
27
    private $creation;
28
29
    /**
30
     * @param string      $address
31
     * @param PdoDatabase $database
32
     * @param bool        $forUpdate
33
     * @return GeoLocation
34
     */
35
    public static function getByAddress($address, PdoDatabase $database, $forUpdate = false)
36
    {
37
        $lockMode = $forUpdate ? ' FOR UPDATE' : '';
38
        $sql = "SELECT * FROM geolocation WHERE address = :id LIMIT 1" . $lockMode;
39
40
        $statement = $database->prepare($sql);
41
        $statement->bindValue(":id", $address);
42
43
        $statement->execute();
44
45
        $resultObject = $statement->fetchObject(get_called_class());
46
47
        if ($resultObject != false) {
48
            $resultObject->setDatabase($database);
49
        }
50
51
        return $resultObject;
52
    }
53
54
    public function save()
55
    {
56
        if ($this->isNew()) {
57
            // insert
58
            $statement = $this->dbObject->prepare("INSERT INTO `geolocation` (address, data) VALUES (:address, :data) ON DUPLICATE KEY UPDATE address = address;");
59
60
            $statement->bindValue(":address", $this->address);
61
            $statement->bindValue(":data", $this->data);
62
63
            if ($statement->execute()) {
64
                $this->id = (int)$this->dbObject->lastInsertId();
65
            }
66
            else {
67
                throw new Exception($statement->errorInfo());
0 ignored issues
show
$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

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