GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — 3.0.x ( 74e535...484b00 )
by Nicolas
03:47
created

AuthorManager::fetchByPasswordResetToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package toolkit
4
 */
5
/**
6
 * The `AuthorManager` class is responsible for managing all Author objects
7
 * in Symphony. Unlike other Manager objects, Authors are stored in the
8
 * database in `tbl_authors` and not on the file system. CRUD methods are
9
 * implemented to allow Authors to be created (add), read (fetch), updated
10
 * (edit) and deleted (delete).
11
 */
12
13
class AuthorManager
14
{
15
    /**
16
     * An array of all the objects that the Manager is responsible for.
17
     * Defaults to an empty array.
18
     * @var array
19
     */
20
    protected static $_pool = array();
21
22
    /**
23
     * Given an associative array of fields, insert them into the database
24
     * returning the resulting Author ID if successful, or false if there
25
     * was an error
26
     *
27
     * @param array $fields
28
     *  Associative array of field names => values for the Author object
29
     * @throws DatabaseException
30
     * @return integer
31
     *  Returns an Author ID of the created Author on success, 0 otherwise.
32
     */
33
    public static function add(array $fields)
34
    {
35
        $inserted = Symphony::Database()
0 ignored issues
show
Deprecated Code introduced by
The function Database::insert() has been deprecated: Symphony 3.0.0 If $table is an array, it is treated as the fields values Use DatabaseInsert::values() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

35
        $inserted = /** @scrutinizer ignore-deprecated */ Symphony::Database()

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
36
            ->insert('tbl_authors')
37
            ->values($fields)
38
            ->execute()
39
            ->success();
40
41
        return $inserted ? Symphony::Database()->getInsertID() : 0;
42
    }
43
44
    /**
45
     * Given an Author ID and associative array of fields, update an existing Author
46
     * row in the `tbl_authors` database table. Returns boolean for success/failure
47
     *
48
     * @param integer $id
49
     *  The ID of the Author that should be updated
50
     * @param array $fields
51
     *  Associative array of field names => values for the Author object
52
     *  This array does need to contain every value for the author object, it
53
     *  can just be the changed values.
54
     * @throws DatabaseException
55
     * @return boolean
56
     */
57
    public static function edit($id, array $fields)
58
    {
59
        return Symphony::Database()
0 ignored issues
show
Deprecated Code introduced by
The function Database::update() has been deprecated: Symphony 3.0.0 This parameter is deprecated and will be removed. Use DatabaseUpdate::where() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

59
        return /** @scrutinizer ignore-deprecated */ Symphony::Database()

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
60
            ->update('tbl_authors')
61
            ->set($fields)
62
            ->where(['id' => (int)$id])
63
            ->execute()
64
            ->success();
65
    }
66
67
    /**
68
     * Given an Author ID, delete an Author from Symphony.
69
     *
70
     * @param integer $id
71
     *  The ID of the Author that should be deleted
72
     * @throws DatabaseException
73
     * @return boolean
74
     */
75
    public static function delete($id)
76
    {
77
        return Symphony::Database()
0 ignored issues
show
Deprecated Code introduced by
The function Database::delete() has been deprecated: Symphony 3.0.0 This parameter is deprecated and will be removed. Use DatabaseDelete::where() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

77
        return /** @scrutinizer ignore-deprecated */ Symphony::Database()

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
78
            ->delete('tbl_authors')
79
            ->where(['id' => (int)$id])
80
            ->execute()
81
            ->success();
82
    }
83
84
    /**
85
     * Fetch a single author by its reset password token
86
     *
87
     * @param string $token
88
     * @return Author
89
     * @throws Exception
90
     *  If the token is not a string
91
     */
92
    public function fetchByPasswordResetToken($token)
93
    {
94
        if (!$token) {
95
            return null;
96
        }
97
        General::ensureType([
98
            'token' => ['var' => $token, 'type' => 'string'],
99
        ]);
100
        return $this->select()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->select()->...t(1)->execute()->next() also could return the type array which is incompatible with the documented return type Author.
Loading history...
101
            ->innerJoin('tbl_forgotpass')->alias('f')
102
            ->on(['a.id' => '$f.author_id'])
103
            ->where(['f.expiry' => ['>' => DateTimeObj::getGMT('c')]])
104
            ->where(['f.token' => $token])
105
            ->limit(1)
106
            ->execute()
107
            ->next();
108
    }
109
110
    /**
111
     * Fetch a single author by its auth token
112
     *
113
     * @param string $token
114
     * @return Author
115
     * @throws Exception
116
     *  If the token is not a string
117
     */
118
    public function fetchByAuthToken($token)
119
    {
120
        if (!$token) {
121
            return null;
122
        }
123
        General::ensureType([
124
            'token' => ['var' => $token, 'type' => 'string'],
125
        ]);
126
        return $this->select()
127
            ->where(['a.auth_token' => $token])
128
            ->limit(1)
129
            ->execute()
130
            ->next();
131
    }
132
133
    /**
134
     * The fetch method returns all Authors from Symphony with the option to sort
135
     * or limit the output. This method returns an array of Author objects.
136
     *
137
     * @deprecated Symphony 3.0.0
138
     *  Use select() instead
139
     * @param string $sortby
140
     *  The field to sort the authors by, defaults to 'id'
141
     * @param string $sortdirection
142
     *  Available values of ASC (Ascending) or DESC (Descending), which refer to the
143
     *  sort order for the query. Defaults to ASC (Ascending)
144
     * @param integer $limit
145
     *  The number of rows to return
146
     * @param integer $start
147
     *  The offset start point for limiting, maps to the LIMIT {x}, {y} MySQL functionality
148
     * @param string $where
149
     *  Any custom WHERE clauses. The `tbl_authors` alias is `a`
150
     * @param string $joins
151
     *  Any custom JOIN's
152
     * @throws DatabaseException
153
     * @return array
154
     *  An array of Author objects. If no Authors are found, an empty array is returned.
155
     */
156
    public static function fetch($sortby = 'id', $sortdirection = 'ASC', $limit = null, $start = null, $where = null, $joins = null)
157
    {
158
        if (Symphony::Log()) {
159
            Symphony::Log()->pushDeprecateWarningToLog('AuthorManager::fetch()', 'AuthorManager::select()');
160
        }
161
162
        $sortby = $sortby ?: 'id';
163
        $sortdirection = strtoupper($sortdirection) === 'ASC' ? 'ASC' : 'DESC';
164
165
        $query = (new AuthorManager)->select();
166
167
        $orderBy = [];
168
        foreach (explode(',', $sortby) as $sortby) {
169
            $sortby = trim($sortby);
170
            $orderBy["a.$sortby"] = $sortdirection;
171
        }
172
        $query->orderBy($orderBy);
173
174
        if ($joins) {
175
            $joins = $query->replaceTablePrefix($joins);
176
            $query->unsafeAppendSQLPart('join', $joins);
177
        }
178
        if ($where) {
179
            $where = $query->replaceTablePrefix($where);
180
            $query->unsafe()->unsafeAppendSQLPart('where', "WHERE $where");
181
        }
182
        if ($limit) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $limit of type null|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
183
            $query->limit($limit);
184
        }
185
        if ($start) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $start of type null|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
186
            $query->offset($start);
187
        }
188
189
        $authors = $query->execute()->rows();
190
191
        foreach ($authors as $author) {
192
            self::$_pool[$author->get('id')] = $author;
193
        }
194
195
        return $authors;
196
    }
197
198
    /**
199
     * Returns Author's that match the provided ID's with the option to
200
     * sort or limit the output. This function will search the
201
     * `AuthorManager::$_pool` for Authors first before querying `tbl_authors`
202
     *
203
     * @param integer|array $id
204
     *  A single ID or an array of ID's
205
     * @throws DatabaseException
206
     * @return mixed
207
     *  If `$id` is an integer, the result will be an Author object,
208
     *  otherwise an array of Author objects will be returned. If no
209
     *  Authors are found, or no `$id` is given, `null` is returned.
210
     */
211
    public static function fetchByID($id)
212
    {
213
        $return_single = false;
214
215
        if (is_null($id)) {
0 ignored issues
show
introduced by
The condition is_null($id) is always false.
Loading history...
216
            return null;
217
        }
218
219
        if (!is_array($id)) {
220
            $return_single = true;
221
            $id = array((int)$id);
222
        }
223
224
        if (empty($id)) {
225
            return null;
226
        }
227
228
        // Get all the Author ID's that are already in `self::$_pool`
229
        $authors = array();
230
        $pooled_authors = array_intersect($id, array_keys(self::$_pool));
231
232
        foreach ($pooled_authors as $pool_author) {
233
            $authors[] = self::$_pool[$pool_author];
234
        }
235
236
        // Get all the Author ID's that are not already stored in `self::$_pool`
237
        $id = array_diff($id, array_keys(self::$_pool));
238
        $id = array_filter($id);
239
240
        if (empty($id)) {
241
            return ($return_single ? $authors[0] : $authors);
242
        }
243
244
        $authors = (new AuthorManager)
245
            ->select()
246
            ->authors($id)
0 ignored issues
show
Bug introduced by
$id of type array is incompatible with the type integer expected by parameter $author_ids of AuthorQuery::authors(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

246
            ->authors(/** @scrutinizer ignore-type */ $id)
Loading history...
247
            ->execute()
248
            ->rows();
249
250
        foreach ($authors as $author) {
251
            self::$_pool[$author->get('id')] = $author;
252
        }
253
254
        return ($return_single ? $authors[0] : $authors);
255
    }
256
257
    /**
258
     * Returns an Author by Username. This function will search the
259
     * `AuthorManager::$_pool` for Authors first before querying `tbl_authors`
260
     *
261
     * @param string $username
262
     *  The Author's username
263
     * @return Author|null
264
     *  If an Author is found, an Author object is returned, otherwise null.
265
     */
266
    public static function fetchByUsername($username)
267
    {
268
        if (!isset(self::$_pool[$username])) {
269
            $author = (new AuthorManager)
270
                ->select()
271
                ->username($username)
272
                ->limit(1)
273
                ->execute()
274
                ->next();
275
276
            if (!$author) {
0 ignored issues
show
introduced by
$author is of type Author, thus it always evaluated to true.
Loading history...
277
                return null;
278
            }
279
280
            self::$_pool[$username] = $author;
281
        }
282
283
        return self::$_pool[$username];
284
    }
285
286
    /**
287
     * Creates a new Author object.
288
     *
289
     * @return Author
290
     */
291
    public static function create()
292
    {
293
        return new Author;
294
    }
295
296
    /**
297
     * Factory method that creates a new AuthorQuery.
298
     *
299
     * @since Symphony 3.0.0
300
     * @param array $projection
301
     *  The projection to select.
302
     *  If no projection gets added, it defaults to `AuthorQuery::getDefaultProjection()`.
303
     * @return AuthorQuery
304
     */
305
    public function select(array $projection = [])
306
    {
307
        return new AuthorQuery(Symphony::Database(), $projection);
308
    }
309
}
310