Passed
Push — master ( ef4849...3d6fe1 )
by Mihail
05:23
created

EntityIndexList::make()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 14
rs 9.4285
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\Obj;
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 bool $currentLang
30
     */
31
    public function __construct($currentLang)
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 (Obj::isArray($scan)) {
49
            foreach ($scan as $file) {
50
                if (!Str::contains('.' . $this->_lang, $file)) {
51
                    continue;
52
                }
53
                $this->files[] = static::INDEX_PATH . '/' . $file;
54
            }
55
        }
56
    }
57
58
    /**
59
     * Build sitemap index files information - location, last modify time
60
     */
61
    public function make()
62
    {
63
        if (!Obj::isArray($this->files)) {
64
            return;
65
        }
66
67
        // build file information data
68
        foreach ($this->files as $file) {
69
            $this->info[] = [
70
                'loc' => App::$Alias->scriptUrl . $file,
71
                'lastmod' => Date::convertToDatetime(File::mTime($file), 'c')
72
            ];
73
        }
74
    }
75
76
    /**
77
     * Get sitemap index files info as array
78
     * @return array|null
79
     */
80
    public function getInfo()
81
    {
82
        return $this->info;
83
    }
84
}