Completed
Push — master ( 74a8a3...46dc92 )
by Victor
13s
created

SettingsController::save()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 29
ccs 19
cts 19
cp 1
rs 8.5806
c 1
b 0
f 0
cc 4
eloc 21
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
	/** @var AppConfig */
23
	private $settings;
24
25
	/** @var ScannerFactory */
26
	private $scannerFactory;
27
	
28
	/** @var IL10N */
29
	private $l10n;
30
	
31 3
	public function __construct(IRequest $request, AppConfig $appConfig, ScannerFactory $scannerFactory, IL10N $l10n) {
32 3
		$this->settings = $appConfig;
33 3
		$this->scannerFactory = $scannerFactory;
34 3
		$this->l10n = $l10n;
35 3
	}
36
	
37
	/**
38
	 * Print config section
39
	 * @return TemplateResponse
40
	 */
41
	public function index() {
42
		$data = $this->settings->getAllValues();
43
		return new TemplateResponse('files_antivirus', 'settings', $data, 'blank');
44
	}
45
46
	/**
47
	 * Save Parameters
48
	 *
49
	 * @param string $avMode - antivirus mode
50
	 * @param string $avSocket - path to socket (Socket mode)
51
	 * @param string $avHost - antivirus url
52
	 * @param int $avPort - port
53
	 * @param string $avCmdOptions - extra command line options
54
	 * @param string $avPath - path to antivirus executable (Executable mode)
55
	 * @param string $avInfectedAction - action performed on infected files
56
	 * @param $avStreamMaxLength - reopen socket after bytes
57
	 * @param int $avMaxFileSize - file size limit
58
	 * @return JSONResponse
59
	 */
60 3
	public function save($avMode, $avSocket, $avHost, $avPort, $avCmdOptions, $avPath, $avInfectedAction, $avStreamMaxLength, $avMaxFileSize) {
61 3
		$this->settings->setAvMode($avMode);
62 3
		if ($avMode === 'executable') {
63 1
			$this->settings->setAvCmdOptions($avCmdOptions);
64 1
			$this->settings->setAvPath($avPath);
65 2
		} else if ($avMode === 'daemon') {
66 1
			$this->settings->setAvPort($avPort);
67 1
			$this->settings->setAvHost($avHost);
68 1
		} else if ($avMode === 'socket') {
69 1
			$this->settings->setAvSocket($avSocket);
70
		}
71
72 3
		$this->settings->setAvInfectedAction($avInfectedAction);
73 3
		$this->settings->setAvStreamMaxLength($avStreamMaxLength);
74 3
		$this->settings->setAvMaxFileSize($avMaxFileSize);
75
76 3
		$connectionStatus = intval($this->scannerFactory->testConnection($this->settings));
77
78 3
		return new JSONResponse(
79
			['data' =>
80
				['message' =>
81 3
					(string) $this->l10n->t('Saved')
82
				],
83 3
				'connection' => $connectionStatus,
84 3
				'status' => 'success',
85 3
				'settings' => $this->settings->getAllValues()
86
			]
87
		);
88
	}
89
}
90