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

EntityIndexList   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 67
rs 10
wmc 10
lcom 1
cbo 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B before() 0 16 5
A make() 0 14 3
A getInfo() 0 4 1
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
}