Passed
Push — dougall-pheobe-database-map-fi... ( ...2d7731 )
by
unknown
22:10
created

App::getUtilityTypeById()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 10
rs 10
c 1
b 0
f 0
cc 3
nc 3
nop 1
ccs 0
cts 5
cp 0
crap 12
1
<?php
2
3
namespace neon\utilities;
4
5
use neon\utilities\utils\DbInfo;
6
use neon\utilities\utils\PhpInfo;
7
use neon\utilities\utils\SystemReport;
8
use neon\utilities\utils\Updates;
9
10
/**
11
 * The neon core settings app class
12
 *
13
 * @property \neon\settings\services\SettingsManager  $settingsManager
14
 */
15
class App extends \neon\core\BaseApp
16
{
17
	/**
18
	 * @event RegisterUtilitiesEvent The event that is triggered when registering utilities
19
	 *
20
	 * Utility types must implement [[IUtility]]. [[\neon\utilities\utils\IUtility]] provides a base implementation.
21
	 * ---
22
	 * ```php
23
	 * use neon\utilities\events\RegisterUtils;
24
	 * use neon\utilities\App as Utility;
25
	 * use yii\base\Event;
26
	 *
27
	 * Event::on(Utility::class, Utility::RegisterUtils, function(Register $event) {
28
	 *     $event->utils[] = MyUtilityType::class;
29
	 * });
30
	 *
31
	 * // or
32
	 *
33
	 * neon()->getApp('utilities')->onRegisterUtils(function(Register $event) {
34
	 *     $event->utils[] = MyUtilityType::class;
35
	 * });
36
	 * ```
37
	 */
38
	const EVENT_REGISTER_UTILITIES = 'registerUtilities';
39
40
	/**
41
	 * Returns all available utility type classes.
42
	 *
43
	 * @return string[]
44
	 */
45
	public function getAllUtilities(): array
46
	{
47
		$utils = [
48
			Updates::class,
49
			SystemReport::class,
50
			PhpInfo::class,
51
			DbInfo::class,
52
			Cache::class,
0 ignored issues
show
Bug introduced by
The type neon\utilities\Cache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
53
			DeprecationErrors::class,
0 ignored issues
show
Bug introduced by
The type neon\utilities\DeprecationErrors was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
54
			DbBackup::class,
0 ignored issues
show
Bug introduced by
The type neon\utilities\DbBackup was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
55
			Migrations::class,
0 ignored issues
show
Bug introduced by
The type neon\utilities\Migrations was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
56
			Errors::class
0 ignored issues
show
Bug introduced by
The type neon\utilities\Errors was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
		];
58
59
		$event = new RegisterUtils(['utils' => $utils]);
0 ignored issues
show
Bug introduced by
The type neon\utilities\RegisterUtils was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
60
		$this->trigger(self::EVENT_REGISTER_UTILITIES, $event);
61
62
		return $event->utils;
63
	}
64
65
	/**
66
	 * Returns all utility type classes that the user has permission to use.
67
	 *
68
	 * @return string[]
69
	 */
70
	public function getAuthorizedUtilities(): array
71
	{
72
		$utilityTypes = [];
73
74
		foreach ($this->getAllUtilities() as $class) {
75
			if ($this->checkAuthorization($class)) {
76
				$utilityTypes[] = $class;
77
			}
78
		}
79
80
		return $utilityTypes;
81
	}
82
83
	/**
84
	 * Returns whether the current user is authorized to use a given utility.
85
	 *
86
	 * @param string $class The utility class
87
	 * @return bool
88
	 */
89
	public function checkAuthorization(string $class): bool
90
	{
91
		/** @var string|IUtilitiy $class */
92
		return neon()->getUser()->checkPermission('utility:' . $class::id());
0 ignored issues
show
Bug introduced by
The method checkPermission() does not exist on neon\user\services\User. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
		return neon()->getUser()->/** @scrutinizer ignore-call */ checkPermission('utility:' . $class::id());
Loading history...
93
	}
94
95
	/**
96
	 * Returns a utility class by its ID
97
	 *
98
	 * @param string $id
99
	 * @return string|null
100
	 */
101
	public function getUtilityTypeById(string $id)
102
	{
103
		foreach ($this->getAllUtilityTypes() as $class) {
0 ignored issues
show
Bug introduced by
The method getAllUtilityTypes() does not exist on neon\utilities\App. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
		foreach ($this->/** @scrutinizer ignore-call */ getAllUtilityTypes() as $class) {
Loading history...
104
			/** @var UtilityInterface $class */
105
			if ($class::id() === $id) {
106
				return $class;
107
			}
108
		}
109
110
		return null;
111
	}
112
113
	/**
114
	 * Shorthand method to register an event
115
	 *
116
	 * @param Callable $handler
117
	 */
118
	public function onRegisterUtils($handler)
119
	{
120
		$this->on(self::EVENT_REGISTER_UTILITIES, $handler);
121
	}
122
123
	/**
124
	 * @inheritdoc
125
	 */
126
	public function getMenu()
127
	{
128
		return [
129
			'label' => $this->getName(),
130
			'order' => 2000,
131
			'url' => ['/utilities/index/index'],
132
			'visible' => neon()->user->is('neon-administrator'),
133
			'active' => is_route('/utilities*')
134
		];
135
	}
136
137
	/**
138
	 * @inheritdoc
139
	 */
140
	public function getName()
141
	{
142
		return 'Utilities';
143
	}
144
145
}
146