| Conditions | 9 |
| Paths | 7 |
| Total Lines | 55 |
| Code Lines | 32 |
| 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 |
||
| 26 | public static function sitemap(): void |
||
| 27 | { |
||
| 28 | // get records from database as activerecord object |
||
| 29 | $contents = ContentRecord::with('category') |
||
| 30 | ->where('display', '=', 1); |
||
| 31 | $contentCount = $contents->count(); |
||
| 32 | if ($contentCount < 1) { |
||
| 33 | return; |
||
| 34 | } |
||
| 35 | |||
| 36 | // get languages if multilanguage enabled |
||
| 37 | $langs = null; |
||
| 38 | if (App::$Properties->get('multiLanguage')) { |
||
| 39 | $langs = App::$Properties->get('languages'); |
||
| 40 | } |
||
| 41 | |||
| 42 | // build sitemap items using iteration - 5000 rows per each one |
||
| 43 | $iterations = (int)($contentCount / self::$contentRowsEachRun); |
||
| 44 | for ($i = 0; $i <= $iterations; $i++) { |
||
| 45 | // check if lifetime is expired for current sitemap index |
||
| 46 | $xmlTime = File::mTime('/upload/sitemap/content.' . $i . '.' . $langs[0] . '.xml'); |
||
| 47 | $updateDelay = self::$updateSitemapDelay * 60; |
||
| 48 | $updateDelay += mt_rand(0, 1800); // +- 0-30 rand min for caching update |
||
| 49 | // do not process if cache time is not expired |
||
| 50 | if (time() - $xmlTime <= $updateDelay) { |
||
| 51 | continue; |
||
| 52 | } |
||
| 53 | |||
| 54 | // get results with current offset |
||
| 55 | $offset = $i * self::$contentRowsEachRun; |
||
| 56 | $result = $contents->take(self::$contentRowsEachRun) |
||
| 57 | ->skip($offset) |
||
| 58 | ->get(); |
||
| 59 | |||
| 60 | // build sitemap from content items via business model |
||
| 61 | $sitemap = new EntityBuildMap($langs); |
||
| 62 | foreach ($result as $content) { |
||
| 63 | /** @var \Apps\ActiveRecord\Content $content */ |
||
| 64 | $uri = '/content/read/'; |
||
| 65 | if (!Str::likeEmpty($content->category->path)) { |
||
| 66 | $uri .= $content->category->path . '/'; |
||
| 67 | } |
||
| 68 | $uri .= $content->path; |
||
| 69 | $sitemap->add($uri, $content->created_at, 'weekly', 0.7); |
||
| 70 | } |
||
| 71 | // add categories |
||
| 72 | $categories = ContentCategory::all(); |
||
| 73 | foreach ($categories as $item) { |
||
| 74 | if ((bool)$item->getProperty('showCategory')) { |
||
| 75 | $uri = '/content/list/' . $item->path; |
||
| 76 | $sitemap->add($uri, date('c'), 'daily', 0.9); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | // save data to xml file |
||
| 80 | $sitemap->save('content.' . $i); |
||
| 81 | } |
||
| 83 | } |