| Conditions | 8 |
| Paths | 6 |
| Total Lines | 51 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 doPublishBlocks() |
||
| 20 | { |
||
| 21 | $owner = $this->owner; |
||
| 22 | $ID = $owner->getRequest()->param('ID'); |
||
| 23 | $Page = BlocksPage::get()->byID($ID); |
||
| 24 | |||
| 25 | Block::$auto_update_page = false; |
||
| 26 | |||
| 27 | $DataCopy = []; |
||
| 28 | $Blocks = $Page->Blocks(); |
||
| 29 | foreach ($Blocks as $Block) { |
||
| 30 | $DataCopy[$Block->ID] = DB::query('SELECT BlockData FROM Block WHERE ID = ' . $Block->ID)->value(); |
||
| 31 | if (!$Block->BlockData) { |
||
| 32 | $Block->BlockData = $DataCopy[$Block->ID]; |
||
| 33 | } |
||
| 34 | $Block->write(); |
||
| 35 | // DB::prepared_query('UPDATE Block SET BlockData = ? WHERE ID = ' . $Block->ID, [$DataCopy[$Block->ID]]); |
||
| 36 | } |
||
| 37 | $Page->publishRecursive(); |
||
| 38 | |||
| 39 | // Publish in all other locales as well! |
||
| 40 | if ($Page->has_extension("\\TractorCow\\Fluent\\Extension\\FluentExtension")) { |
||
| 41 | $state = \TractorCow\Fluent\State\FluentState::singleton(); |
||
| 42 | $currentLocale = $state->getLocale(); |
||
| 43 | $allLocales = \TractorCow\Fluent\Model\Locale::get()->exclude('Locale', $currentLocale); |
||
| 44 | foreach ($allLocales as $locale) { |
||
| 45 | $state->withState(function ($state) use ($locale, $Page, $DataCopy) { |
||
| 46 | $state->setLocale($locale->Locale); |
||
| 47 | |||
| 48 | foreach ($Page->Blocks() as $Block) { |
||
| 49 | if (!$Block->BlockData) { |
||
| 50 | $Block->BlockData = $DataCopy[$Block->ID]; |
||
| 51 | } |
||
| 52 | $Block->write(); |
||
| 53 | } |
||
| 54 | |||
| 55 | $Page->publishRecursive(); |
||
| 56 | }); |
||
| 57 | } |
||
| 58 | |||
| 59 | // Preserve original data |
||
| 60 | // TODO: understand why Data is emptied |
||
| 61 | foreach ($Blocks as $Block) { |
||
| 62 | DB::prepared_query('UPDATE Block SET BlockData = ? WHERE ID = ' . $Block->ID, [$DataCopy[$Block->ID]]); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | $message = "Blocks published"; |
||
| 67 | |||
| 68 | $owner->getResponse()->addHeader('X-Status', rawurlencode($message)); |
||
| 69 | return $owner->getResponseNegotiator()->respond($owner->getRequest()); |
||
| 70 | } |
||
| 76 |