| Conditions | 25 |
| Paths | 2001 |
| Total Lines | 114 |
| 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 |
||
| 117 | public function getFormattedImage($format) |
||
| 118 | { |
||
| 119 | $args = func_get_args(); |
||
| 120 | $logger = CloudAssets::inst()->getLogger(); |
||
| 121 | |||
| 122 | if ($this->ID && $this->Filename && Director::fileExists($this->Filename)) { |
||
| 123 | $cacheFile = call_user_func_array(array($this, "cacheFilename"), $args); |
||
| 124 | $cachePath = Director::baseFolder()."/".$cacheFile; |
||
| 125 | |||
| 126 | /** @var CloudImageCachedStore $stored */ |
||
| 127 | $stored = CloudImageCachedStore::get()->filter('Filename', $cacheFile)->first(); |
||
| 128 | if ($stored && !$stored->exists()) { |
||
| 129 | $stored = null; |
||
| 130 | } |
||
| 131 | |||
| 132 | // If ?flush is present, always wipe existing data and start clean |
||
| 133 | if (isset($_GET['flush'])) { |
||
| 134 | // There was a bug here caused by the fact that GDBackend tries |
||
| 135 | // to read the size off the cached image, which would be a placeholder |
||
| 136 | // in certain cases. |
||
| 137 | // I'm not 100% sure what the correct behaviour is here. For now |
||
| 138 | // we'll destroy the existing image, causing it to be re-uploaded |
||
| 139 | // every time. That seems safer if a little bit wasteful. |
||
| 140 | $logger->debug("CloudAssets: deleting cached image because of flush: $cachePath"); |
||
| 141 | if (file_exists($cachePath)) { |
||
| 142 | unlink($cachePath); |
||
| 143 | } |
||
| 144 | |||
| 145 | // delete the existing meta if it existed |
||
| 146 | if ($stored) { |
||
| 147 | $stored->delete(); |
||
| 148 | $stored = null; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | // start building out the record |
||
| 153 | $cached = new CloudImageCached($cacheFile); |
||
| 154 | $cached->Title = $this->Title; |
||
| 155 | $cached->ParentID = $this->ParentID; |
||
| 156 | |||
| 157 | // Is there a meta record for this formatted file? |
||
| 158 | if ($stored) { |
||
| 159 | // Has it been successfully uploaded to the cloud? |
||
| 160 | // If so, we can just send this puppy on |
||
| 161 | // If not, is there a local file that's present and correct? |
||
| 162 | // If not, we need to wipe the meta and anything local and regenerate |
||
| 163 | if ($stored->CloudStatus !== 'Live' && $cached->isLocalMissing()) { |
||
| 164 | $stored->delete(); |
||
| 165 | $stored = null; |
||
| 166 | if (file_exists($cachePath)) { |
||
| 167 | unlink($cachePath); |
||
| 168 | } |
||
| 169 | } else { |
||
| 170 | $cached->setStoreRecord($stored); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | // If there is no meta record (or an invalid one), is there a local file or placeholder? |
||
| 175 | if (!$stored) { |
||
| 176 | // if the local exists as a placeholder, we need to check if the cloud version is valid |
||
| 177 | if (file_exists($cachePath) && $cached->containsPlaceholder()) { |
||
| 178 | try { |
||
| 179 | $cached->downloadFromCloud(); |
||
| 180 | } catch (Exception $e) { |
||
| 181 | // We want to fail silently here if there is any trouble |
||
| 182 | // because we can always regenerate the thumbnail |
||
| 183 | if (CloudAssets::config()->missing_image) { |
||
| 184 | return new CloudImageMissing($this, $args); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | // If we don't have a valid local version at this point... |
||
| 190 | if ($cached->isLocalMissing()) { |
||
| 191 | // delete whatever might have been there |
||
| 192 | if (file_exists($cachePath)) { |
||
| 193 | unlink($cachePath); |
||
| 194 | } |
||
| 195 | $logger->debug("CloudAssets: generating formatted image at $cachePath"); |
||
| 196 | |||
| 197 | // Regenerate the formatted image |
||
| 198 | if ($this->CloudStatus === 'Live' && $this->isLocalMissing()) { |
||
| 199 | try { |
||
| 200 | $this->downloadFromCloud(); |
||
| 201 | } catch (Exception $e) { |
||
| 202 | if (CloudAssets::config()->missing_image) { |
||
| 203 | return new CloudImageMissing($this, $args); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | call_user_func_array(array($this, "generateFormattedImage"), $args); |
||
| 208 | $this->convertToPlaceholder(); |
||
| 209 | } else { |
||
| 210 | call_user_func_array(array($this, "generateFormattedImage"), $args); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | // If we now have a valid image, generate a stored meta record for it |
||
| 215 | if (file_exists($cachePath)) { |
||
| 216 | $stored = new CloudImageCachedStore(); |
||
| 217 | $stored->Filename = $cacheFile; |
||
| 218 | $stored->SourceID = $this->ID; |
||
| 219 | $stored->write(); |
||
| 220 | // all the other fields will get set when the cloud status is updated |
||
| 221 | $cached->setStoreRecord($stored); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | // upload to cloud if needed |
||
| 226 | $cached->updateCloudStatus(); |
||
| 227 | |||
| 228 | return $cached; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 264 |