Completed
Pull Request — master (#1613)
by Christoph
27:51
created

PageController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 100
Duplicated Lines 6 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 7
c 4
b 0
f 0
lcom 2
cbo 3
dl 6
loc 100
ccs 45
cts 45
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B index() 6 26 3
A compose() 0 21 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
/**
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
37
class PageController extends Controller {
38
39
	/** @var IURLGenerator */
40
	private $urlGenerator;
41
42
	/** @var IConfig */
43
	private $config;
44
45
	/** @var AccountService */
46
	private $accountService;
47
48
	/** @var AliasesService */
49
	private $aliasesService;
50
51
	/** @var string */
52
	private $currentUserId;
53
54
	/**
55
	 * @param string $appName
56
	 * @param IRequest $request
57
	 * @param IURLGenerator $urlGenerator
58
	 * @param IConfig $config
59
	 * @param AccountService $accountService
60
	 * @param AliasesService $aliasesService
61
	 * @param string $UserId
62
	 */
63 6
	public function __construct($appName, IRequest $request,
64
		IURLGenerator $urlGenerator, IConfig $config, AccountService $accountService,
65
		AliasesService $aliasesService, $UserId) {
66 6
		parent::__construct($appName, $request);
67 6
		$this->urlGenerator = $urlGenerator;
68 6
		$this->config = $config;
69 6
		$this->accountService = $accountService;
70 6
		$this->aliasesService = $aliasesService;
71 6
		$this->currentUserId = $UserId;
72 6
	}
73
74
	/**
75
	 * @NoAdminRequired
76
	 * @NoCSRFRequired
77
	 *
78
	 * @return TemplateResponse renders the index page
79
	 */
80 1
	public function index() {
81 1
		$mailAccounts = $this->accountService->findByUserId($this->currentUserId);
82
83 1
		$accountsJson = [];
84 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...
85 1
			$conf = $mailAccount->getConfiguration();
86 1
			$conf['aliases'] = $this->aliasesService->findAll($conf['accountId'],
87 1
				$this->currentUserId);
88 1
			$accountsJson[] = $conf;
89 1
		}
90
91 1
		$response = new TemplateResponse($this->appName, 'index', [
92 1
			'debug' => $this->config->getSystemValue('debug', false),
93 1
			'app-version' => $this->config->getAppValue('mail', 'installed_version'),
94 1
			'accounts' => base64_encode(json_encode($accountsJson)),
95 1
		]);
96
97
		// set csp rules for ownCloud 8.1
98 1
		if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) {
99 1
			$csp = new ContentSecurityPolicy();
100 1
			$csp->addAllowedFrameDomain('\'self\'');
101 1
			$response->setContentSecurityPolicy($csp);
102 1
		}
103
104 1
		return $response;
105
	}
106
107
	/**
108
	 * @NoAdminRequired
109
	 * @NoCSRFRequired
110
	 *
111
	 * @param string $uri
112
	 * @return TemplateResponse renders the compose page
113
	 */
114 5
	public function compose($uri) {
115 5
		$parts = parse_url($uri);
116 5
		$params = ['to' => $parts['path']];
117 5
		if (isset($parts['query'])) {
118 4
			$parts = explode('&', $parts['query']);
119 4
			foreach ($parts as $part) {
120 4
				$pair = explode('=', $part, 2);
121 4
				$params[strtolower($pair[0])] = urldecode($pair[1]);
122 4
			}
123 4
		}
124
125 5
		array_walk($params,
126 5
			function (&$value, $key) {
127 5
			$value = "$key=" . urlencode($value);
128 5
		});
129
130 5
		$hashParams = '#mailto?' . implode('&', $params);
131
132 5
		$baseUrl = $this->urlGenerator->linkToRoute("mail.page.index");
133 5
		return new RedirectResponse($baseUrl . $hashParams);
134
	}
135
136
}
137