| Conditions | 16 |
| Paths | 10248 |
| Total Lines | 120 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 63 | public function parseResults(LoopResult $loopResult) |
||
| 64 | { |
||
| 65 | // Create image processing event |
||
| 66 | $event = new ImageEvent(); |
||
| 67 | |||
| 68 | // Prepare tranformations |
||
| 69 | $width = $this->getWidth(); |
||
| 70 | $height = $this->getHeight(); |
||
| 71 | $rotation = $this->getRotation(); |
||
| 72 | $background_color = $this->getBackgroundColor(); |
||
| 73 | $quality = $this->getQuality(); |
||
| 74 | $effects = $this->getEffects(); |
||
| 75 | |||
| 76 | $event->setAllowZoom($this->getAllowZoom()); |
||
| 77 | |||
| 78 | if (! is_null($effects)) { |
||
| 79 | $effects = explode(',', $effects); |
||
| 80 | } |
||
| 81 | |||
| 82 | switch ($this->getResizeMode()) { |
||
| 83 | case 'crop': |
||
| 84 | $resizeMode = \Thelia\Action\Image::EXACT_RATIO_WITH_CROP; |
||
| 85 | break; |
||
| 86 | |||
| 87 | case 'borders': |
||
| 88 | $resizeMode = \Thelia\Action\Image::EXACT_RATIO_WITH_BORDERS; |
||
| 89 | break; |
||
| 90 | |||
| 91 | case 'none': |
||
| 92 | default: |
||
| 93 | $resizeMode = \Thelia\Action\Image::KEEP_IMAGE_RATIO; |
||
| 94 | |||
| 95 | } |
||
| 96 | |||
| 97 | /** @var DealerImage $result */ |
||
| 98 | foreach ($loopResult->getResultDataCollection() as $result) { |
||
| 99 | // Setup required transformations |
||
| 100 | if (! is_null($width)) { |
||
| 101 | $event->setWidth($width); |
||
| 102 | } |
||
| 103 | if (! is_null($height)) { |
||
| 104 | $event->setHeight($height); |
||
| 105 | } |
||
| 106 | $event->setResizeMode($resizeMode); |
||
| 107 | if (! is_null($rotation)) { |
||
| 108 | $event->setRotation($rotation); |
||
| 109 | } |
||
| 110 | if (! is_null($background_color)) { |
||
| 111 | $event->setBackgroundColor($background_color); |
||
| 112 | } |
||
| 113 | if (! is_null($quality)) { |
||
| 114 | $event->setQuality($quality); |
||
| 115 | } |
||
| 116 | if (! is_null($effects)) { |
||
| 117 | $event->setEffects($effects); |
||
| 118 | } |
||
| 119 | |||
| 120 | // Put source image file path |
||
| 121 | $sourceFilePath = sprintf( |
||
| 122 | '%s/%s', |
||
| 123 | $result->getUploadDir(), |
||
| 124 | $result->getFile() |
||
| 125 | ); |
||
| 126 | |||
| 127 | $event->setSourceFilepath($sourceFilePath); |
||
| 128 | $event->setCacheSubdirectory($this->objectType); |
||
| 129 | |||
| 130 | $loopResultRow = new LoopResultRow($result); |
||
| 131 | |||
| 132 | $loopResultRow |
||
| 133 | ->set("ID", $result->getId()) |
||
| 134 | ->set("LOCALE", $this->locale) |
||
| 135 | ->set("ORIGINAL_IMAGE_PATH", $sourceFilePath) |
||
| 136 | ->set("TITLE", $result->getVirtualColumn('i18n_TITLE')) |
||
| 137 | ->set("CHAPO", $result->getVirtualColumn('i18n_CHAPO')) |
||
| 138 | ->set("DESCRIPTION", $result->getVirtualColumn('i18n_DESCRIPTION')) |
||
| 139 | ->set("POSTSCRIPTUM", $result->getVirtualColumn('i18n_POSTSCRIPTUM')) |
||
| 140 | ->set("OBJECT_TYPE", $this->objectType) |
||
| 141 | ->set("OBJECT_ID", $this->objectId) |
||
| 142 | ; |
||
| 143 | |||
| 144 | $addRow = true; |
||
| 145 | |||
| 146 | $returnErroredImages = $this->getBackendContext() || ! $this->getIgnoreProcessingErrors(); |
||
| 147 | |||
| 148 | try { |
||
| 149 | // Dispatch image processing event |
||
| 150 | $this->dispatcher->dispatch(TheliaEvents::IMAGE_PROCESS, $event); |
||
| 151 | |||
| 152 | $loopResultRow |
||
| 153 | ->set("IMAGE_URL", $event->getFileUrl()) |
||
| 154 | ->set("ORIGINAL_IMAGE_URL", $event->getOriginalFileUrl()) |
||
| 155 | ->set("IMAGE_PATH", $event->getCacheFilepath()) |
||
| 156 | ->set("PROCESSING_ERROR", false) |
||
| 157 | ; |
||
| 158 | } catch (\Exception $ex) { |
||
| 159 | // Ignore the result and log an error |
||
| 160 | Tlog::getInstance()->addError(sprintf("Failed to process image in lapelerine_product_image loop: %s", $ex->getMessage())); |
||
| 161 | |||
| 162 | if ($returnErroredImages) { |
||
| 163 | $loopResultRow |
||
| 164 | ->set("IMAGE_URL", '') |
||
| 165 | ->set("ORIGINAL_IMAGE_URL", '') |
||
| 166 | ->set("IMAGE_PATH", '') |
||
| 167 | ->set("PROCESSING_ERROR", true) |
||
| 168 | ; |
||
| 169 | } else { |
||
| 170 | $addRow = false; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | if ($addRow) { |
||
| 175 | $this->addOutputFields($loopResultRow, $result); |
||
| 176 | |||
| 177 | $loopResult->addRow($loopResultRow); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | return $loopResult; |
||
| 182 | } |
||
| 183 | } |