Completed
Push — master ( 14d226...3a0e06 )
by Joas
10:50
created

SettingsController::createCodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OCA\TwoFactorBackupCodes\Controller;
24
25
use OCA\TwoFactorBackupCodes\Service\BackupCodeStorage;
26
use OCP\AppFramework\Controller;
27
use OCP\AppFramework\Http\JSONResponse;
28
use OCP\IRequest;
29
use OCP\IUserSession;
30
31
class SettingsController extends Controller {
32
33
	/** @var BackupCodeStorage */
34
	private $storage;
35
36
	/** @var IUserSession */
37
	private $userSession;
38
39
	/**
40
	 * @param string $appName
41
	 * @param IRequest $request
42
	 * @param BackupCodeStorage $storage
43
	 * @param IUserSession $userSession
44
	 */
45
	public function __construct($appName, IRequest $request, BackupCodeStorage $storage, IUserSession $userSession) {
46
		parent::__construct($appName, $request);
47
		$this->userSession = $userSession;
48
		$this->storage = $storage;
49
	}
50
51
	/**
52
	 * @NoAdminRequired
53
	 * @return JSONResponse
54
	 */
55
	public function state() {
56
		$user = $this->userSession->getUser();
57
		return $this->storage->getBackupCodesState($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->userSession->getUser() on line 56 can be null; however, OCA\TwoFactorBackupCodes...::getBackupCodesState() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
58
	}
59
60
	/**
61
	 * @NoAdminRequired
62
	 * @return JSONResponse
63
	 */
64
	public function createCodes() {
65
		$user = $this->userSession->getUser();
66
		$codes = $this->storage->createCodes($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->userSession->getUser() on line 65 can be null; however, OCA\TwoFactorBackupCodes...eStorage::createCodes() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
67
		return [
68
		    'codes' => $codes,
69
		    'state' => $this->storage->getBackupCodesState($user),
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->userSession->getUser() on line 65 can be null; however, OCA\TwoFactorBackupCodes...::getBackupCodesState() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
70
		];
71
	}
72
73
}
74