PageController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Thomas Müller <[email protected]>
7
 * @author Timo Witte <[email protected]>
8
 *
9
 * Mail
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\Mail\Controller;
26
27
use OCA\Mail\Service\AccountService;
28
use OCA\Mail\Service\AliasesService;
29
use OCP\AppFramework\Controller;
30
use OCP\AppFramework\Http\ContentSecurityPolicy;
31
use OCP\AppFramework\Http\RedirectResponse;
32
use OCP\AppFramework\Http\TemplateResponse;
33
use OCP\IConfig;
34
use OCP\IRequest;
35
use OCP\IURLGenerator;
36
use OCP\IUserSession;
37
38
class PageController extends Controller {
39
40
	/** @var IURLGenerator */
41
	private $urlGenerator;
42
43
	/** @var IConfig */
44
	private $config;
45
46
	/** @var AccountService */
47
	private $accountService;
48
49
	/** @var AliasesService */
50
	private $aliasesService;
51
52
	/** @var string */
53
	private $currentUserId;
54
55
	/** @var IUserSession */
56
	private $userSession;
57
58
	/**
59
	 * @param string $appName
60
	 * @param IRequest $request
61
	 * @param IURLGenerator $urlGenerator
62
	 * @param IConfig $config
63
	 * @param AccountService $accountService
64
	 * @param AliasesService $aliasesService
65 6
	 * @param string $UserId
66 6
	 */
67 6
	public function __construct($appName, IRequest $request,
68 6
		IURLGenerator $urlGenerator, IConfig $config, AccountService $accountService,
69 6
		AliasesService $aliasesService, $UserId, IUserSession $userSession) {
70 6
		parent::__construct($appName, $request);
71 6
		$this->urlGenerator = $urlGenerator;
72
		$this->config = $config;
73
		$this->accountService = $accountService;
74
		$this->aliasesService = $aliasesService;
75
		$this->currentUserId = $UserId;
76
		$this->userSession = $userSession;
77
	}
78
79 1
	/**
80 1
	 * @NoAdminRequired
81 1
	 * @NoCSRFRequired
82 1
	 *
83 1
	 * @return TemplateResponse renders the index page
84
	 */
85
	public function index() {
86 1
		$mailAccounts = $this->accountService->findByUserId($this->currentUserId);
87 1
88 1
		$accountsJson = [];
89 1 View Code Duplication
		foreach ($mailAccounts as $mailAccount) {
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...
90 1
			$conf = $mailAccount->getConfiguration();
91
			$conf['aliases'] = $this->aliasesService->findAll($conf['accountId'], $this->currentUserId);
92 1
			$accountsJson[] = $conf;
93
		}
94
95
		$user = $this->userSession->getUser();
96
		$response = new TemplateResponse($this->appName, 'index', [
97
			'debug' => $this->config->getSystemValue('debug', false),
98
			'app-version' => $this->config->getAppValue('mail', 'installed_version'),
99
			'accounts' => base64_encode(json_encode($accountsJson)),
100
			'prefill_displayName' => $user->getDisplayName(),
101
			'prefill_email' => $this->config->getUserValue($user->getUID(), 'settings', 'email', ''),
102 5
		]);
103 5
104 5
		// set csp rules for ownCloud 8.1
105 5
		if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) {
106 4
			$csp = new ContentSecurityPolicy();
107 4
			$csp->addAllowedFrameDomain('\'self\'');
108 4
			$response->setContentSecurityPolicy($csp);
109 4
		}
110 4
111 4
		return $response;
112
	}
113 5
114 5
	/**
115 5
	 * @NoAdminRequired
116
	 * @NoCSRFRequired
117 5
	 *
118
	 * @param string $uri
119 5
	 * @return TemplateResponse renders the compose page
120 5
	 */
121
	public function compose($uri) {
122
		$parts = parse_url($uri);
123
		$params = ['to' => $parts['path']];
124
		if (isset($parts['query'])) {
125
			$parts = explode('&', $parts['query']);
126
			foreach ($parts as $part) {
127
				$pair = explode('=', $part, 2);
128
				$params[strtolower($pair[0])] = urldecode($pair[1]);
129
			}
130
		}
131
132
		array_walk($params, function (&$value, $key) {
133
			$value = "$key=" . urlencode($value);
134
		});
135
136
		$hashParams = '#mailto?' . implode('&', $params);
137
138
		$baseUrl = $this->urlGenerator->linkToRoute("mail.page.index");
139
		return new RedirectResponse($baseUrl . $hashParams);
140
	}
141
142
}
143