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

HasLinks   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A decodeArgs() 0 8 3
A moduleLink() 0 10 4
A encodeArgs() 0 10 2
A selfLink() 0 14 3
1
<?php
2
3
namespace Epesi\Core\System\Modules\Concerns;
4
5
use Epesi\Core\System\Modules\ModuleView;
6
use Illuminate\Support\Str;
7
use atk4\core\SessionTrait;
8
9
trait HasLinks
10
{
11
	use HasModule;
12
	
13
	/**
14
	 * Create self module link
15
	 * 
16
	 * @param string $method
17
	 * @param array $args
18
	 * @return string
19
	 */
20
	final public static function selfLink($method = 'body', $args = [])
21
	{
22
		$defaultView = self::module()::view();
23
		
24
		$viewClass = is_a(static::class, ModuleView::class, true)? static::class: $defaultView;
25
		
26
		$viewAlias = null;
27
		if ($viewClass !== $defaultView) {
28
			$names = array_slice(explode('\\', $viewClass), -1);
29
			
30
			$viewAlias = str_ireplace('_', '-', Str::snake(reset($names)));
31
		}
32
		
33
		return url(implode('/', ['view', implode(':', array_filter([self::alias(), $viewAlias])), $method, self::encodeArgs($args)]));
34
	}
35
	
36
	/**
37
	 * Create module link
38
	 * Associative $args are used to set module properties
39
	 * Numeric key values are used as method arguments
40
	 * 
41
	 * @param string $module
42
	 * @param string $method
43
	 * @param array $args
44
	 * @return string
45
	 */
46
	final public static function moduleLink($module, $method = 'body', $args = [])
47
	{
48
		$alias = class_exists($module)? $module::alias(): $module;
49
		
50
		if (is_array($method) && !$args) {
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...
introduced by
The condition is_array($method) is always false.
Loading history...
51
		    $args = $method;
52
		    $method = 'body';
53
		}
54
		
55
		return url(implode('/', ['view', $alias, $method, self::encodeArgs($args)]));
56
	}
57
	
58
	/**
59
	 * Decode the arguments hash and return stored arguments
60
	 * 
61
	 * @param string $hash
62
	 * @return array
63
	 * 
64
	 * @throws \Illuminate\Http\Exceptions\HttpResponseException
65
	 */
66
	final public static function decodeArgs($hash) {
67
		if (! $hash) return [];
68
		
69
		$args = session($hash);
70
71
		if (is_null($args)) abort(419);
72
		
73
		return (array) $args;
74
	}
75
	
76
	/**
77
	 * Encode arguments for the module method
78
	 * 
79
	 * @param array|mixed $args
80
	 * @return string
81
	 */
82
	final public static function encodeArgs($args) {
83
		$args = (array) $args;
84
		
85
		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...
86
		
87
		$hash = md5(serialize($args));
88
		
89
		session([$hash => $args]);
90
		
91
		return $hash;
92
	}
93
}
94