Completed
Push — master ( 5fa6d3...7e7cc5 )
by
unknown
03:10
created

SettingsController::save()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4

Importance

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