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

Entitizer::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 2
1
<?php
2
3
namespace Modules {
4
5
	use Exception;
6
7 View Code Duplication
	abstract class Entitizer {
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 = 'Invalid entity type';
10
11
		# Objects cache
12
13
		protected static $cache = [];
14
15
		# Entities classes
16
17
		protected static $classes = [
18
19
			ENTITY_TYPE_PAGE                => 'Modules\Entitizer\Entity\Page',
20
			ENTITY_TYPE_MENUITEM            => 'Modules\Entitizer\Entity\Menuitem',
21
			ENTITY_TYPE_VARIABLE            => 'Modules\Entitizer\Entity\Variable',
22
			ENTITY_TYPE_WIDGET              => 'Modules\Entitizer\Entity\Widget',
23
			ENTITY_TYPE_USER                => 'Modules\Entitizer\Entity\User',
24
			ENTITY_TYPE_USER_SECRET         => 'Modules\Entitizer\Entity\User\Secret',
25
			ENTITY_TYPE_USER_SESSION        => 'Modules\Entitizer\Entity\User\Session'
26
		];
27
28
		# Get entity
29
30
		public static function get(string $type, int $id = 0) {
31
32
			if (!isset(self::$classes[$type])) throw new Exception\General(self::$error_message);
33
34
			$cached = isset(self::$cache[$type][$id]);
35
36
			# ------------------------
37
38
			return (!$cached ? new self::$classes[$type]($id) : self::$cache[$type][$id]);
39
		}
40
	}
41
}
42