Conditions | 18 |
Paths | 58 |
Total Lines | 67 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
21 | public function onBeforeInit() |
||
22 | { |
||
23 | //make sure that caching is always https |
||
24 | $owner = $this->getOwner(); |
||
25 | if (Director::isLive()) { |
||
26 | $owner->response->addHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains'); |
||
27 | } |
||
28 | if (Security::getCurrentUser()) { |
||
29 | return null; |
||
30 | } |
||
31 | if (Versioned::get_reading_mode() !== 'Stage.Live') { |
||
32 | return null; |
||
33 | } |
||
34 | |||
35 | $sc = SiteConfig::current_site_config(); |
||
36 | if (! $sc->HasCaching) { |
||
37 | return null; |
||
38 | } |
||
39 | /** PageController|ControllerExtension $owner */ |
||
40 | if ($owner instanceof ContentController) { |
||
41 | $dataRecord = $owner->data(); |
||
42 | if (empty($dataRecord)) { |
||
43 | return null; |
||
44 | } |
||
45 | if ($dataRecord->NeverCachePublicly) { |
||
46 | HTTPCacheControlMiddleware::singleton() |
||
47 | ->disableCache() |
||
48 | ; |
||
49 | return null; |
||
50 | } |
||
51 | |||
52 | if ($owner->hasMethod('updateCacheControl')) { |
||
53 | $extend = $owner->extend('updateCacheControl'); |
||
54 | if ($extend) { |
||
55 | return null; |
||
56 | } |
||
57 | } |
||
58 | |||
59 | $request = $owner->getRequest(); |
||
60 | if ($owner->hasMethod('cacheControlExcludedActions')) { |
||
61 | $excludeActions = $owner->cacheControlExcludedActions(); |
||
62 | if ($request->param('Action') && in_array($request->param('Action'), $excludeActions)) { |
||
63 | return null; |
||
64 | } |
||
65 | } |
||
66 | if ($request->isAjax()) { |
||
67 | return null; |
||
68 | } |
||
69 | if ($request->getVar('flush')) { |
||
70 | return null; |
||
71 | } |
||
72 | if ($request->requestVars()) { |
||
73 | return null; |
||
74 | } |
||
75 | |||
76 | |||
77 | $cacheTime = (int) ($dataRecord->PublicCacheDurationInSeconds ?: $sc->PublicCacheDurationInSeconds); |
||
78 | if ($cacheTime > 0) { |
||
79 | return HTTPCacheControlMiddleware::singleton() |
||
80 | ->enableCache() |
||
81 | ->setMaxAge($cacheTime) |
||
82 | ->publicCache(true) |
||
83 | ; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | return null; |
||
88 | } |
||
90 |