SitemapBehavior::generateSiteMap()   C
last analyzed

Complexity

Conditions 8
Paths 12

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 31
rs 5.3846
cc 8
eloc 16
nc 12
nop 0
1
<?php
2
/**
3
 * Behavior for XML Sitemap Yii2 module.
4
 *
5
 * @author Serge Larin <[email protected]>
6
 * @link https://github.com/assayer-pro/yii2-sitemap-module
7
 * @copyright 2015 Assayer Pro Company
8
 * @license http://opensource.org/licenses/MIT MIT
9
 *
10
 * based on https://github.com/himiklab/yii2-sitemap-module
11
 */
12
13
namespace assayerpro\sitemap\behaviors;
14
15
use yii\base\Behavior;
16
use yii\base\InvalidConfigException;
17
18
/**
19
 * Behavior for XML Sitemap Yii2 module.
20
 *
21
 * For example:
22
 *
23
 * ```php
24
 * public function behaviors()
25
 * {
26
 *  return [
27
 *       'sitemap' => [
28
 *           'class' => SitemapBehavior::className(),
29
 *           'scope' => function ($model) {
30
 *               $model->select(['url', 'lastmod']);
31
 *               $model->andWhere(['is_deleted' => 0]);
32
 *           },
33
 *           'dataClosure' => function ($model) {
34
 *              return [
35
 *                  'loc' => yii\helpers\Url::to($model->url, true),
36
 *                  'lastmod' => Sitemap::dateToW3C($model->lastmod),
37
 *                  'changefreq' => Sitemap::DAILY,
38
 *                  'priority' => 0.8
39
 *              ];
40
 *          }
41
 *       ],
42
 *  ];
43
 * }
44
 * ```
45
 *
46
 * @see http://www.sitemaps.org/protocol.html
47
 * @author Serge Larin <[email protected]>
48
 * @author HimikLab
49
 * @package assayerpro\sitemap
50
 */
51
class SitemapBehavior extends Behavior
52
{
53
    /** @var callable function generate url array for model */
54
    public $dataClosure;
55
56
    /** @var string|bool default time for model change frequency */
57
    public $defaultChangefreq = false;
58
59
    /** @var float|bool  default priority for model */
60
    public $defaultPriority = false;
61
62
    /** @var callable function for model filter */
63
    public $scope;
64
65
    /** @var int Query batch size */
66
    public $batchSize = 100;
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function init()
72
    {
73
        if (!is_callable($this->dataClosure)) {
74
            throw new InvalidConfigException('SitemapBehavior::$dataClosure isn\'t callable.');
75
        }
76
    }
77
78
    /**
79
     * generate sitemap structure for model
80
     *
81
     * @access public
82
     * @return array
83
     */
84
    public function generateSiteMap()
85
    {
86
        $result = [];
87
88
        /** @var \yii\db\ActiveRecord $owner */
89
        $owner = $this->owner;
90
        $query = $owner::find();
91
        if (is_callable($this->scope)) {
92
            call_user_func($this->scope, $query);
93
        }
94
95
        foreach ($query->each($this->batchSize) as $model) {
96
97
            $urlData = call_user_func($this->dataClosure, $model);
98
99
            if (empty($urlData)) {
100
                continue;
101
            }
102
103
            if (!isset($urlData['changefreq']) && ($this->defaultChangefreq !== false)) {
104
                $urlData['changefreq'] = $this->defaultChangefreq;
105
            }
106
107
            if (!isset($urlData['priority']) && ($this->defaultPriority !== false)) {
108
                $urlData['priority'] = $this->defaultPriority;
109
            }
110
111
            $result[] = $urlData;
112
        }
113
        return $result;
114
    }
115
}
116