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

SitemapController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 32
c 0
b 0
f 0
ccs 0
cts 20
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A actionCreate() 0 8 1
A writeItems() 0 13 3
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