Completed
Push — master ( 83ab72...026655 )
by Maxence
02:38
created

TemplatesController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 113
Duplicated Lines 8.85 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 10
loc 113
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 1
A getOptionsPanel() 0 12 3
B getNavigationPanels() 0 26 5
A getTemplate() 0 8 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
2
/**
3
 * FullTextSearch - Full text search framework for Nextcloud
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2018
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\FullTextSearch\Controller;
28
29
use Exception;
30
use OC\AppFramework\Http;
31
use OCA\FullTextSearch\AppInfo\Application;
32
use OCA\FullTextSearch\Exceptions\ProviderDoesNotExistException;
33
use OCA\FullTextSearch\IFullTextSearchProvider;
34
use OCA\FullTextSearch\Service\ConfigService;
35
use OCA\FullTextSearch\Service\MiscService;
36
use OCA\FullTextSearch\Service\ProviderService;
37
use OCP\AppFramework\Controller;
38
use OCP\AppFramework\Http\DataResponse;
39
use OCP\AppFramework\Http\TemplateResponse;
40
use OCP\IConfig;
41
use OCP\IRequest;
42
use OCP\Util;
43
44
class TemplatesController extends Controller {
45
46
	/** @var IConfig */
47
	private $config;
48
49
	/** @var ConfigService */
50
	private $configService;
51
52
	/** @var ProviderService */
53
	private $providerService;
54
55
	/** @var MiscService */
56
	private $miscService;
57
58
59
	/**
60
	 * TemplatesController constructor.
61
	 *
62
	 * @param IRequest $request
63
	 * @param IConfig $config
64
	 * @param ConfigService $configService
65
	 * @param ProviderService $providerService
66
	 * @param MiscService $miscService
67
	 */
68 View Code Duplication
	public function __construct(
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...
69
		IRequest $request, IConfig $config, ConfigService $configService,
70
		ProviderService $providerService, MiscService $miscService
71
	) {
72
		parent::__construct(Application::APP_NAME, $request);
73
		$this->config = $config;
74
		$this->configService = $configService;
75
		$this->providerService = $providerService;
76
		$this->miscService = $miscService;
77
	}
78
79
80
	/**
81
	 * @NoAdminRequired
82
	 * @NoSubAdminRequired
83
	 *
84
	 * @param $providerId
85
	 *
86
	 * @return DataResponse
87
	 * @throws Exception
88
	 * @throws ProviderDoesNotExistException
89
	 */
90
	public function getOptionsPanel($providerId) {
91
		$provider = $this->providerService->getProvider($providerId);
92
93
		$ret = [];
94
		$options = $provider->getOptionsTemplate();
95
		if (is_array($options) && array_key_exists('panel', $options)) {
96
			$tmpl = $this->getTemplate($provider, $options['panel']);
97
			$ret[$providerId] = $tmpl->render();
98
		}
99
100
		return new DataResponse($ret, Http::STATUS_OK);
101
	}
102
103
104
	/**
105
	 * @NoAdminRequired
106
	 * @NoSubAdminRequired
107
	 *
108
	 * @return DataResponse
109
	 * @throws Exception
110
	 */
111
	public function getNavigationPanels() {
112
		$providers = $this->providerService->getProviders();
113
114
		$ret = [];
115
		foreach ($providers as $provider) {
116
			$providerId = $provider->getId();
117
118
			$options = $provider->getOptionsTemplate();
119
			$nav = [];
120
			if (is_array($options) && array_key_exists('navigation', $options)) {
121
				$nav = $options['navigation'];
122
			}
123
124
			if (array_key_exists('css', $nav)) {
125
				Util::addStyle($provider->getAppId(), $nav['css']);
126
			}
127
128
			$ret[$providerId] =
129
				[
130
					'title'      => $provider->getName(),
131
					'navigation' => $nav
132
				];
133
		}
134
135
		return new DataResponse($ret, Http::STATUS_OK);
136
	}
137
138
139
	/**
140
	 * @param IFullTextSearchProvider $provider
141
	 * @param array $panel
142
	 *
143
	 * @return TemplateResponse
144
	 * @throws Exception
145
	 */
146
	private function getTemplate(IFullTextSearchProvider $provider, $panel) {
147
148
		if (!is_array($panel) || !array_key_exists('template', $panel)) {
149
			throw new Exception('malformed option panel');
150
		}
151
152
		return new TemplateResponse($provider->getAppId(), $panel['template'], [], 'blank');
153
	}
154
155
156
}