EntitySortByClause::prepare()   F
last analyzed

Complexity

Conditions 17
Paths 270

Size

Total Lines 78
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 32.2402

Importance

Changes 0
Metric Value
cc 17
eloc 51
nc 270
nop 2
dl 0
loc 78
ccs 30
cts 48
cp 0.625
crap 32.2402
rs 3.5083
c 0
b 0
f 0

How to fix   Long Method    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\Database\Clauses;
4
5
use Elgg\Database\QueryBuilder;
6
use Elgg\Exceptions\DomainException;
7
8
/**
9
 * Extends QueryBuilder with clauses necesary to sort entity lists by entity properties
10
 */
11
class EntitySortByClause extends OrderByClause {
12
13
	/**
14
	 * @var string
15
	 */
16
	public $property;
17
18
	/**
19
	 * @var bool
20
	 */
21
	public $signed;
22
23
	/**
24
	 * @var string
25
	 */
26
	public $join_type;
27
28
	/**
29
	 * @var string
30
	 */
31
	public $property_type;
32
	
33
	/**
34
	 * @var bool
35
	 */
36
	public $inverse_relationship;
37
	
38
	/**
39
	 * @var int
40
	 */
41
	public $relationship_guid;
42
43
	/**
44
	 * {@inheritdoc}
45
	 *
46
	 * @throws DomainException
47
	 */
48 18
	public function prepare(QueryBuilder $qb, $table_alias = null) {
49
50 18
		if (!isset($this->property_type)) {
51 14
			if (in_array($this->property, \ElggEntity::PRIMARY_ATTR_NAMES)) {
52 3
				$this->property_type = 'attribute';
53
			} else {
54 11
				$this->property_type = 'metadata';
55
			}
56
		}
57
		
58
		// get correct base GUID column
59
		// default assumes the main table is 'entities'
60 18
		$from_column = 'guid';
61 18
		switch ($qb->getTableName()) {
62
			case QueryBuilder::TABLE_ANNOTATIONS:
63
			case QueryBuilder::TABLE_METADATA:
64
				$from_column = 'entity_guid';
65
				break;
66
			case QueryBuilder::TABLE_RELATIONSHIPS:
67
				$from_column = 'guid_one';
68
				if ((bool) $this->inverse_relationship) {
69
					$from_column = 'guid_two';
70
				}
71
				break;
72
		}
73
74 18
		switch ($this->property_type) {
75 18
			case 'metadata':
76 11
				$md_alias = $qb->joinMetadataTable($table_alias, $from_column, $this->property, $this->join_type);
77 11
				$column = "{$md_alias}.value";
78 11
				break;
79
80 7
			case 'attribute':
81 4
				if (!in_array($this->property, \ElggEntity::PRIMARY_ATTR_NAMES)) {
82 1
					throw new DomainException("'{$this->property}' is not a valid entity attribute");
83
				}
84
				
85 3
				if ($qb->getTableName() !== QueryBuilder::TABLE_ENTITIES) {
86
					$e_alias = $qb->joinEntitiesTable($table_alias, $from_column, $this->join_type);
87
				} else {
88 3
					$e_alias = $table_alias;
89
				}
90
				
91 3
				$column = "{$e_alias}.{$this->property}";
92 3
				break;
93
94 3
			case 'annotation':
95 2
				$an_alias = $qb->joinAnnotationTable($table_alias, $from_column, $this->property, $this->join_type);
96 2
				$column = "{$an_alias}.value";
97 2
				break;
98
99 1
			case 'relationship':
100
				if ($qb->getTableName() !== QueryBuilder::TABLE_RELATIONSHIPS) {
101
					$er_alias = $qb->joinRelationshipTable($table_alias, $from_column, $this->property, $this->inverse_relationship, $this->join_type);
102
					if (!empty($this->relationship_guid)) {
103
						$guid_column = $this->inverse_relationship ? 'guid_two' : 'guid_one';
104
						$qb->andWhere($qb->compare("{$er_alias}.{$guid_column}", '=', $this->relationship_guid, ELGG_VALUE_GUID));
105
					}
106
				} else {
107
					$er_alias = $table_alias;
108
				}
109
				
110
				$column = "{$er_alias}.time_created";
111
				
112
				break;
113
114
			default:
115 1
				elgg_log("'{$this->property_type}' is not a valid entity property type. Sorting ignored.");
116 1
				return null;
117
		}
118
119 16
		if ($this->signed) {
120 8
			$column = "CAST({$column} AS SIGNED)";
121
		}
122
123 16
		$this->expr = $column;
124
125 16
		return parent::prepare($qb, $table_alias);
126
	}
127
}
128