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

WebController::actionIndex()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 3
eloc 11
nc 3
nop 1
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
        Yii::$app->response->format = Response::FORMAT_XML;
59
        $result = $sitemap[$id]['xml'];
60
        if ($this->module->enableGzip) {
61
            $result = gzencode($result);
62
            $headers->add('Content-Encoding', 'gzip');
0 ignored issues
show
Bug introduced by
The variable $headers does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
63
            $headers->add('Content-Length', strlen($result));
64
        }
65
        return $result;
66
    }
67
}
68