Passed
Push — master ( c57047...e9f946 )
by Mihail
06:06
created

ScheduledActions::sitemap()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 55
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 32
nc 7
nop 0
dl 0
loc 55
rs 8.0555
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace Apps\Controller\Front\Content;
4
5
use Apps\ActiveRecord\Content as ContentRecord;
6
use Apps\ActiveRecord\ContentCategory;
7
use Apps\Model\Front\Sitemap\EntityBuildMap;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Helper\FileSystem\File;
10
use Ffcms\Core\Helper\Type\Str;
11
12
13
/**
14
 * Trait ScheduledActions
15
 * @package Apps\Controller\Front\Content
16
 */
17
trait ScheduledActions
18
{
19
    public static $updateSitemapDelay = 120; // sitemap update delay in minutes, 120 = 2h
20
    public static $contentRowsEachRun = 5000; // content count per each one iteration
21
22
    /** Cron schedule action - build content sitemap
23
     * @throws \Ffcms\Core\Exception\SyntaxException
24
     * @return void
25
     */
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
        }
82
    }
83
}