Completed
Push — master ( aadd4c...2d89ff )
by Julius
10s
created

SettingsController::getOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[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\AppOrder\Controller;
25
26
use \OCP\AppFramework\Controller;
27
use \OCP\AppFramework\Http\TemplateResponse;
28
use \OCP\IRequest;
29
use \OCP\INavigationManager;
30
use \OCA\AppOrder\Service\ConfigService;
31
use \OCA\AppOrder\Util;
32
33
class SettingsController extends Controller {
34
35
	private $userId;
36
	private $appConfig;
37
	private $navigationManager;
38
	private $util;
39
40 7
	public function __construct($appName, IRequest $request, ConfigService $appConfig, INavigationManager $urlGenerator, Util $util, $userId) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 140 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
41 7
		parent::__construct($appName, $request);
42 7
		$this->userId = $userId;
43 7
		$this->appConfig = $appConfig;
44 7
		$this->navigationManager = $urlGenerator;
45 7
		$this->util = $util;
46 7
	}
47
48
	/**
49
	 * Admin: render admin page
50
	 * FIXME: Move to dedicated class
51
	 *
52
	 * @return TemplateResponse
53
	 */
54 1
	public function adminIndex() {
55
		// Private API call
56 1
		$navigation = $this->navigationManager->getAll();
57 1
		$order = json_decode($this->appConfig->getAppValue('order'));
58 1
		if($order === null) $order = array();
59 1
		$nav = $this->util->matchOrder($navigation, $order);
60 1
		$hidden = json_decode($this->appConfig->getAppValue('hidden'));
61 1
		if($hidden === null) $hidden = array();
62 1
		return new TemplateResponse(
63 1
			$this->appName,
64 1
			'admin',
65 1
			["nav" => $nav, 'type' => 'admin', 'hidden' => $hidden],
66
			'blank'
67 1
		);
68
	}
69
70 1
	public function personalIndex() {
71
		// Private API call
72 1
		$navigation = $this->navigationManager->getAll();
73 1
		$order = json_decode($this->appConfig->getUserValue('order', $this->userId));
74 1 View Code Duplication
		if($order === null){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
75
			$order = json_decode($this->appConfig->getAppValue('order'));
76
			if($order === null) $order = array();
77
		}
78 1
		$nav = $this->util->matchOrder($navigation, $order);
79 1
		$hidden = json_decode($this->appConfig->getUserValue('hidden',$this->userId));
80 1 View Code Duplication
		if($hidden === null){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
81
			$hidden = json_decode($this->appConfig->getAppValue('hidden'));
82
			if($hidden === null) $hidden = array();
83
		}
84 1
		return new TemplateResponse(
85 1
			$this->appName,
86 1
			'admin',
87 1
			["nav" => $nav, 'type' => 'personal', 'hidden' => $hidden],
88
			'blank'
89 1
		);
90
	}
91
92
	/**
93
	 * Admin: save default order
94
	 *
95
	 * @param $order
96
	 * @return array response
97
	 */
98 2
	public function saveDefaultOrder($order) {
99 2
		if (!is_null($order)) {
100 2
			$this->appConfig->setAppValue('order', $order);
101 2
		}
102 2
		return array('status' => 'success', 'order' => $order);
103
	}
104
105
	/**
106
	 * Return order for current user
107
	 *
108
	 * @NoAdminRequired
109
	 * @return array response
110
	 */
111 1
	public function getOrder() {
112 1
		$order = $this->util->getAppOrder();
113 1
		$hidden = $this->util->getAppHidden();
114 1
		return array('status' => 'success', 'order' => $order, 'hidden' => $hidden);
115
	}
116
117
	/**
118
	 * Save order for current user
119
	 *
120
	 * @NoAdminRequired
121
	 * @param $order string
122
	 * @return array response
123
	 */
124 2 View Code Duplication
	public function savePersonal($order) {
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...
125 2
		$this->appConfig->setUserValue('order', $this->userId, $order);
126
		$response = array(
127 2
			'status' => 'success',
128 2
			'data' => array('message' => 'User order saved successfully.'),
129
			'order' => $order
130 2
		);
131 2
		return $response;
132
	}
133
134
	/**
135
	 * Save hidden for current user
136
	 *
137
	 * @NoAdminRequired
138
	 * @param $hidden string
139
	 * @return array response
140
	 */
141 View Code Duplication
	public function savePersonalHidden($hidden) {
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...
142
		$this->appConfig->setUserValue('hidden', $this->userId, $hidden);
143
		$response = array(
144
			'status' => 'success',
145
			'data' => array('message' => 'User hidden saved successfully.'),
146
			'hidden' => $hidden
147
		);
148
		return $response;
149
	}
150
151
	/**
152
	 * Admin: save default hidden
153
	 *
154
	 * @param $hidden
155
	 * @return array response
156
	 */
157
	public function saveDefaultHidden($hidden) {
158
		if (!is_null($hidden)) {
159
			$this->appConfig->setAppValue('hidden', $hidden);
160
		}
161
		return array('status' => 'success', 'hidden' => $hidden);
162
	}
163
164
}
165