ApiKeyController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 2
1
<?php
2
/**
3
 * ownCloud - maps
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Vinzenz Rosenkranz <[email protected]>
9
 * @copyright Vinzenz Rosenkranz 2015
10
 */
11
12
namespace OCA\Maps\Controller;
13
14
use OCA\Maps\Db\ApiKey;
15
use OCA\Maps\Db\ApiKeyMapper;
16
use \OCP\IRequest;
17
use \OCP\AppFramework\Http\JSONResponse;
18
use \OCP\AppFramework\ApiController;
19
20
21
class ApiKeyController extends ApiController {
22
23
	private $userId;
24
	private $apiKeyMapper;
25
26
	public function __construct($appName, IRequest $request, ApiKeyMapper $apiKeyMapper, $userId) {
27
		parent::__construct($appName, $request);
28
		$this->apiKeyMapper = $apiKeyMapper;
29
		$this->userId = $userId;
30
	}
31
32
	/**
33
	 * @NoAdminRequired
34
	 *
35
	 * @param $key string
36
	 * @param $id int
37
	 * @return JSONResponse
38
	 */
39 View Code Duplication
	public function updateKey($key, $id) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
41
		$apikey = new ApiKey();
42
		$apikey->setId($id);
43
		$apikey->setApiKey($key);
44
45
		/* Only save apiKey if it exists in db */
46
		try {
47
			$this->apiKeyMapper->find($id);
48
			return new JSONResponse($this->apiKeyMapper->update($apikey));
49
		} catch(\OCP\AppFramework\Db\DoesNotExistException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\Db\DoesNotExistException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
50
			return new JSONResponse([
51
				'error' => $e->getMessage()
52
			]);
53
		}
54
	}
55
56
	/**
57
	 * @NoAdminRequired
58
	 *
59
	 * @param $key string
60
	 * @return JSONResponse
61
	 */
62
	public function addKey($key){
63
		$apikey = new ApiKey();
64
		$apikey->setApiKey($key);
65
		$apikey->setUserId($this->userId);
66
67
		/* @var $apikey ApiKey */
68
		$apikey = $this->apiKeyMapper->insert($apikey);
69
70
		$response = array('id'=> $apikey->getId());
71
		return new JSONResponse($response);
72
	}
73
74
	/**
75
	 * @NoAdminRequired
76
	 *
77
	 * @return JSONResponse
78
	 */
79
	public function getKey(){
80
		$apikey = new ApiKey();
81
		try {
82
			$apikey = $this->apiKeyMapper->findByUser($this->userId);
83
		} catch(\OCP\AppFramework\Db\DoesNotExistException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\Db\DoesNotExistException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
84
			$apikey->setUserId($this->userId);
85
		}
86
		return new JSONResponse($apikey);
87
	}
88
89
	/**
90
	 * @NoAdminRequired
91
	 *
92
	 * @param $id int
93
	 * @return JSONResponse
94
	 */
95
	public function removeApiKey($id){
96
		$apikey = $this->apiKeyMapper->find($id);
97
		if($apikey->userId == $this->userId) {
98
			$this->apiKeyMapper->delete($apikey);
99
		}
100
		return new JSONResponse();
101
	}
102
103
}
104