Completed
Push — master ( 908ea6...448b39 )
by Maxence
11:53
created

TemplatesController::getTemplate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 2
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
	public function __construct(
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
		$panel = [];
94
		$options = $provider->getOptionsTemplate();
95
		if (is_array($options) && array_key_exists('panel', $options)) {
96
			$panel = $options['panel'];
97
		}
98
99
		if (array_key_exists('template', $panel)) {
100
			$tmpl = new TemplateResponse($provider->getAppId(), $panel['template'], [], 'blank');
101
			$panel['template'] = $tmpl->render();
102
		}
103
104
		$ret[$providerId] = $panel;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ret was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ret = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
105
106
		return new DataResponse($ret, Http::STATUS_OK);
107
	}
108
109
110
	/**
111
	 * @NoAdminRequired
112
	 * @NoSubAdminRequired
113
	 *
114
	 * @return DataResponse
115
	 * @throws Exception
116
	 */
117
	public function getNavigationPanels() {
118
		$providers = $this->providerService->getProviders();
119
120
		$ret = [];
121
		foreach ($providers as $provider) {
122
			$providerAppId = $provider->getAppId();
123
124
			$options = $provider->getOptionsTemplate();
125
			$nav = [];
126
			if (is_array($options) && array_key_exists('navigation', $options)) {
127
				$nav = $options['navigation'];
128
			}
129
130
			if (array_key_exists('css', $nav)) {
131
				Util::addStyle($provider->getAppId(), $nav['css']);
132
			}
133
134
			$ret[$providerAppId] =
135
				[
136
					'provider'   => $provider->getId(),
137
					'title'      => $provider->getName(),
138
					'navigation' => $nav
139
				];
140
		}
141
142
		return new DataResponse($ret, Http::STATUS_OK);
143
	}
144
145
146
}