HasLinks::selfLink()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 2
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Epesi\Core\System\Modules\Concerns;
4
5
use Epesi\Core\System\Modules\ModuleView;
6
use Illuminate\Support\Str;
7
8
trait HasLinks
9
{
10
	use HasModule;
11
	
12
	/**
13
	 * Create self module link
14
	 * 
15
	 * @param string $method
16
	 * @param array $args
17
	 * @return string
18
	 */
19
	final public static function selfLink($method = 'body', $args = [])
20
	{
21
		$defaultView = self::module()::view();
22
		
23
		$viewClass = is_a(static::class, ModuleView::class, true)? static::class: $defaultView;
24
		
25
		$viewAlias = null;
26
		if ($viewClass !== $defaultView) {
27
			$names = array_slice(explode('\\', $viewClass), -1);
28
			
29
			$viewAlias = str_ireplace('_', '-', Str::snake(reset($names)));
30
		}
31
		
32
		return url(implode('/', ['view', implode(':', array_filter([self::alias(), $viewAlias])), $method, self::encodeArgs($args)]));
33
	}
34
	
35
	/**
36
	 * Create module link
37
	 * Associative $args are used to set module properties
38
	 * Numeric key values are used as method arguments
39
	 * 
40
	 * @param string $module
41
	 * @param string $method
42
	 * @param array $args
43
	 * @return string
44
	 */
45
	final public static function moduleLink($module, $method = 'body', $args = [])
46
	{
47
		$alias = class_exists($module)? $module::alias(): $module;
48
		
49
		if (is_array($method) && !$args) {
0 ignored issues
show
introduced by
The condition is_array($method) is always false.
Loading history...
Bug Best Practice introduced by
The expression $args of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
50
		    $args = $method;
51
		    $method = 'body';
52
		}
53
		
54
		return url(implode('/', ['view', $alias, $method, self::encodeArgs($args)]));
55
	}
56
	
57
	/**
58
	 * Decode the arguments hash and return stored arguments
59
	 * 
60
	 * @param string $hash
61
	 * @return array
62
	 * 
63
	 * @throws \Illuminate\Http\Exceptions\HttpResponseException
64
	 */
65
	final public static function decodeArgs($hash) {
66
		if (! $hash) return [];
67
		
68
		$args = session($hash);
69
70
		if (is_null($args)) abort(419);
71
		
72
		return (array) $args;
73
	}
74
	
75
	/**
76
	 * Encode arguments for the module method
77
	 * 
78
	 * @param array|mixed $args
79
	 * @return string
80
	 */
81
	final public static function encodeArgs($args) {
82
		$args = (array) $args;
83
		
84
		if (! $args) return;
0 ignored issues
show
Bug Best Practice introduced by
The expression $args of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
85
		
86
		$hash = md5(serialize($args));
87
		
88
		session([$hash => $args]);
89
		
90
		return $hash;
91
	}
92
}
93