Issues (186)

includes/DataObjects/UserPreference.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 Exception;
13
use Waca\DataObject;
14
use Waca\Exceptions\OptimisticLockFailedException;
15
use Waca\PdoDatabase;
16
17
class UserPreference extends DataObject
18
{
19
20
    /** @var int */
21
    private $user;
22
23
    /** @var ?int */
24
    private $domain;
25
26
    /** @var string */
27
    private $preference;
28
29
    /** @var ?mixed */
30
    private $value;
31
32
    public static function getLocalPreference(PdoDatabase $database, int $user, string $preference, int $domain) {
33
        $statement = $database->prepare('SELECT * FROM userpreference WHERE preference = :preference AND USER = :user AND domain = :domain');
34
        $statement->execute([
35
            ':user' => $user,
36
            ':preference' => $preference,
37
            ':domain' => $domain
38
        ]);
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 static function getGlobalPreference(PdoDatabase $database, int $user, string $preference) {
50
        $statement = $database->prepare('SELECT * FROM userpreference WHERE preference = :preference AND USER = :user AND domain IS NULL');
51
        $statement->execute([
52
            ':user' => $user,
53
            ':preference' => $preference
54
        ]);
55
56
        $resultObject = $statement->fetchObject(get_called_class());
57
58
        if ($resultObject !== false) {
59
            $resultObject->setDatabase($database);
60
        }
61
62
        return $resultObject;
63
    }
64
65
    /**
66
     * @inheritDoc
67
     * @throws Exception
68
     */
69
    public function save()
70
    {
71
        if($this->isNew()) {
72
            // insert
73
            $statement = $this->dbObject->prepare(<<<SQL
74
                INSERT INTO `userpreference` (
75
                    user, domain, preference, value
76
                ) VALUES (
77
                    :user, :domain, :preference, :value
78
                );
79
SQL
80
            );
81
            $statement->bindValue(":user", $this->user);
82
            $statement->bindValue(":domain", $this->domain);
83
            $statement->bindValue(":preference", $this->preference);
84
            $statement->bindValue(":value", $this->value);
85
86
            if ($statement->execute()) {
87
                $this->id = (int)$this->dbObject->lastInsertId();
88
            }
89
            else {
90
                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

90
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
91
            }
92
        }else{
93
            // update
94
            $statement = $this->dbObject->prepare(<<<SQL
95
                UPDATE `userpreference` SET 
96
                    value = :value,
97
                    updateversion = updateversion + 1
98
                WHERE id = :id AND updateversion = :updateversion;
99
SQL
100
            );
101
            $statement->bindValue(":value", $this->value);
102
103
            $statement->bindValue(':id', $this->id);
104
            $statement->bindValue(':updateversion', $this->updateversion);
105
106
            if (!$statement->execute()) {
107
                throw new Exception($statement->errorInfo());
108
            }
109
110
            if ($statement->rowCount() !== 1) {
111
                throw new OptimisticLockFailedException();
112
            }
113
114
            $this->updateversion++;
115
        }
116
    }
117
118
    /**
119
     * @return int
120
     */
121
    public function getUser(): int
122
    {
123
        return $this->user;
124
    }
125
126
    /**
127
     * @param int $user
128
     */
129
    public function setUser(int $user): void
130
    {
131
        $this->user = $user;
132
    }
133
134
    /**
135
     * @return ?int
136
     */
137
    public function getDomain(): ?int
138
    {
139
        return $this->domain;
140
    }
141
142
    /**
143
     * @param ?int $domain
144
     */
145
    public function setDomain(?int $domain): void
146
    {
147
        $this->domain = $domain;
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getPreference(): string
154
    {
155
        return $this->preference;
156
    }
157
158
    /**
159
     * @param string $preference
160
     */
161
    public function setPreference(string $preference): void
162
    {
163
        $this->preference = $preference;
164
    }
165
166
    /**
167
     * @return mixed|null
168
     */
169
    public function getValue()
170
    {
171
        return $this->value;
172
    }
173
174
    /**
175
     * @param mixed|null $value
176
     */
177
    public function setValue($value): void
178
    {
179
        $this->value = $value;
180
    }
181
}
182