Passed
Pull Request — master (#966)
by René
04:19
created

SubscriptionApiController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 16
ccs 0
cts 15
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author René Gieling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Polls\Controller;
25
26
use Exception;
27
use OCP\AppFramework\Db\DoesNotExistException;
28
29
use OCP\IRequest;
30
use OCP\ILogger;
31
32
use OCP\AppFramework\ApiController;
33
use OCP\AppFramework\Http;
34
use OCP\AppFramework\Http\DataResponse;
35
36
use OCA\Polls\Service\SubscriptionService;
37
38
class SubscriptionApiController extends ApiController {
39
40
	private $userId;
41
	private $subscriptionService;
42
	private $logger;
43
44
	/**
45
	 * SubscriptionController constructor.
46
	 * @param string $appName
47
	 * @param $UserId
48
	 * @param SubscriptionService $subscriptionService
49
	 * @param IRequest $request
50
	 * @param ILogger $logger
51
	 */
52
53
	public function __construct(
54
		string $appName,
55
		$userId,
56
		SubscriptionService $subscriptionService,
57
		IRequest $request,
58
		ILogger $logger
59
60
	) {
61
		parent::__construct($appName,
62
			$request,
63
			'PUT, GET, DELETE',
64
            'Authorization, Content-Type, Accept',
65
            1728000);
66
		$this->userId = $userId;
67
		$this->subscriptionService = $subscriptionService;
68
		$this->logger = $logger;
69
	}
70
71
	/**
72
	 * @NoAdminRequired
73
	 * CORS
74
	 * @NoCSRFRequired
75
	 * @param integer $pollId
76
	 * @return DataResponse
77
	 */
78
	public function get($pollId) {
79
		try {
80
			return new DataResponse($this->subscriptionService->get($pollId), Http::STATUS_OK);
81
		} catch (NotAuthorizedException $e) {
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Controller\NotAuthorizedException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
82
			return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
0 ignored issues
show
Bug introduced by
'Unauthorized' of type string is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct(). ( Ignorable by Annotation )

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

82
			return new DataResponse(/** @scrutinizer ignore-type */ 'Unauthorized', Http::STATUS_FORBIDDEN);
Loading history...
83
		} catch (DoesNotExistException $e) {
84
			return new DataResponse('Not subscribed', Http::STATUS_NOT_FOUND);
85
		}
86
	}
87
88
	/**
89
	 * @NoAdminRequired
90
	 * @CORS
91
	 * @NoCSRFRequired
92
	 * @param integer $pollId
93
	 */
94
	public function subscribe($pollId) {
95
		try {
96
			return $this->subscriptionService->set($pollId, true);
97
			return new DataResponse('Subscribed', Http::STATUS_OK);
0 ignored issues
show
Unused Code introduced by
return new OCP\AppFramew...mework\Http::STATUS_OK) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
98
		} catch (NotAuthorizedException $e) {
99
			return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
0 ignored issues
show
Bug introduced by
'Unauthorized' of type string is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct(). ( Ignorable by Annotation )

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

99
			return new DataResponse(/** @scrutinizer ignore-type */ 'Unauthorized', Http::STATUS_FORBIDDEN);
Loading history...
100
		}
101
	}
102
	/**
103
	 * @NoAdminRequired
104
	 * @CORS
105
	 * @NoCSRFRequired
106
	 * @param integer $pollId
107
	 */
108
	public function unsubscribe($pollId) {
109
		try {
110
			$this->subscriptionService->set($pollId, false);
111
			return new DataResponse('Unsubscribed', Http::STATUS_OK);
0 ignored issues
show
Bug introduced by
'Unsubscribed' of type string is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct(). ( Ignorable by Annotation )

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

111
			return new DataResponse(/** @scrutinizer ignore-type */ 'Unsubscribed', Http::STATUS_OK);
Loading history...
112
		} catch (NotAuthorizedException $e) {
113
			return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
114
		}
115
	}
116
}
117