Completed
Pull Request — master (#225)
by Victor
05:30 queued 04:01
created

SettingsController::save()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 33
ccs 20
cts 20
cp 1
rs 8.5806
c 1
b 0
f 0
cc 4
eloc 23
nc 4
nop 9
crap 4

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
 * Copyright (c) 2015 Viktar Dubiniuk <[email protected]>
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
namespace OCA\Files_Antivirus\Controller;
10
11
use OCA\Files_Antivirus\ScannerFactory;
12
use \OCP\AppFramework\Controller;
13
use \OCP\IRequest;
14
use \OCP\IL10N;
15
use \OCA\Files_Antivirus\AppConfig;
16
17
use \OCP\AppFramework\Http\TemplateResponse;
18
use \OCP\AppFramework\Http\JSONResponse;
19
20
class SettingsController extends Controller {
21
22
	/**
23
	 * @var AppConfig
24
	 */
25
	private $settings;
26
27
	/**
28
	 * @var ScannerFactory
29
	 */
30
	private $scannerFactory;
31
	
32
	/**
33
	 * @var IL10N
34
	 */
35
	private $l10n;
36
37
	/**
38
	 * SettingsController constructor.
39
	 *
40
	 * @param IRequest $request
41
	 * @param AppConfig $appConfig
42
	 * @param ScannerFactory $scannerFactory
43
	 * @param IL10N $l10n
44
	 */
45 3
	public function __construct(IRequest $request,
46
		AppConfig $appConfig, ScannerFactory $scannerFactory, IL10N $l10n
47
	) {
48 3
		$this->settings = $appConfig;
49 3
		$this->scannerFactory = $scannerFactory;
50 3
		$this->l10n = $l10n;
51 3
	}
52
	
53
	/**
54
	 * Print config section
55
	 *
56
	 * @return TemplateResponse
57
	 */
58
	public function index() {
59
		$data = $this->settings->getAllValues();
60
		return new TemplateResponse('files_antivirus', 'settings', $data, 'blank');
61
	}
62
63
	/**
64
	 * Save Parameters
65
	 *
66
	 * @param string $avMode - antivirus mode
67
	 * @param string $avSocket - path to socket (Socket mode)
68
	 * @param string $avHost - antivirus url
69
	 * @param int $avPort - port
70
	 * @param string $avCmdOptions - extra command line options
71
	 * @param string $avPath - path to antivirus executable (Executable mode)
72
	 * @param string $avInfectedAction - action performed on infected files
73
	 * @param int $avStreamMaxLength - reopen socket after bytes
74
	 * @param int $avMaxFileSize - file size limit
75
	 *
76
	 * @return JSONResponse
77
	 */
78 3
	public function save($avMode, $avSocket, $avHost, $avPort,
79
		$avCmdOptions, $avPath, $avInfectedAction, $avStreamMaxLength, $avMaxFileSize
80
	) {
81 3
		$this->settings->setAvMode($avMode);
82 3
		if ($avMode === 'executable') {
83 1
			$this->settings->setAvCmdOptions($avCmdOptions);
84 1
			$this->settings->setAvPath($avPath);
85 2
		} else if ($avMode === 'daemon') {
86 1
			$this->settings->setAvPort($avPort);
87 1
			$this->settings->setAvHost($avHost);
88 1
		} else if ($avMode === 'socket') {
89 1
			$this->settings->setAvSocket($avSocket);
90
		}
91
92 3
		$this->settings->setAvInfectedAction($avInfectedAction);
93 3
		$this->settings->setAvStreamMaxLength($avStreamMaxLength);
94 3
		$this->settings->setAvMaxFileSize($avMaxFileSize);
95
96 3
		$connectionStatus = intval(
97 3
			$this->scannerFactory->testConnection($this->settings)
98
		);
99
100 3
		return new JSONResponse(
101
			['data' =>
102
				['message' =>
103 3
					(string) $this->l10n->t('Saved')
104
				],
105 3
				'connection' => $connectionStatus,
106 3
				'status' => 'success',
107 3
				'settings' => $this->settings->getAllValues()
108
			]
109
		);
110
	}
111
}
112