Passed
Push — master ( a29a7e...12a432 )
by Mihail
08:06
created

Content   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 96
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C buildSitemapSchedule() 0 52 9
1
<?php
2
3
namespace Apps\Controller\Front;
4
5
use Apps\ActiveRecord\Content as ContentEntity;
6
use Apps\ActiveRecord\Content as ContentRecord;
7
use Apps\ActiveRecord\ContentCategory;
8
use Apps\Model\Front\Content\EntityCategoryList;
9
use Apps\Model\Front\Content\FormNarrowContentUpdate;
10
use Apps\Model\Front\Sitemap\EntityBuildMap;
11
use Extend\Core\Arch\FrontAppController;
12
use Ffcms\Core\App;
13
use Ffcms\Core\Exception\ForbiddenException;
14
use Ffcms\Core\Exception\NotFoundException;
15
use Ffcms\Core\Helper\FileSystem\File;
16
use Ffcms\Core\Helper\HTML\SimplePagination;
17
use Ffcms\Core\Helper\Type\Str;
18
use Suin\RSSWriter\Channel;
19
use Suin\RSSWriter\Feed;
20
use Suin\RSSWriter\Item;
21
22
/**
23
 * Class Content. Controller of content app - content and categories.
24
 * @package Apps\Controller\Front
25
 */
26
class Content extends FrontAppController
27
{
28
    const TAG_PER_PAGE = 50;
29
    const SITEMAP_UPDATE_DELAY = 120; // sitemap update delay in minutes, 120 = 2h
30
    const SITEMAP_CONTENT_COUNT_ITERATION = 5000; // content count per each one iteration
31
32
    const EVENT_CONTENT_READ = 'content.read';
33
    const EVENT_RSS_READ = 'content.rss.read';
34
    const EVENT_CONTENT_LIST = 'content.list';
35
    const EVENT_TAG_LIST = 'content.tags';
36
37
    /**
38
     * Fatty action like actionList(), actionRead() are located in standalone traits.
39
     * This feature allow provide better read&write accessibility
40
     */
41
42
    use Content\ActionList {
0 ignored issues
show
Bug introduced by
The trait Apps\Controller\Front\Content\ActionList requires the property $query which is not provided by Apps\Controller\Front\Content.
Loading history...
43
        listing as actionList;
44
    }
45
46
    use Content\ActionRead {
0 ignored issues
show
introduced by
The trait Apps\Controller\Front\Content\ActionRead requires some properties which are not provided by Apps\Controller\Front\Content: $role, $id, $title
Loading history...
47
        read as actionRead;
48
    }
49
50
    use Content\ActionUpdate {
0 ignored issues
show
introduced by
The trait Apps\Controller\Front\Content\ActionUpdate requires some properties which are not provided by Apps\Controller\Front\Content: $id, $author_id, $display
Loading history...
51
        update as actionUpdate;
52
    }
53
54
    use Content\ActionTag {
55
        tag as actionTag;
56
    }
57
58
    use Content\ActionRss {
0 ignored issues
show
introduced by
The trait Apps\Controller\Front\Content\ActionRss requires some properties which are not provided by Apps\Controller\Front\Content: $items, $scriptUrl, $baseUrl, $category, $headers
Loading history...
59
        rss as actionRss;
60
    }
61
62
    use Content\ActionMy {
0 ignored issues
show
Bug introduced by
The trait Apps\Controller\Front\Content\ActionMy requires the property $query which is not provided by Apps\Controller\Front\Content.
Loading history...
63
        my as actionMy;
64
    }
65
66
    /**
67
     * Cron schedule action - build content sitemap
68
     * @throws \Ffcms\Core\Exception\SyntaxException
69
     */
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)) {
0 ignored issues
show
Bug introduced by
The property path does not seem to exist on Illuminate\Database\Eloquent\Builder.
Loading history...
107
                    $uri .= $category->path . '/';
108
                }
109
                $uri .= $content->path;
0 ignored issues
show
Bug introduced by
The property path does not seem to exist on Ffcms\Core\Arch\ActiveModel. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
110
                $sitemap->add($uri, $content->created_at, 'weekly', 0.7);
0 ignored issues
show
Bug introduced by
$content->created_at of type DateTime is incompatible with the type integer|string expected by parameter $lastmod of Apps\Model\Front\Sitemap\EntityBuildMap::add(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
                $sitemap->add($uri, /** @scrutinizer ignore-type */ $content->created_at, 'weekly', 0.7);
Loading history...
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
        }
123
    }
124
}
125