EntityIndexList   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 22
dl 0
loc 66
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 11 3
A before() 0 14 6
A __construct() 0 4 1
A getInfo() 0 3 2
1
<?php
2
3
namespace Apps\Model\Front\Sitemap;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Arch\Model;
7
use Ffcms\Core\Exception\SyntaxException;
8
use Ffcms\Core\Helper\Date;
9
use Ffcms\Core\Helper\FileSystem\Directory;
10
use Ffcms\Core\Helper\FileSystem\File;
11
use Ffcms\Core\Helper\Type\Any;
12
use Ffcms\Core\Helper\Type\Str;
13
14
/**
15
 * Class EntityIndexList. List sitemap files in special directory
16
 * @package Apps\Model\Front\Sitemap
17
 */
18
class EntityIndexList extends Model
19
{
20
    const INDEX_PATH = '/upload/sitemap';
21
22
    public $files;
23
    private $info;
24
25
    private $_lang;
26
27
    /**
28
     * EntityIndexList constructor. Pass current language from controller request
29
     * @param string|null $currentLang
30
     */
31
    public function __construct($currentLang = null)
32
    {
33
        $this->_lang = $currentLang;
34
        parent::__construct();
35
    }
36
37
    /**
38
     * Try to find sitemap indexes in storage directory
39
     * @throws SyntaxException
40
     */
41
    public function before()
42
    {
43
        if (!Directory::exist(static::INDEX_PATH)) {
44
            throw new SyntaxException(__('Directory %dir% for sitemaps is not exists', ['dir' => static::INDEX_PATH]));
45
        }
46
47
        $scan = File::listFiles(static::INDEX_PATH, ['.xml'], true);
48
        if (Any::isArray($scan)) {
49
            foreach ($scan as $file) {
50
                if ($this->_lang !== null && !Str::contains('.' . $this->_lang, $file)) {
51
                    continue;
52
                }
53
54
                $this->files[] = static::INDEX_PATH . '/' . $file;
55
            }
56
        }
57
    }
58
59
    /**
60
     * Build sitemap index files information - location, last modify time
61
     */
62
    public function make()
63
    {
64
        if (!Any::isArray($this->files)) {
65
            return;
66
        }
67
68
        // build file information data
69
        foreach ($this->files as $file) {
70
            $this->info[] = [
71
                'loc' => App::$Alias->scriptUrl . $file,
72
                'lastmod' => Date::convertToDatetime(File::mTime($file), 'c')
73
            ];
74
        }
75
    }
76
77
    /**
78
     * Get sitemap index files info as array
79
     * @return array
80
     */
81
    public function getInfo()
82
    {
83
        return (Any::isArray($this->info) ? $this->info : []);
84
    }
85
}
86