Completed
Push — rbac ( f06361...8f6a08 )
by Michael
05:58 queued 16s
created

Ban::save()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 48
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 48
rs 9.0968
cc 5
nc 5
nop 0
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 Exception;
12
use PDO;
13
use Waca\DataObject;
14
use Waca\Exceptions\OptimisticLockFailedException;
15
use Waca\PdoDatabase;
16
17
/**
18
 * Ban data object
19
 */
20
class Ban extends DataObject
21
{
22
    private $type;
23
    private $target;
24
    private $user;
25
    private $reason;
26
    private $date;
27
    private $duration;
28
    private $active;
29
30
    /**
31
     * Gets all active bans, filtered by the optional target.
32
     *
33
     * @param string|null $target
34
     * @param PdoDatabase $database
35
     *
36
     * @return Ban[]
37
     */
38
    public static function getActiveBans($target, PdoDatabase $database)
39
    {
40
        if ($target !== null) {
41
            $query = <<<SQL
42
SELECT * FROM ban WHERE target = :target AND (duration > UNIX_TIMESTAMP() OR duration = -1) AND active = 1;
43
SQL;
44
            $statement = $database->prepare($query);
45
            $statement->bindValue(":target", $target);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :target 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...
46
        }
47
        else {
48
            $query = "SELECT * FROM ban WHERE (duration > UNIX_TIMESTAMP() OR duration = -1) AND active = 1;";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal SELECT * FROM ban WHERE ...n = -1) AND active = 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...
49
            $statement = $database->prepare($query);
50
        }
51
52
        $statement->execute();
53
54
        $result = array();
55
56
        /** @var Ban $v */
57
        foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) {
58
            $v->setDatabase($database);
59
            $result[] = $v;
60
        }
61
62
        return $result;
63
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getActiveBans()
Loading history...
64
65
    /**
66
     * Gets a ban by it's ID if it's currently active.
67
     *
68
     * @param     integer $id
69
     * @param PdoDatabase $database
70
     *
71
     * @return Ban
72
     */
73
    public static function getActiveId($id, PdoDatabase $database)
74
    {
75
        $statement = $database->prepare(<<<SQL
76
SELECT *
77
FROM ban
78
WHERE id = :id  AND (duration > UNIX_TIMESTAMP() OR duration = -1) AND active = 1;
79
SQL
80
        );
81
        $statement->bindValue(":id", $id);
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...
82
83
        $statement->execute();
84
85
        $resultObject = $statement->fetchObject(get_called_class());
86
87
        if ($resultObject != false) {
88
            $resultObject->setDatabase($database);
89
        }
90
91
        return $resultObject;
92
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getActiveId()
Loading history...
93
94
    /**
95
     * Get all active bans for a target and type.
96
     *
97
     * @param string      $target
98
     * @param string      $type
99
     * @param PdoDatabase $database
100
     *
101
     * @return Ban
102
     */
103
    public static function getBanByTarget($target, $type, PdoDatabase $database)
104
    {
105
        $query = <<<SQL
106
SELECT * FROM ban
107
WHERE type = :type
108
	AND target = :target
109
	AND (duration > UNIX_TIMESTAMP() OR duration = -1)
110
	AND active = 1;
111
SQL;
112
        $statement = $database->prepare($query);
113
        $statement->bindValue(":target", $target);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :target 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...
114
        $statement->bindValue(":type", $type);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :type 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...
115
116
        $statement->execute();
117
118
        $resultObject = $statement->fetchObject(get_called_class());
119
120
        if ($resultObject != false) {
121
            $resultObject->setDatabase($database);
122
        }
123
124
        return $resultObject;
125
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getBanByTarget()
Loading history...
126
127
    /**
128
     * @throws Exception
129
     */
0 ignored issues
show
Coding Style introduced by
Expected 2 @throws tag(s) in function comment; 1 found
Loading history...
130
    public function save()
131
    {
132
        if ($this->isNew()) {
133
            // insert
134
            $statement = $this->dbObject->prepare(<<<SQL
135
INSERT INTO `ban` (type, target, user, reason, date, duration, active)
136
VALUES (:type, :target, :user, :reason, CURRENT_TIMESTAMP(), :duration, :active);
137
SQL
138
            );
139
            $statement->bindValue(":type", $this->type);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :type 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...
140
            $statement->bindValue(":target", $this->target);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :target 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...
141
            $statement->bindValue(":user", $this->user);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :user 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...
142
            $statement->bindValue(":reason", $this->reason);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :reason 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...
143
            $statement->bindValue(":duration", $this->duration);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :duration 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...
144
            $statement->bindValue(":active", $this->active);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :active 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...
145
146
            if ($statement->execute()) {
147
                $this->id = (int)$this->dbObject->lastInsertId();
148
            }
149
            else {
150
                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

150
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
151
            }
152
        }
153
        else {
154
            // update
155
            $statement = $this->dbObject->prepare(<<<SQL
156
UPDATE `ban`
157
SET duration = :duration, active = :active, user = :user, updateversion = updateversion + 1
158
WHERE id = :id AND updateversion = :updateversion
159
LIMIT 1;
160
SQL
161
            );
162
            $statement->bindValue(':id', $this->id);
163
            $statement->bindValue(':updateversion', $this->updateversion);
164
165
            $statement->bindValue(':duration', $this->duration);
166
            $statement->bindValue(':active', $this->active);
167
            $statement->bindValue(':user', $this->user);
168
169
            if (!$statement->execute()) {
170
                throw new Exception($statement->errorInfo());
171
            }
172
173
            if ($statement->rowCount() !== 1) {
174
                throw new OptimisticLockFailedException();
175
            }
176
177
            $this->updateversion++;
178
        }
179
    }
0 ignored issues
show
Coding Style introduced by
Expected //end save()
Loading history...
180
181
    /**
182
     * @return string
183
     */
184
    public function getType()
185
    {
186
        return $this->type;
187
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getType()
Loading history...
188
189
    /**
190
     * @param string $type
191
     */
192
    public function setType($type)
193
    {
194
        $this->type = $type;
195
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setType()
Loading history...
196
197
    /**
198
     * @return string
199
     */
200
    public function getTarget()
201
    {
202
        return $this->target;
203
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getTarget()
Loading history...
204
205
    /**
206
     * @param string $target
207
     */
208
    public function setTarget($target)
209
    {
210
        $this->target = $target;
211
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setTarget()
Loading history...
212
213
    /**
214
     * @return string
215
     */
216
    public function getReason()
217
    {
218
        return $this->reason;
219
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getReason()
Loading history...
220
221
    /**
222
     * @param string $reason
223
     */
224
    public function setReason($reason)
225
    {
226
        $this->reason = $reason;
227
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setReason()
Loading history...
228
229
    /**
230
     * @return mixed
231
     */
232
    public function getDate()
233
    {
234
        return $this->date;
235
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getDate()
Loading history...
236
237
    /**
238
     * @return mixed
239
     */
240
    public function getDuration()
241
    {
242
        return $this->duration;
243
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getDuration()
Loading history...
244
245
    /**
246
     * @param mixed $duration
247
     */
248
    public function setDuration($duration)
249
    {
250
        $this->duration = $duration;
251
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setDuration()
Loading history...
252
253
    /**
254
     * @return bool
255
     */
256
    public function isActive()
257
    {
258
        return $this->active == 1;
259
    }
0 ignored issues
show
Coding Style introduced by
Expected //end isActive()
Loading history...
260
261
    /**
262
     * @param bool $active
263
     */
264
    public function setActive($active)
265
    {
266
        $this->active = $active ? 1 : 0;
267
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setActive()
Loading history...
268
269
    /**
270
     * @return int
271
     */
272
    public function getUser()
273
    {
274
        return $this->user;
275
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getUser()
Loading history...
276
277
    /**
278
     * @param int $user UserID of user who is setting the ban
279
     *
280
     * @throws Exception
281
     */
282
    public function setUser($user)
283
    {
284
        $this->user = $user;
285
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setUser()
Loading history...
286
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
287