Passed
Push — master ( dd4450...9a9614 )
by Andrey
05:55
created

SitemapArticle::getSitemapItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace app\models\sitemap;
4
5
use yii\helpers\Url;
6
use Itstructure\Sitemap\interfaces\Basic;
7
use Itstructure\Sitemap\interfaces\GoogleAlternateLang;
8
use Itstructure\AdminModule\models\Language;
9
use app\models\Article;
10
11
/**
12
 * Class SitemapArticle
13
 *
14
 * @package app\commands\models\sitemap
15
 */
16
class SitemapArticle extends Article implements Basic, GoogleAlternateLang
17
{
18
    /**
19
     * Handle materials by selecting batch of elements.
20
     * Increase this value and got more handling speed but more memory usage.
21
     *
22
     * @var int
23
     */
24
    public $sitemapBatchSize = 10;
25
    /**
26
     * List of available site languages
27
     *
28
     * @var array [langId => langCode]
29
     */
30
    public $sitemapLanguages = [];
31
    /**
32
     * If TRUE - Yii::$app->language will be switched for each sitemapLanguages and restored after.
33
     *
34
     * @var bool
35
     */
36
    public $sitemapSwithLanguages = true;
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function init()
42
    {
43
        $this->sitemapLanguages = Language::getShortLanguageList();
44
45
        parent::init();
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function getSitemapItems($lang = null)
52
    {
53
        return null;
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function getSitemapItemsQuery($lang = null)
60
    {
61
        return static::find()
62
            ->with([
63
                'articlesLanguages' => function ($query) use ($lang) {
64
                    /** @var \yii\db\Query $query */
65
                    $query->andWhere([
66
                        'language_id' => Language::findOne([
67
                            'shortName' => $lang
68
                        ])->id
69
                    ]);
70
                }
71
            ])
72
            ->where([
73
                'active' => 1
74
            ])
75
            ->orderBy([
76
                'id' => SORT_DESC
77
            ]);
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function getSitemapLoc($lang = null)
84
    {
85
        return Url::to('/' . $lang . '/article/' . $this->alias, true);
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91
    public function getSitemapLastmod($lang = null)
92
    {
93
        $inDateTime = new \DateTime($this->updated_at);
94
95
        return $inDateTime->getTimestamp();
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101
    public function getSitemapChangefreq($lang = null)
102
    {
103
        return static::CHANGEFREQ_MONTHLY;
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109
    public function getSitemapPriority($lang = null)
110
    {
111
        return static::PRIORITY_8;
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117
    public function getSitemapAlternateLinks()
118
    {
119
        $buffer = [];
120
121
        foreach ($this->sitemapLanguages as $langCode) {
122
            $buffer[$langCode] = $this->getSitemapLoc($langCode);
123
        }
124
125
        return $buffer;
126
    }
127
}
128