| Conditions | 9 |
| Paths | 7 |
| Total Lines | 52 |
| Code Lines | 30 |
| 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 |
||
| 70 | public static function buildSitemapSchedule() |
||
| 71 | { |
||
| 72 | // get records from database as activerecord object |
||
| 73 | $contents = ContentRecord::where('display', '=', 1); |
||
| 74 | $contentCount = $contents->count(); |
||
| 75 | if ($contentCount < 1) { |
||
| 76 | return; |
||
| 77 | } |
||
| 78 | |||
| 79 | // get languages if multilanguage enabled |
||
| 80 | $langs = null; |
||
| 81 | if (App::$Properties->get('multiLanguage')) { |
||
| 82 | $langs = App::$Properties->get('languages'); |
||
| 83 | } |
||
| 84 | |||
| 85 | // build sitemap items using iteration - 5000 rows per each one |
||
| 86 | $iterations = (int)($contentCount / static::SITEMAP_CONTENT_COUNT_ITERATION); |
||
| 87 | for ($i = 0; $i <= $iterations; $i++) { |
||
| 88 | // check if lifetime is expired for current sitemap index |
||
| 89 | $xmlTime = File::mTime('/upload/sitemap/content.' . $i . '.' . $langs[0] . '.xml'); |
||
| 90 | $updateDelay = static::SITEMAP_UPDATE_DELAY * 60; |
||
| 91 | $updateDelay += mt_rand(0, 1800); // +- 0-30 rand min for caching update |
||
| 92 | // do not process if cache time is not expired |
||
| 93 | if (time() - $xmlTime <= $updateDelay) { |
||
| 94 | continue; |
||
| 95 | } |
||
| 96 | |||
| 97 | // get results with current offset |
||
| 98 | $offset = $i * static::SITEMAP_CONTENT_COUNT_ITERATION; |
||
| 99 | $result = $contents->take(static::SITEMAP_CONTENT_COUNT_ITERATION)->skip($offset)->get(); |
||
| 100 | |||
| 101 | // build sitemap from content items via business model |
||
| 102 | $sitemap = new EntityBuildMap($langs); |
||
| 103 | foreach ($result as $content) { |
||
| 104 | $category = $content->getCategory(); |
||
| 105 | $uri = '/content/read/'; |
||
| 106 | if (!Str::likeEmpty($category->path)) { |
||
| 107 | $uri .= $category->path . '/'; |
||
| 108 | } |
||
| 109 | $uri .= $content->path; |
||
| 110 | $sitemap->add($uri, $content->created_at, 'weekly', 0.7); |
||
| 111 | } |
||
| 112 | // add categories |
||
| 113 | $categories = ContentCategory::all(); |
||
| 114 | foreach ($categories as $item) { |
||
| 115 | if ((bool)$item->getProperty('showCategory')) { |
||
| 116 | $uri = '/content/list/' . $item->path; |
||
| 117 | $sitemap->add($uri, date('c'), 'daily', 0.9); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | // save data to xml file |
||
| 121 | $sitemap->save('content.' . $i); |
||
| 122 | } |
||
| 125 |