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

SitemapCategory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 113
rs 10
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getSitemapLastmod() 0 5 1
A getSitemapPriority() 0 3 1
A getSitemapLoc() 0 3 1
A getSitemapItemsQuery() 0 21 1
A getSitemapAlternateLinks() 0 9 2
A init() 0 5 1
A getSitemapItems() 0 3 1
A getSitemapChangefreq() 0 3 1
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\Category;
10
11
/**
12
 * Class SitemapCategory
13
 *
14
 * @package app\commands\models\sitemap
15
 */
16
class SitemapCategory extends Category 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
                'categoriesLanguages' => 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
            ])->andWhere([
75
                'NOT', ['alias' => null]
76
            ])->andWhere([
77
                'NOT', ['alias' => '']
78
            ])->orderBy([
79
                'id' => SORT_DESC
80
            ]);
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86
    public function getSitemapLoc($lang = null)
87
    {
88
        return Url::to('/' . $lang . '/category/' . $this->alias, true);
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94
    public function getSitemapLastmod($lang = null)
95
    {
96
        $inDateTime = new \DateTime($this->updated_at);
97
98
        return $inDateTime->getTimestamp();
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104
    public function getSitemapChangefreq($lang = null)
105
    {
106
        return static::CHANGEFREQ_MONTHLY;
107
    }
108
109
    /**
110
     * @inheritdoc
111
     */
112
    public function getSitemapPriority($lang = null)
113
    {
114
        return static::PRIORITY_8;
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120
    public function getSitemapAlternateLinks()
121
    {
122
        $buffer = [];
123
124
        foreach ($this->sitemapLanguages as $langCode) {
125
            $buffer[$langCode] = $this->getSitemapLoc($langCode);
126
        }
127
128
        return $buffer;
129
    }
130
}
131