Passed
Push — master ( ba823b...756562 )
by Jeroen
06:16
created

Route::resolvePageOwner()   C

Complexity

Conditions 13
Paths 15

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 31
nc 15
nop 0
dl 0
loc 47
rs 5.0999
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Elgg\Router;
4
5
/**
6
 * Route Wrapper
7
 */
8
class Route extends \Symfony\Component\Routing\Route {
9
10
	/**
11
	 * @var array
12
	 */
13
	protected $parameters = [];
14
15
	/**
16
	 * Set matched parameters
17
	 *
18
	 * @param array $parameters Parameters
19
	 *
20
	 * @return static
21
	 * @internal
22
	 */
23
	public function setMatchedParameters(array $parameters) {
24
		$this->parameters = $parameters;
25
26
		return $this;
27
	}
28
29
	/**
30
	 * Get matched parameters
31
	 *
32
	 * @return array
33
	 * @internal
34
	 */
35
	public function getMatchedParameters() {
36
		return $this->parameters;
37
	}
38
39
	/**
40
	 * Attemps to resolve page owner from route parameters
41
	 *
42
	 * @return \ElggEntity|null
43
	 * @internal
44
	 */
45
	public function resolvePageOwner() {
46
47
		$params = $this->getMatchedParameters();
48
		$route = $params['_route'];
49
50
		$route_parts = explode(':', $route);
51
52
		$from_guid = function ($guid) {
53
			$entity = get_entity($guid);
54
			if ($entity instanceof \ElggUser || $entity instanceof \ElggGroup) {
55
				return $entity;
56
			} else if ($entity instanceof \ElggObject) {
57
				return $entity->getContainerEntity();
58
			}
59
		};
60
61
		switch ($route_parts[0]) {
62
			case 'view' :
63
			case 'edit' :
64
				$username = elgg_extract('username', $params);
65
				if ($username) {
66
					return get_user_by_username($username);
0 ignored issues
show
Bug Best Practice introduced by Ismayil Khayredinov
The expression return get_user_by_username($username) could also return false which is incompatible with the documented return type null|ElggEntity. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
67
				}
68
69
				$guid = elgg_extract('guid', $params);
70
				if ($guid) {
71
					return $from_guid($guid);
72
				}
73
				break;
74
75
			case 'add' :
76
			case 'collection' :
77
				$username = elgg_extract('username', $params);
78
				if ($username) {
79
					return get_user_by_username($username);
0 ignored issues
show
Bug Best Practice introduced by Ismayil Khayredinov
The expression return get_user_by_username($username) could also return false which is incompatible with the documented return type null|ElggEntity. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
80
				}
81
82
				$guid = elgg_extract('guid', $params);
83
				if ($guid) {
84
					return $from_guid($guid);
85
				}
86
87
				$container_guid = elgg_extract('container_guid', $params);
88
				if ($container_guid) {
89
					return $from_guid($container_guid);
90
				}
91
				break;
92
		}
93
94
	}
95
}
96