Passed
Push — master ( b534e3...17b30a )
by Mihail
19:51
created

Content::actionTag()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 30
Code Lines 14

Duplication

Lines 3
Ratio 10 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 3
nop 1
dl 3
loc 30
rs 8.5806
c 0
b 0
f 0
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 {
43
        listing as actionList;
44
    }
45
46
    use Content\ActionRead {
47
        read as actionRead;
48
    }
49
50
    use Content\ActionUpdate {
51
        update as actionUpdate;
52
    }
53
54
    use Content\ActionTag {
55
        tag as actionTag;
56
    }
57
58
    use Content\ActionRss {
59
        rss as actionRss;
60
    }
61
62
    use Content\ActionMy {
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)) {
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
        }
123
    }
124
}
125