Completed
Push — master ( c1632c...027069 )
by Joas
22:03 queued 08:37
created

AppsController::enable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Roeland Jago Douma <[email protected]>
8
 * @author Tom Needham <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OCA\Provisioning_API\Controller;
27
28
use OC\OCSClient;
29
use \OC_App;
30
use OCP\App\IAppManager;
31
use OCP\AppFramework\Http\DataResponse;
32
use OCP\AppFramework\OCS\OCSException;
33
use OCP\AppFramework\OCS\OCSNotFoundException;
34
use OCP\AppFramework\OCSController;
35
use OCP\IRequest;
36
37
class AppsController extends OCSController {
38
	/** @var \OCP\App\IAppManager */
39
	private $appManager;
40
	/** @var OCSClient */
41
	private $ocsClient;
42
43
	/**
44
	 * @param string $appName
45
	 * @param IRequest $request
46
	 * @param IAppManager $appManager
47
	 * @param OCSClient $ocsClient
48
	 */
49
	public function __construct(
50
		$appName,
51
		IRequest $request,
52
		IAppManager $appManager,
53
		OCSClient $ocsClient
54
	) {
55
		parent::__construct($appName, $request);
56
57
		$this->appManager = $appManager;
58
		$this->ocsClient = $ocsClient;
59
	}
60
61
	/**
62
	 * @param string $filter
63
	 * @return DataResponse
64
	 * @throws OCSException
65
	 */
66
	public function getApps($filter = null) {
67
		$apps = OC_App::listAllApps(false, true, $this->ocsClient);
68
		$list = [];
69
		foreach($apps as $app) {
70
			$list[] = $app['id'];
71
		}
72
		if($filter){
0 ignored issues
show
Bug Best Practice introduced by
The expression $filter of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
73
			switch($filter){
74
				case 'enabled':
75
					return new DataResponse(['apps' => \OC_App::getEnabledApps()]);
76
					break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
77
				case 'disabled':
78
					$enabled = OC_App::getEnabledApps();
79
					return new DataResponse(['apps' => array_diff($list, $enabled)]);
80
					break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
81
				default:
82
					// Invalid filter variable
83
					throw new OCSException('', 101);
84
			}
85
86
		} else {
87
			return new DataResponse(['apps' => $list]);
88
		}
89
	}
90
91
	/**
92
	 * @param string $app
93
	 * @return DataResponse
94
	 * @throws OCSNotFoundException
95
	 */
96
	public function getAppInfo($app) {
97
		$info = \OCP\App::getAppInfo($app);
98
		if(!is_null($info)) {
99
			return new DataResponse(OC_App::getAppInfo($app));
0 ignored issues
show
Bug introduced by
It seems like \OC_App::getAppInfo($app) targeting OC_App::getAppInfo() can also be of type null; however, OCP\AppFramework\Http\DataResponse::__construct() does only seem to accept array|object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
100
		} else {
101
			throw new OCSException('The request app was not found', \OCP\API::RESPOND_NOT_FOUND);
102
		}
103
	}
104
105
	/**
106
	 * @param string $app
107
	 * @return DataResponse
108
	 */
109
	public function enable($app) {
110
		$this->appManager->enableApp($app);
111
		return new DataResponse();
112
	}
113
114
	/**
115
	 * @param string $app
116
	 * @return DataResponse
117
	 */
118
	public function disable($app) {
119
		$this->appManager->disableApp($app);
120
		return new DataResponse();
121
	}
122
123
}
124