ConfigController::set()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace OCA\Chat\Controller;
4
5
use \OCA\Chat\Db\ConfigMapper;
6
use \OCA\Chat\IBackendManager;
7
use \OCP\AppFramework\Controller;
8
use \OCP\AppFramework\Http\JSONResponse;
9
use \OCP\AppFramework\Http;
10
use \OCP\IRequest;
11
use \OCA\Chat\BackendNotFoundException;
12
13
14
class ConfigController extends Controller {
15
16
	/**
17
	 * @var \OCA\Chat\Db\ConfigMapper
18
	 */
19
	private $configMapper;
20
21
	/**
22
	 * @var \OCA\Chat\BackendManager
23
	 */
24
	private $backendManager;
25
26
	public function __construct($appName, IRequest $request, ConfigMapper $configMapper, IBackendManager $backendManager){
27
		parent::__construct($appName, $request);
28
		$this->configMapper = $configMapper;
29
		$this->backendManager = $backendManager;
0 ignored issues
show
Documentation Bug introduced by
$backendManager is of type object<OCA\Chat\IBackendManager>, but the property $backendManager was declared to be of type object<OCA\Chat\BackendManager>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
30
	}
31
32
	/**
33
	 * @NoAdminRequired
34
	 * @param $backends
35
	 * @return JSONResponse
36
	 */
37
	public function set($backends){
38
		foreach($backends as $backend){
39
			foreach($backend['config'] as $key=>$value){
40
				$this->configMapper->set($backend['id'], $key, $value);
41
			}
42
		}
43
		$res = new JSONResponse();
44
		$res->setStatus(Http::STATUS_OK);
45
		return $res;
46
	}
47
48
	/**
49
	 * @param $id
50
	 * @return JSONResponse
51
	 */
52
	public function enableBackend($id){
53
		try {
54
			$this->backendManager->enableBackend($id);
55
			return new JSONResponse(array("status" => "success"));
56
		} Catch (BackendNotFoundException $e) {
57
			return new JSONResponse(array("status" => "error", "msg" => 404));
58
		}
59
	}
60
61
	/**
62
	 * @param $id
63
	 * @return JSONResponse
64
	 */
65
	public function disableBackend($id){
66
		try {
67
			$this->backendManager->disableBackend($id);
68
			return new JSONResponse(array("status" => "success"));
69
		} Catch (BackendNotFoundException $e) {
70
			return new JSONResponse(array("status" => "error", "msg" => 404));
71
		}
72
	}
73
}