Completed
Push — master ( d0bd4d...b4b0fd )
by Igor
23:04
created

SitemapController::actionCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace app\commands;
4
5
use yii\console\Controller;
6
use yii2tech\sitemap\File;
7
use yii\helpers\Console;
8
9
/**
10
 * Command for create sitemap
11
 */
12
class SitemapController extends Controller
13
{
14
    private $sitemap;
15
16
    public function init()
17
    {
18
        $this->sitemap = new File();
19
    }
20
21
    public function actionCreate(): void
22
    {
23
        // writeItems…
24
25
        $this->sitemap->close();
26
27
        $this->stdout("Done!\n", Console::FG_GREEN);
28
    }
29
30
    private function writeItems(string $modelClass, $cb)
31
    {
32
        $items = $modelClass::find()->active()->asArray()->all();
33
        foreach ($items as $item) {
34
            $options = [];
35
36
            if (isset($item['date_update'])) {
37
                $options['lastModified'] = substr($item['date_update'], 0, 10);
38
            }
39
40
            $this->sitemap->writeUrl($cb($item), $options);
41
        }
42
    }
43
}
44