SettingsController::save()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 26
cp 0
rs 9.456
c 0
b 0
f 0
cc 4
nc 6
nop 9
crap 20

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 Victor 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\Scanner\ScannerFactory;
12
use OCA\Files_Antivirus\Status;
13
use \OCP\AppFramework\Controller;
14
use \OCP\IRequest;
15
use \OCP\IL10N;
16
use \OCA\Files_Antivirus\AppConfig;
17
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 IL10N
29
	 */
30
	private $l10n;
31
32
	private $scannerFactory;
33
34
	public function __construct($appName, IRequest $request, AppConfig $appconfig, IL10N $l10n, ScannerFactory $scannerFactory) {
35
		parent::__construct($appName, $request);
36
37
		$this->settings = $appconfig;
38
		$this->l10n = $l10n;
39
		$this->scannerFactory = $scannerFactory;
40
	}
41
42
	/**
43
	 * Save Parameters
44
	 *
45
	 * @param string $avMode - antivirus mode
46
	 * @param string $avSocket - path to socket (Socket mode)
47
	 * @param string $avHost - antivirus url
48
	 * @param int $avPort - port
49
	 * @param string $avCmdOptions - extra command line options
50
	 * @param string $avPath - path to antivirus executable (Executable mode)
51
	 * @param string $avInfectedAction - action performed on infected files
52
	 * @param $avStreamMaxLength - reopen socket after bytes
53
	 * @param int $avMaxFileSize - file size limit
54
	 * @return JSONResponse
55
	 */
56
	public function save($avMode, $avSocket, $avHost, $avPort, $avCmdOptions, $avPath, $avInfectedAction, $avStreamMaxLength, $avMaxFileSize) {
57
		$this->settings->setAvMode($avMode);
58
		$this->settings->setAvSocket($avSocket);
59
		$this->settings->setAvHost($avHost);
60
		$this->settings->setAvPort($avPort);
61
		$this->settings->setAvCmdOptions($avCmdOptions);
62
		$this->settings->setAvPath($avPath);
63
		$this->settings->setAvInfectedAction($avInfectedAction);
64
		$this->settings->setAvStreamMaxLength($avStreamMaxLength);
65
		$this->settings->setAvMaxFileSize($avMaxFileSize);
66
67
		try {
68
			$scanner = $this->scannerFactory->getScanner();
69
			$result = $scanner->scanString("dummy scan content");
70
			$success = $result->getNumericStatus() == Status::SCANRESULT_CLEAN;
71
			$message = $success ? $this->l10n->t('Saved') : 'unexpected scan results for test content';
72
		} catch (\Exception $e) {
73
			$message = $e->getMessage();
74
			$success = false;
75
		}
76
77
		return new JSONResponse(
78
			['data' =>
79
				['message' => $message],
80
				'status' => $success ? 'success' : 'error',
81
				'settings' => $this->settings->getAllValues(),
82
			]
83
		);
84
	}
85
}
86