Completed
Pull Request — master (#8)
by
unknown
03:16
created

ConsoleController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A options() 0 4 1
B actionIndex() 0 24 2
1
<?php
2
/**
3
 * CreateController for sitemap module
4
 *
5
 * @link https://github.com/assayer-pro/yii2-sitemap-module
6
 * @author Serge Larin <[email protected]>
7
 * @copyright 2015 Assayer Pro Company
8
 * @license http://opensource.org/licenses/MIT MIT
9
 */
10
11
namespace assayerpro\sitemap\controllers;
12
13
use assayerpro\sitemap\Module;
14
use Yii;
15
use yii\console\Controller;
16
use yii\helpers\Console;
17
use yii\helpers\Url;
18
19
20
/**
21
 * Generate sitemap for application
22
 *
23
 * @author Serge Larin <[email protected]>
24
 * @package assayerpro\sitemap
25
 */
26
27
/**
28
 * Class ConsoleController
29
 *
30
 * @package assayerpro\sitemap\controllers
31
 * @property Module $module
32
 */
33
class ConsoleController extends Controller
34
{
35
    /**
36
     * @var string folder for sitemaps files
37
     */
38
    public $rootDir = '@webroot';
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function options($actionID)
44
    {
45
        return array_merge(parent::options($actionID), ['rootDir']);
46
    }
47
48
    /**
49
     * Generate sitemap.xml file
50
     *
51
     * @access public
52
     * @return integer
53
     */
54
    public function actionIndex()
55
    {
56
        $route = '/' . $this->module->id . '/web/index';
57
58
        $rootDir = Yii::getAlias('/' . trim($this->rootDir, '/'));
59
        $file = $rootDir . Url::to([$route], false);
60
61
        $this->stdout("Generate sitemap file.\n", Console::FG_GREEN);
62
        $this->stdout("Rendering sitemap...\n", Console::FG_GREEN);
63
        $generator = $this->module->generator;
64
        $sitemap = $generator->render();
65
66
        $this->stdout("Writing sitemap to $file\n", Console::FG_GREEN);
67
        file_put_contents($file, $sitemap[0]['xml']);
68
        $sitemapCount = count($sitemap);
69
70
        for ($i = 0; $i < $sitemapCount; $i++) {
71
            $file = $rootDir . Url::to([$route, 'id' => $i], false);
72
            $this->stdout("Writing sitemap to $file\n", Console::FG_GREEN);
73
            file_put_contents($file, $sitemap[$i + 1]['xml']);
74
        }
75
        $this->stdout("Done\n", Console::FG_GREEN);
76
        return self::EXIT_CODE_NORMAL;
77
    }
78
}
79