Conditions | 12 |
Paths | 40 |
Total Lines | 90 |
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 |
||
97 | public function doGet($request) |
||
98 | { |
||
99 | self::$logger->debug('>>doGet(request=['.var_export($request, true).'])'); |
||
100 | |||
101 | $config = ConfigProvider::getInstance(); |
||
102 | |||
103 | $params = $request->getParams(); |
||
104 | |||
105 | try { |
||
106 | $imgSource = urldecode($params['source']); |
||
107 | $imgWidth = $params['width']; |
||
108 | $imgHeight = $params['height']; |
||
109 | $imgType = $params['type']; |
||
110 | $imgQuality = (double)$params['quality']; |
||
111 | $imgScale = new Boolean($params['scale']); |
||
112 | $imgSecure = new Boolean($params['secure']); |
||
113 | } catch (\Exception $e) { |
||
114 | self::$logger->error('Required param missing for ImageController controller['.$e->getMessage().']'); |
||
115 | throw new ResourceNotFoundException('File not found'); |
||
116 | } |
||
117 | |||
118 | $modified = filemtime($imgSource); |
||
119 | |||
120 | $responseHeaders = array(); |
||
121 | |||
122 | $responseHeaders['Last-Modified'] = date('D, d M Y H:i:s', $modified).' GMT'; |
||
123 | $responseHeaders['Cache-Control'] = 'max-age=1800'; |
||
124 | |||
125 | // exit if not modified |
||
126 | if ($request->getHeader('If-Modified-Since') != null) { |
||
127 | if (strtotime($request->getHeader('If-Modified-Since')) == $modified) { |
||
128 | return new Response(304, '', $responseHeaders); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | // handle secure tokens |
||
133 | if ($imgSecure->getBooleanValue() && $config->get('cms.images.widget.secure')) { |
||
134 | $valid = $this->checkSecurityFields(); |
||
135 | |||
136 | // if not valid, just return a blank black image of the same dimensions |
||
137 | if (!$valid) { |
||
138 | $im = imagecreatetruecolor($imgWidth, $imgHeight); |
||
139 | $bgc = imagecolorallocate($im, 0, 0, 0); |
||
140 | imagefilledrectangle($im, 0, 0, $imgWidth, $imgHeight, $bgc); |
||
141 | |||
142 | if ($imgSource == 'png' && $config->get('cms.images.perserve.png')) { |
||
143 | ob_start(); |
||
144 | imagepng($im); |
||
145 | $body = ob_get_contents(); |
||
146 | $contentType = 'image/png'; |
||
147 | ob_end_clean(); |
||
148 | } else { |
||
149 | ob_start(); |
||
150 | imagejpeg($im); |
||
151 | $body = ob_get_contents(); |
||
152 | $contentType = 'image/jpeg'; |
||
153 | ob_end_clean(); |
||
154 | } |
||
155 | |||
156 | imagedestroy($im); |
||
157 | |||
158 | self::$logger->warn('The client ['.$request->getUserAgent().'] was blocked from accessing the file ['.$imgSource.'] due to bad security tokens being provided'); |
||
159 | |||
160 | $responseHeaders['Content-Type'] = $contentType; |
||
161 | |||
162 | return new Response(200, $body, $responseHeaders); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | try { |
||
167 | $image = new Image($imgSource, $imgWidth, $imgHeight, $imgType, $imgQuality, $imgScale->getBooleanValue(), $imgSecure->getBooleanValue()); |
||
168 | ob_start(); |
||
169 | $image->renderImage(); |
||
170 | $body = ob_get_contents(); |
||
171 | ob_end_clean(); |
||
172 | } catch (IllegalArguementException $e) { |
||
173 | self::$logger->error($e->getMessage()); |
||
174 | throw new ResourceNotFoundException('File not found'); |
||
175 | } |
||
176 | |||
177 | self::$logger->debug('<<__doGet'); |
||
178 | |||
179 | if ($imgSource == 'png' && $config->get('cms.images.perserve.png')) { |
||
180 | $responseHeaders['Content-Type'] = 'image/png'; |
||
181 | } else { |
||
182 | $responseHeaders['Content-Type'] = 'image/jpeg'; |
||
183 | } |
||
184 | |||
185 | return new Response(200, $body, $responseHeaders); |
||
186 | } |
||
187 | } |
||
188 |