Completed
Branch 0.2.1 (e70612)
by Anton
09:15
created

Definition::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 10
Ratio 100 %
Metric Value
dl 10
loc 10
rs 9.4286
cc 3
eloc 4
nc 3
nop 1
1
<?php
2
3
namespace Modules\Entitizer {
4
5
	use Exception;
6
7 View Code Duplication
	abstract class Definition {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
8
9
		private static $error_message = 'Entity definition does not exist';
10
11
		# Objects cache
12
13
		protected static $cache = [];
14
15
		# Definitions classes
16
17
		protected static $classes = [
18
19
			ENTITY_TYPE_PAGE                => 'Modules\Entitizer\Definition\Page',
20
			ENTITY_TYPE_MENUITEM            => 'Modules\Entitizer\Definition\Menuitem',
21
			ENTITY_TYPE_VARIABLE            => 'Modules\Entitizer\Definition\Variable',
22
			ENTITY_TYPE_WIDGET              => 'Modules\Entitizer\Definition\Widget',
23
			ENTITY_TYPE_USER                => 'Modules\Entitizer\Definition\User',
24
			ENTITY_TYPE_USER_SECRET         => 'Modules\Entitizer\Definition\User\Secret',
25
			ENTITY_TYPE_USER_SESSION        => 'Modules\Entitizer\Definition\User\Session'
26
		];
27
28
		# Get definition
29
30
		public static function get(string $type) {
31
32
			if (!isset(self::$classes[$type])) throw new Exception\General(self::$error_message);
33
34
			$cached = isset(self::$cache[$type]);
35
36
			# ------------------------
37
38
			return (!$cached ? (self::$cache[$type] = new self::$classes[$type]) : self::$cache[$type]);
39
		}
40
	}
41
}
42