Completed
Push — master ( 4a1c4b...dec28d )
by Angus
03:08
created

GetKey   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 65.31 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 32
loc 49
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A index() 16 16 3
A restore() 16 16 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php defined('BASEPATH') or exit('No direct script access allowed');
2
3
class GetKey extends AJAX_Controller {
4
	public function __construct() {
5
		parent::__construct();
6
7
		$this->load->library('Limiter');
8
		$this->load->library('form_validation');
9
	}
10
11
	/**
12
	 * Used to generate the API Key the userscript can use.
13
	 *
14
	 * REQ_PARAMS: N/A
15
	 * METHOD:     POST
16
	 * URL:        /ajax/get_apikey
17
	 */
18 View Code Duplication
	public function index() : void {
19
		if($this->ion_auth->logged_in()) {
20
			if(!$this->limiter->limit('new_api_key', 10)) {
21
				$api_key = $this->User->get_new_api_key();
22
				$json    = ['api-key' => $api_key];
23
24
				$this->output
25
					->set_content_type('application/json')
26
					->set_output(json_encode($json));
27
			} else {
28
				$this->output->set_status_header('429', 'Rate limit reached.');
29
			}
30
		} else {
31
			$this->output->set_status_header('400', 'Not logged in.');
32
		}
33
	}
34
35 View Code Duplication
	public function restore() : void {
36
		if($this->ion_auth->logged_in()) {
37
			if(!$this->limiter->limit('new_api_key', 10)) {
38
				$api_key = $this->User->restore_api_key();
39
				$json    = ['api-key' => $api_key];
40
41
				$this->output
42
					->set_content_type('application/json')
43
					->set_output(json_encode($json));
44
			} else {
45
				$this->output->set_status_header('429', 'Rate limit reached.');
46
			}
47
		} else {
48
			$this->output->set_status_header('400', 'Not logged in.');
49
		}
50
	}
51
}
52