| Conditions | 21 |
| Paths | 33 |
| Total Lines | 102 |
| Lines | 27 |
| Ratio | 26.47 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |