| Conditions | 12 |
| Paths | 504 |
| Total Lines | 70 |
| Code Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 8 | ||
| 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 |
||
| 64 | public function generatePageResponse($url) |
||
| 65 | { |
||
| 66 | if (Director::is_relative_url($url)) { |
||
| 67 | $url = Director::absoluteURL($url); |
||
| 68 | } |
||
| 69 | $urlParts = parse_url($url); |
||
| 70 | if (!empty($urlParts['query'])) { |
||
| 71 | parse_str($urlParts['query'], $getVars); |
||
| 72 | } else { |
||
| 73 | $getVars = []; |
||
| 74 | } |
||
| 75 | // back up requirements backend |
||
| 76 | $origRequirements = Requirements::backend(); |
||
| 77 | Requirements::set_backend(Requirements_Backend::create()); |
||
| 78 | |||
| 79 | $origThemes = SSViewer::get_themes(); |
||
| 80 | $staticThemes = self::config()->get('static_publisher_themes'); |
||
| 81 | if ($staticThemes) { |
||
| 82 | SSViewer::set_themes($staticThemes); |
||
| 83 | } else { |
||
| 84 | // get the themes raw from config to prevent the "running from the CMS" problem where no themes are live |
||
| 85 | $rawThemes = SSViewer::config()->uninherited('themes'); |
||
| 86 | SSViewer::set_themes($rawThemes); |
||
| 87 | } |
||
| 88 | try { |
||
| 89 | $ssl = Environment::getEnv('SS_STATIC_FORCE_SSL'); |
||
| 90 | if (is_null($ssl)) { |
||
| 91 | $ssl = $urlParts['scheme'] == 'https' ? true : false; |
||
| 92 | } |
||
| 93 | |||
| 94 | // try to add all the server vars that would be needed to create a static cache |
||
| 95 | $request = HTTPRequestBuilder::createFromVariables( |
||
| 96 | [ |
||
| 97 | '_SERVER' => [ |
||
| 98 | 'REQUEST_URI' => isset($urlParts['path']) ? $urlParts['path'] : '', |
||
| 99 | 'REQUEST_METHOD' => 'GET', |
||
| 100 | 'REMOTE_ADDR' => '127.0.0.1', |
||
| 101 | 'HTTPS' => $ssl ? 'on' : 'off', |
||
| 102 | 'QUERY_STRING' => isset($urlParts['query']) ? $urlParts['query'] : '', |
||
| 103 | 'REQUEST_TIME' => DBDatetime::now()->getTimestamp(), |
||
| 104 | 'REQUEST_TIME_FLOAT' => (float) DBDatetime::now()->getTimestamp(), |
||
| 105 | 'HTTP_HOST' => $urlParts['host'] . (isset($urlParts['port']) ? ':' . $urlParts['port'] : ''), |
||
| 106 | 'HTTP_USER_AGENT' => 'silverstripe/staticpublishqueue', |
||
| 107 | ], |
||
| 108 | '_GET' => $getVars, |
||
| 109 | '_POST' => [], |
||
| 110 | ], |
||
| 111 | '' |
||
| 112 | ); |
||
| 113 | $app = $this->getHTTPApplication(); |
||
| 114 | $response = $app->handle($request); |
||
| 115 | |||
| 116 | if ($this->config()->get('add_timestamp')) { |
||
| 117 | $response->setBody( |
||
| 118 | str_replace( |
||
| 119 | '</html>', |
||
| 120 | '<!-- ' . DBDateTime::now()->Full() . " -->\n</html>", |
||
| 121 | $response->getBody() |
||
| 122 | ) |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | } catch (HTTPResponse_Exception $e) { |
||
| 126 | $response = $e->getResponse(); |
||
| 127 | } finally { |
||
| 128 | // restore backends |
||
| 129 | SSViewer::set_themes($origThemes); |
||
| 130 | Requirements::set_backend($origRequirements); |
||
| 131 | DataObject::singleton()->flushCache(); |
||
| 132 | } |
||
| 133 | return $response; |
||
| 134 | } |
||
| 184 |