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