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

WelcomeTemplate::save()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 43
rs 9.1928
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
 * Welcome template data object
19
 */
20
class WelcomeTemplate extends DataObject
21
{
22
    /** @var string */
23
    private $usercode;
24
    /** @var string */
25
    private $botcode;
26
    private $usageCache;
27
    private $deleted = 0;
28
29
    /**
30
     * Summary of getAll
31
     *
32
     * @param PdoDatabase $database
33
     *
34
     * @return WelcomeTemplate[]
35
     */
36
    public static function getAll(PdoDatabase $database)
37
    {
38
        $statement = $database->prepare("SELECT * FROM welcometemplate WHERE deleted = 0;");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal SELECT * FROM welcometemplate WHERE deleted = 0; 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...
39
40
        $statement->execute();
41
42
        $result = array();
43
        /** @var WelcomeTemplate $v */
44
        foreach ($statement->fetchAll(PDO::FETCH_CLASS, self::class) as $v) {
45
            $v->setDatabase($database);
46
            $result[] = $v;
47
        }
48
49
        return $result;
50
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getAll()
Loading history...
51
52
    /**
53
     * @throws Exception
54
     */
0 ignored issues
show
Coding Style introduced by
Expected 2 @throws tag(s) in function comment; 1 found
Loading history...
55
    public function save()
56
    {
57
        if ($this->isNew()) {
58
            // insert
59
            $statement = $this->dbObject->prepare(<<<SQL
60
INSERT INTO welcometemplate (usercode, botcode) VALUES (:usercode, :botcode);
61
SQL
62
            );
63
            $statement->bindValue(":usercode", $this->usercode);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :usercode 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...
64
            $statement->bindValue(":botcode", $this->botcode);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :botcode 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...
65
66
            if ($statement->execute()) {
67
                $this->id = (int)$this->dbObject->lastInsertId();
68
            }
69
            else {
70
                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

70
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
71
            }
72
        }
73
        else {
74
            // update
75
            $statement = $this->dbObject->prepare(<<<SQL
76
UPDATE `welcometemplate`
77
SET usercode = :usercode, botcode = :botcode, updateversion = updateversion + 1
78
WHERE id = :id AND updateversion = :updateversion
79
LIMIT 1;
80
SQL
81
            );
82
83
            $statement->bindValue(':id', $this->id);
84
            $statement->bindValue(':updateversion', $this->updateversion);
85
86
            $statement->bindValue(':usercode', $this->usercode);
87
            $statement->bindValue(':botcode', $this->botcode);
88
89
            if (!$statement->execute()) {
90
                throw new Exception($statement->errorInfo());
91
            }
92
93
            if ($statement->rowCount() !== 1) {
94
                throw new OptimisticLockFailedException();
95
            }
96
97
            $this->updateversion++;
98
        }
99
    }
0 ignored issues
show
Coding Style introduced by
Expected //end save()
Loading history...
100
101
    /**
102
     * @return string
103
     */
104
    public function getUserCode()
105
    {
106
        return $this->usercode;
107
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getUserCode()
Loading history...
108
109
    /**
110
     * @param string $usercode
111
     */
112
    public function setUserCode($usercode)
113
    {
114
        $this->usercode = $usercode;
115
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setUserCode()
Loading history...
116
117
    /**
118
     * @return string
119
     */
120
    public function getBotCode()
121
    {
122
        return $this->botcode;
123
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getBotCode()
Loading history...
124
125
    /**
126
     * @param string $botcode
127
     */
128
    public function setBotCode($botcode)
129
    {
130
        $this->botcode = $botcode;
131
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setBotCode()
Loading history...
132
133
    /**
134
     * @return User[]
135
     */
136
    public function getUsersUsingTemplate()
137
    {
138
        if ($this->usageCache === null) {
139
            $statement = $this->dbObject->prepare("SELECT * FROM user WHERE welcome_template = :id;");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal SELECT * FROM user WHERE welcome_template = :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...
140
141
            $statement->execute(array(":id" => $this->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...
142
143
            $result = array();
144
            /** @var WelcomeTemplate $v */
145
            foreach ($statement->fetchAll(PDO::FETCH_CLASS, User::class) as $v) {
146
                $v->setDatabase($this->dbObject);
147
                $result[] = $v;
148
            }
149
150
            $this->usageCache = $result;
151
        }
152
153
        return $this->usageCache;
154
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getUsersUsingTemplate()
Loading history...
155
156
    /**
157
     * Deletes the object from the database
158
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
159
    public function delete()
160
    {
161
        if ($this->id === null) {
162
            // wtf?
163
            return;
164
        }
165
166
        $deleteQuery = "UPDATE welcometemplate SET deleted = 1 WHERE id = :id AND updateversion = :updateversion;";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal UPDATE welcometemplate S...rsion = :updateversion; 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...
167
        $statement = $this->dbObject->prepare($deleteQuery);
168
169
        $statement->bindValue(":id", $this->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...
170
        $statement->bindValue(":updateversion", $this->updateversion);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :updateversion 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...
171
        $statement->execute();
172
173
        if ($statement->rowCount() !== 1) {
174
            throw new OptimisticLockFailedException();
175
        }
176
    }
0 ignored issues
show
Coding Style introduced by
Expected //end delete()
Loading history...
177
178
    /**
179
     * @return bool
180
     */
181
    public function isDeleted()
182
    {
183
        return ((int)$this->deleted) === 1;
184
    }
0 ignored issues
show
Coding Style introduced by
Expected //end isDeleted()
Loading history...
185
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
186