Completed
Push — master ( b8000a...9e8244 )
by Roeland
10s
created

SettingsController::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 20
cp 0
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 9
crap 2

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 \OCP\AppFramework\Controller;
12
use \OCP\IRequest;
13
use \OCP\IL10N;
14
use \OCA\Files_Antivirus\AppConfig;
15
16
use \OCP\AppFramework\Http\TemplateResponse;
17
use \OCP\AppFramework\Http\JSONResponse;
18
19
class SettingsController extends Controller {
20
21
	/**
22
	 * @var AppConfig
23
	 */
24
	private $settings;
25
	
26
	/**
27
	 * @var IL10N
28
	 */
29
	private $l10n;
30
	
31
	public function __construct($appName, IRequest $request, AppConfig $appconfig, IL10N $l10n) {
32
		parent::__construct($appName, $request);
33
34
		$this->settings = $appconfig;
35
		$this->l10n = $l10n;
36
	}
37
38
	/**
39
	 * Save Parameters
40
	 *
41
	 * @param string $avMode - antivirus mode
42
	 * @param string $avSocket - path to socket (Socket mode)
43
	 * @param string $avHost - antivirus url
44
	 * @param int $avPort - port
45
	 * @param string $avCmdOptions - extra command line options
46
	 * @param string $avPath - path to antivirus executable (Executable mode)
47
	 * @param string $avInfectedAction - action performed on infected files
48
	 * @param $avStreamMaxLength - reopen socket after bytes
49
	 * @param int $avMaxFileSize - file size limit
50
	 * @return JSONResponse
51
	 */
52
	public function save($avMode, $avSocket, $avHost, $avPort, $avCmdOptions, $avPath, $avInfectedAction, $avStreamMaxLength, $avMaxFileSize) {
53
		$this->settings->setAvMode($avMode);
54
		$this->settings->setAvSocket($avSocket);
55
		$this->settings->setAvHost($avHost);
56
		$this->settings->setAvPort($avPort);
57
		$this->settings->setAvCmdOptions($avCmdOptions);
58
		$this->settings->setAvPath($avPath);
59
		$this->settings->setAvInfectedAction($avInfectedAction);
60
		$this->settings->setAvStreamMaxLength($avStreamMaxLength);
0 ignored issues
show
Documentation Bug introduced by
The method setAvStreamMaxLength does not exist on object<OCA\Files_Antivirus\AppConfig>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
61
		$this->settings->setAvMaxFileSize($avMaxFileSize);
62
		
63
		return new JSONResponse(
64
			['data' =>
65
				['message' =>
66
					$this->l10n->t('Saved')
67
				],
68
				'status' => 'success',
69
				'settings' => $this->settings->getAllValues()
70
			]
71
		);
72
	}
73
}
74