Passed
Push — master ( 054586...a82a12 )
by Anton
03:12
created

Entitizer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 12 3
A getDataset() 0 4 1
A getDefinition() 0 4 1
A getListview() 0 4 1
A getTreeview() 0 4 1
1
<?php
2
3
/**
4
 * @package Cadmium\System\Modules\Entitizer
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Modules {
11
12
	use Exception;
13
14
	abstract class Entitizer extends Entitizer\Utils\Cache {
15
16
		private static $error_message = 'The entity class for the given table does not exist';
17
18
		# Entities classes
19
20
		protected static $classes = [
21
22
			TABLE_PAGES             => 'Modules\Entitizer\Entity\Page',
23
			TABLE_MENU              => 'Modules\Entitizer\Entity\Menuitem',
24
			TABLE_VARIABLES         => 'Modules\Entitizer\Entity\Variable',
25
			TABLE_WIDGETS           => 'Modules\Entitizer\Entity\Widget',
26
			TABLE_USERS             => 'Modules\Entitizer\Entity\User',
27
			TABLE_USERS_SECRETS     => 'Modules\Entitizer\Entity\User\Secret',
28
			TABLE_USERS_SESSIONS    => 'Modules\Entitizer\Entity\User\Session'
29
		];
30
31
		/**
32
		 * Get an entity object by a given type (a table name) and an id. If the entity has not been already loaded,
33
		 * it will be selected from database, otherwise the cached object will be returned.
34
		 * If the id is not given or equals 0, an empty entity will be returned.
35
		 * If the table name is invalid, an exception will be thrown
36
		 */
37
38
		public static function get(string $table, int $id = 0) : Entitizer\Utils\Entity {
39
40
			if (!isset(self::$classes[$table])) throw new Exception\General(self::$error_message);
41
42
			if (isset(self::$cache[$table][$id])) return self::$cache[$table][$id];
43
44
			$entity = new self::$classes[$table]; $entity->init($id);
45
46
			# ------------------------
47
48
			return $entity;
49
		}
50
51
		/**
52
		 * Get a dataset object with a custom data
53
		 */
54
55
		public static function getDataset(string $table, array $data = []) : Entitizer\Utils\Dataset {
56
57
			return Entitizer\Dataset::get($table, $data);
58
		}
59
60
		/**
61
		 * Get a definition object
62
		 */
63
64
		public static function getDefinition(string $table) : Entitizer\Utils\Definition {
65
66
			return Entitizer\Definition::get($table);
67
		}
68
69
		/**
70
		 * Get a listview object
71
		 */
72
73
		public static function getListview(string $table) : Entitizer\Utils\Listview {
74
75
			return Entitizer\Listview::get($table);
76
		}
77
78
		/**
79
		 * Get a treeview object
80
		 */
81
82
		public static function getTreeview(string $table) : Entitizer\Utils\Treeview {
83
84
			return Entitizer\Treeview::get($table);
85
		}
86
	}
87
}
88