CreateController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 50
rs 10
ccs 0
cts 21
cp 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A options() 0 4 1
A actionCreate() 0 18 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\console;
12
13
use Yii;
14
use yii\console\Controller;
15
use yii\helpers\Console;
16
17
18
/**
19
 * Generate sitemap for application
20
 *
21
 * @author Serge Larin <[email protected]>
22
 * @package assayerpro\sitemap
23
 */
24
class CreateController extends Controller
25
{
26
    /**
27
     * @inheritdoc
28
     */
29
    public $defaultAction = 'create';
30
31
    /**
32
     * @var string folder for sitemaps files
33
     */
34
    public $rootDir = '@webroot';
35
36
    /**
37
     * @var string sitemap main file name
38
     */
39
    public $sitemapFile = 'sitemap.xml';
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function options($actionID)
45
    {
46
        return array_merge(parent::options($actionID), ['rootDir', 'sitemapFile']);
47
    }
48
49
    /**
50
     * Generate sitemap.xml file
51
     *
52
     * @access public
53
     * @return integer
54
     */
55
    public function actionCreate()
56
    {
57
        $file = Yii::getAlias($this->rootDir.'/'.$this->sitemapFile);
58
        $this->stdout("Generate sitemap file.\n", Console::FG_GREEN);
59
        $this->stdout("Rendering sitemap...\n", Console::FG_GREEN);
60
        $sitemap = Yii::$app->sitemap->render();
61
62
        $this->stdout("Writing sitemap to $file\n", Console::FG_GREEN);
63
        file_put_contents($file, $sitemap[0]['xml']);
64
        $sitemap_count = count($sitemap);
65
        for ($i = 1; $i < $sitemap_count; $i++) {
66
            $file = Yii::getAlias($this->rootDir.'/'.trim($sitemap[$i]['file'], '/'));
67
            $this->stdout("Writing sitemap to $file\n", Console::FG_GREEN);
68
            file_put_contents($file, $sitemap[$i]['xml']);
69
        }
70
        $this->stdout("Done\n", Console::FG_GREEN);
71
        return self::EXIT_CODE_NORMAL;
72
    }
73
}
74