Completed
Pull Request — master (#8)
by
unknown
07:09
created

WebController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 43
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 11 1
A actionIndex() 0 19 3
1
<?php
2
/**
3
 * DefaultController for sitemap module
4
 *
5
 * @link https://github.com/himiklab/yii2-sitemap-module
6
 * @author Serge Larin <[email protected]>
7
 * @author HimikLab
8
 * @copyright 2015 Assayer Pro Company
9
 * @copyright Copyright (c) 2014 HimikLab
10
 * @license http://opensource.org/licenses/MIT MIT
11
 */
12
13
namespace assayerpro\sitemap\controllers;
14
15
use Yii;
16
use yii\web\Controller;
17
use yii\web\NotFoundHttpException;
18
use yii\web\Response;
19
use assayerpro\sitemap\RobotsTxt;
20
21
/**
22
 * DefaultController for sitemap module
23
 *
24
 * @author Serge Larin <[email protected]>
25
 * @author HimikLab
26
 * @package assayerpro\sitemap
27
 */
28
class WebController extends Controller
29
{
30
    /**
31
     * @inheritdoc
32
     */
33
    public function behaviors()
34
    {
35
        return [
36
            'pageCache' => [
37
                'class' => 'yii\filters\PageCache',
38
                'only' => ['index'],
39
                'duration' => $this->module->generator->cacheExpire,
40
                'variations' => [Yii::$app->request->get('id')],
41
            ],
42
        ];
43
    }
44
45
    /**
46
     * Action for sitemap/default/index
47
     *
48
     * @access public
49
     * @return string
50
     */
51
    public function actionIndex($id = 0)
52
    {
53
        $sitemap = $this->module->generator->render();
54
        if (empty($sitemap[$id])) {
55
            throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
56
        }
57
58
        $response = Yii::$app->response;
59
        $response->format = Response::FORMAT_XML;
60
        $result = $sitemap[$id]['xml'];
61
        if ($this->module->enableGzip) {
62
            $result = gzencode($result);
63
            $headers = $response->headers;
64
            $headers->add('Content-Encoding', 'gzip');
65
            $headers->add('Content-Length', strlen($result));
66
        }
67
68
        echo $result;
69
    }
70
}
71