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

Route   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setMatchedParameters() 0 4 1
C resolvePageOwner() 0 47 13
A getMatchedParameters() 0 2 1
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
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
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