Test Failed
Push — master ( 8f2167...5d2217 )
by Georgi
08:27
created

ModuleJoint::collect()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php 
2
3
namespace Epesi\Core\System\Modules;
4
5
use Illuminate\Support\Collection;
6
7
abstract class ModuleJoint
8
{
9
	use Concerns\HasLinks;
10
	use Concerns\HasAccessControl;
11
	use Concerns\HasPackageManifest;
12
13
	/**
14
	 * List of runtime registered joints
15
	 * Can be used for tesing purposes
16
	 * 
17
	 * @var array
18
	 */
19
	protected static $registry = [];
20
	
21
	/**
22
	 * 	Make all joints which are applicable to static class and user has access to
23
	 *
24
	 * @return 	Collection
25
	 */
26
	final public static function collect()
27
	{
28
		$ret = collect();
29
		foreach (self::list() as $class) {
30
			/**
31
			 * @var ModuleJoint $joint
32
			 */
33
			$joint = new $class();
34
35
			if (! $joint->access()) continue;
36
			
37
			$ret->add($joint);
38
		}
39
		
40
		return $ret;
41
	}
42
		
43
	/**
44
	 * List all registered joints which are subclasses of static
45
	 */
46
	final public static function list()
47
	{
48
		$joints = collect(config('epesi.joints'))
49
			->merge(self::packageManifest()->joints())
50
			->merge(self::moduleJoints())
51
			->merge(self::$registry)
52
			->unique();
53
			
54
		return $joints->filter(function($class) {
55
			return is_a($class, static::class, true);
56
		});
57
	}
58
	
59
	/**
60
	 * Collect all joints declared in the modules and return array
61
	 *
62
	 * @return array
63
	 */
64
	final public static function moduleJoints()
65
	{
66
		$ret = collect();
67
		foreach (ModuleManager::getInstalled() as $module) {
68
			$ret = $ret->merge($module::joints());
69
		}
70
71
		return $ret->all();
72
	}
73
	
74
	final public static function register($class)
75
	{
76
		self::$registry[] = $class;
77
	}
78
}