contentExtensionEntry_Relationship_FieldDelete   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 111
Duplicated Lines 24.32 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 21
lcom 0
cbo 0
dl 27
loc 111
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F view() 27 102 21

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
	/**
3
	 * Copyright: Deux Huit Huit 2015
4
	 * LICENCE: MIT https://deuxhuithuit.mit-license.org
5
	 */
6
7
	if(!defined("__IN_SYMPHONY__")) die("<h2>Error</h2><p>You cannot directly access this file</p>");
8
9
	require_once(TOOLKIT . '/class.jsonpage.php');
10
11
	class contentExtensionEntry_Relationship_FieldDelete extends JSONPage {
12
13
		const NUMBER_OF_URL_PARAMETERS = 3;
14
		
15
		/**
16
		 *
17
		 * Builds the content view
18
		 */
19
		public function view() {
20
			if ($_SERVER['REQUEST_METHOD'] != 'POST') {
21
				$this->_Result['status'] = Page::HTTP_STATUS_BAD_REQUEST;
22
				$this->_Result['error'] = __('This page accepts posts only');
23
				$this->setHttpStatus($this->_Result['status']);
24
				return;
25
			}
26
			
27
			// _context[0] => entry id to delete
28
			// _context[1] => fieldId
29
			// _context[2] => current entry id (parent of entry id to delete)
30 View Code Duplication
			if (!is_array($this->_context) || empty($this->_context)) {
31
				$this->_Result['error'] = __('Parameters not found');
32
				return;
33
			}
34
			else if (count($this->_context) < self::NUMBER_OF_URL_PARAMETERS) {
35
				$this->_Result['error'] = __('Not enough parameters');
36
				return;
37
			}
38
			else if (count($this->_context) > self::NUMBER_OF_URL_PARAMETERS) {
39
				$this->_Result['error'] = __('Too many parameters');
40
				return;
41
			}
42
			
43
			// Validate to delete entry ID
44
			$rawToDeleteEntryId = MySQL::cleanValue($this->_context[0]);
45
			$toDeleteEntryId = General::intval($rawToDeleteEntryId);
46
			if ($toDeleteEntryId < 1) {
47
				$this->_Result['error'] = __('No entry no found');
48
				return;
49
			}
50
			
51
			// Validate parent field exists
52
			$parentFieldId = General::intval(MySQL::cleanValue($this->_context[1]));
53
			if ($parentFieldId < 1) {
54
				$this->_Result['error'] = __('Parent id not valid');
55
				return;
56
			}
57
			$parentField = FieldManager::fetch($parentFieldId);
58
			if (!$parentField || empty($parentField)) {
59
				$this->_Result['error'] = __('Parent field not found');
60
				return;
61
			}
62
			
63
			// Validate parent entry ID
64
			$rawEntryId = MySQL::cleanValue($this->_context[2]);
65
			$entryId = General::intval($rawEntryId);
66 View Code Duplication
			if ($entryId < 1) {
67
				$this->_Result['error'] = sprintf(
68
					__('Parent entry id `%s` not valid'),
69
					$rawEntryId
70
				);
71
				return;
72
			}
73
			
74
			// Validate parent entry exists
75
			$entry = EntryManager::fetch($entryId);
76 View Code Duplication
			if ($entry == null || count($entry) != 1) {
77
				$this->_Result['error'] = __('Parent entry not found');
78
				return;
79
			}
80
			if (is_array($entry)) {
81
				$entry = $entry[0];
82
			}
83 View Code Duplication
			if ($entry->get('section_id') != $parentField->get('parent_section')) {
84
				$this->_Result['error'] = __('Field and entry do not belong together');
85
				return;
86
			}
87
			
88
			// Validate to delete entry exists
89
			$toDeleteEntry = EntryManager::fetch($toDeleteEntryId);
90
			if ($toDeleteEntry == null || count($toDeleteEntry) != 1) {
91
				$this->_Result['error'] = __('Entry not found');
92
				return;
93
			}
94
			if (is_array($toDeleteEntry)) {
95
				$toDeleteEntry = $toDeleteEntry[0];
96
			}
97
			
98
			// Validate entry is not linked anywhere else
99
			if (!isset($_REQUEST['no-assoc'])) {
100
				//$toDeleteSection = SectionManager::fetch($toDeleteEntry->get('section_id'));
101
				//$toDeleteAssoc = $toDeleteSection->fetchChildAssociations(false);
102
				$toDeleteAssoc = SectionManager::fetchChildAssociations($toDeleteEntry->get('section_id'), false);
103
				//var_dump($toDeleteAssoc);die;
104
				// TODO: find if the toDeleteEntry is linked or not.
105
				if (count($toDeleteAssoc) > 1) {
106
					$this->_Result['assoc'] = true;
107
					$this->_Result['error'] = __('Entry might be link elsewhere. Do you want to continue?');
108
					return;
109
				}
110
			}
111
			
112
			// Delete the entry
113
			if (!EntryManager::delete($toDeleteEntryId)) {
114
				$this->_Result['error'] = __('Could not delete the entry');
115
				return;
116
			}
117
			
118
			$this->_Result['entry-id'] = $entryId;
119
			$this->_Result['ok'] = true;
120
		}
121
	}