view()   F
last analyzed

Complexity

Conditions 13
Paths 368

Size

Total Lines 95

Duplication

Lines 16
Ratio 16.84 %

Importance

Changes 0
Metric Value
dl 16
loc 95
rs 3.0557
c 0
b 0
f 0
cc 13
nc 368
nop 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
 * Copyright: Deux Huit Huit 2017
4
 * LICENCE: MIT https://deuxhuithuit.mit-license.org
5
 */
6
7
require_once(TOOLKIT . '/class.jsonpage.php');
8
9
class contentExtensionEntry_relationship_fieldSearch extends JSONPage
10
{
11
	public function view()
12
	{
13 View Code Duplication
		if (class_exists('FLang')) {
14
			try {
15
				FLang::setMainLang(Lang::get());
16
				FLang::setLangCode(Lang::get(), '');
17
			} catch (Exception $ex) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
18
		}
19
		
20
		$section = General::sanitize($this->_context[0]);
21
		$sectionId = SectionManager::fetchIDFromHandle($section);
22
		$sectionId = General::intval($sectionId);
23
		$excludes = !isset($this->_context[1]) ? null : array_filter(
24
			array_map(
25
				array('General', 'intval'),
26
				explode(',', General::sanitize($this->_context[1]))
27
			),
28
			function ($item) {
29
				return $item !== -1;
30
			}
31
		);
32
		if (empty($excludes)) {
33
			$excludes = '';
34
		} else {
35
			$excludes = " AND `e`.`id` NOT IN (" . implode(',', $excludes) . ") ";
36
		}
37
38
		if ($sectionId < 1) {
39
			$this->_Result['status'] = Page::HTTP_STATUS_BAD_REQUEST;
40
			$this->_Result['error'] = __('No section id found');
41
			return;
42
		}
43
44
		$section = SectionManager::fetch($sectionId);
45
46 View Code Duplication
		if (empty($section)) {
47
			$this->_Result['status'] = Page::HTTP_STATUS_NOT_FOUND;
48
			$this->_Result['error'] = __('Section not found');
49
			return;
50
		}
51
52
		$query = General::sanitize($_GET['query']);
53
		$entries = array();
54
		$filterableFields = $section->fetchFilterableFields();
55 View Code Duplication
		if (empty($filterableFields)) {
56
			$this->_Result['status'] = Page::HTTP_STATUS_BAD_REQUEST;
57
			$this->_Result['error'] = __('Section not filterable');
58
			return;
59
		}
60
61
		$primaryField = $section->fetchVisibleColumns();
62
		if (empty($primaryField)) {
63
			$primaryField = current($filterableFields);
64
			reset($filterableFields);
65
		} else {
66
			$primaryField = current($primaryField);
67
		}
68
69
		foreach ($filterableFields as $fId => $field) {
70
			EntryManager::setFetchSorting($fId, 'ASC');
71
			$fQuery = $query;
72
			$joins = '';
73
			$where = $excludes;
74
			$opt = array_map(function ($op) {
75
				return trim($op['filter']);
76
			}, $field->fetchFilterableOperators());
77
			if (in_array('regexp:', $opt)) {
78
				$fQuery = 'regexp: ' . $fQuery;
79
			}
80
			$field->buildDSRetrievalSQL(array($fQuery), $joins, $where, false);
81
			$fEntries = EntryManager::fetch(null, $sectionId, 10, 0, $where, $joins, true, false);
82
			if (!empty($fEntries)) {
83
				$entries = array_merge($entries, $fEntries);
84
			}
85
		}
86
87
		EntryManager::setFetchSorting('system:id', 'ASC');
88
		if (!empty($entries)) {
89
			$entries = EntryManager::fetch(array_unique(array_map(function ($e) {
90
				return $e['id'];
91
			}, $entries)), $sectionId);
92
		}
93
94
		$entries = array_map(function ($entry) use ($primaryField) {
95
			return array(
96
				'value' => $entry->get('id') . ':' .
97
					$primaryField->prepareReadableValue(
98
						$entry->getData($primaryField->get('id')),
99
						$entry->get('id')
100
				),
101
			);
102
		}, $entries);
103
104
		$this->_Result['entries'] = $entries;
105
	}
106
}
107