Conditions | 8 |
Paths | 80 |
Total Lines | 55 |
Code Lines | 40 |
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 |
||
48 | public function generatePageResponse($url) |
||
49 | { |
||
50 | if (Director::is_relative_url($url)) { |
||
51 | $url = Director::absoluteURL($url); |
||
52 | } |
||
53 | $urlParts = parse_url($url); |
||
54 | if (!empty($urlParts['query'])) { |
||
55 | parse_str($urlParts['query'], $getVars); |
||
56 | } else { |
||
57 | $getVars = []; |
||
58 | } |
||
59 | // back up requirements backend |
||
60 | $origRequirements = Requirements::backend(); |
||
61 | Requirements::set_backend(Requirements_Backend::create()); |
||
62 | |||
63 | $origThemes = SSViewer::get_themes(); |
||
64 | $staticThemes = self::config()->get('static_publisher_themes'); |
||
65 | if ($staticThemes) { |
||
66 | SSViewer::set_themes($staticThemes); |
||
67 | } else { |
||
68 | // get the themes raw from config to prevent the "running from the CMS" problem where no themes are live |
||
69 | $rawThemes = SSViewer::config()->uninherited('themes'); |
||
70 | SSViewer::set_themes($rawThemes); |
||
71 | } |
||
72 | try { |
||
73 | // try to add all the server vars that would be needed to create a static cache |
||
74 | $request = HTTPRequestBuilder::createFromVariables( |
||
75 | [ |
||
76 | '_SERVER' => [ |
||
77 | 'REQUEST_URI' => isset($urlParts['path']) ? $urlParts['path'] : '', |
||
78 | 'REQUEST_METHOD' => 'GET', |
||
79 | 'REMOTE_ADDR' => '127.0.0.1', |
||
80 | 'HTTPS' => $urlParts['scheme'] == 'https' ? 'on' : 'off', |
||
81 | 'QUERY_STRING' => isset($urlParts['query']) ? $urlParts['query'] : '', |
||
82 | 'REQUEST_TIME' => DBDatetime::now()->getTimestamp(), |
||
83 | 'REQUEST_TIME_FLOAT' => (float) DBDatetime::now()->getTimestamp(), |
||
84 | 'HTTP_HOST' => $urlParts['host'], |
||
85 | 'HTTP_USER_AGENT' => 'silverstripe/staticpublishqueue', |
||
86 | ], |
||
87 | '_GET' => $getVars, |
||
88 | '_POST' => [], |
||
89 | ], |
||
90 | '' |
||
91 | ); |
||
92 | $app = $this->getHTTPApplication(); |
||
93 | $response = $app->handle($request); |
||
94 | } catch (HTTPResponse_Exception $e) { |
||
95 | $response = $e->getResponse(); |
||
96 | } finally { |
||
97 | // restore backends |
||
98 | SSViewer::set_themes($origThemes); |
||
99 | Requirements::set_backend($origRequirements); |
||
100 | DataObject::singleton()->flushCache(); |
||
101 | } |
||
102 | return $response; |
||
103 | } |
||
173 |