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

EntityBuildMap::__construct()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 8
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 14
rs 8.8571
1
<?php
2
3
namespace Apps\Model\Front\Sitemap;
4
5
6
use Ffcms\Core\App;
7
use Ffcms\Core\Arch\Model;
8
use Ffcms\Core\Helper\Date;
9
use Ffcms\Core\Helper\FileSystem\File;
10
use Ffcms\Core\Helper\Type\Obj;
11
use Ffcms\Core\Helper\Type\Str;
12
use Ffcms\Core\Helper\Url;
13
14
class EntityBuildMap extends Model
15
{
16
    private $data;
17
    private $langs;
18
19
    /**
20
     * EntityBuildMap constructor. Pass available languages and data as array inside.
21
     * @param array|null $langs
22
     * @param array|null $data
23
     */
24
    public function __construct(array $langs = null, array $data = null)
25
    {
26
        $this->langs = $langs;
27
28
        if ($data !== null && count($data) > 0) {
29
            foreach ($data as $item) {
30
                if (!Obj::isArray($item) || !isset($item['uri'], $item['lastmod'])) {
31
                    continue;
32
                }
33
                $this->add($item['uri'], $item['lastmod'], $item['freq'], $item['priority']);
34
            }
35
        }
36
        parent::__construct();
37
    }
38
39
    /**
40
     * Add uri/url item to sitemap builder
41
     * @param string $uri
42
     * @param int|string $lastmod
43
     * @param string $freq
44
     * @param float $priority
45
     */
46
    public function add($uri, $lastmod, $freq = 'weekly', $priority = 0.5)
47
    {
48
        // generate multi-language files
49
        if ($this->langs !== null && Obj::isArray($this->langs) && count($this->langs) > 0) {
50
            foreach ($this->langs as $lang) {
51
                // set data to local attribute
52
                $this->data[$lang][] = [
53
                    'uri' => Url::standaloneUrl($uri, $lang),
0 ignored issues
show
Bug introduced by
The method standaloneUrl() does not seem to exist on object<Ffcms\Core\Helper\Url>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
                    'lastmod' => Date::convertToDatetime($lastmod, 'c'),
55
                    'freq' => (string)$freq,
56
                    'priority' => (float)$priority
57
                ];
58
            }
59
        } else { // only one language, multilanguage is disabled
60
            $this->data[App::$Properties->get('singleLanguage')][] = [
61
                'uri' => Url::standaloneUrl($uri),
0 ignored issues
show
Bug introduced by
The method standaloneUrl() does not seem to exist on object<Ffcms\Core\Helper\Url>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
                'lastmod' => Date::convertToDatetime($lastmod, 'c'),
63
                'freq' => (string)$freq,
64
                'priority' => (float)$priority
65
            ];
66
        }
67
    }
68
69
    /**
70
     * Build xml output and save it into sitemap folder
71
     * @param string $uniqueName
72
     * @return bool
73
     * @throws \Ffcms\Core\Exception\NativeException
74
     * @throws \Ffcms\Core\Exception\SyntaxException
75
     */
76
    public function save($uniqueName)
77
    {
78
        // check if data exists
79
        if ($this->data === null || !Obj::isArray($this->data)) {
80
            return false;
81
        }
82
83
        // list data each every language, render xml output and write into file
84
        foreach ($this->data as $lang => $items) {
85
            $xml = App::$View->render('native/sitemap_urlset', [
86
                'items' => $items
87
            ]);
88
            
89
            File::write(EntityIndexList::INDEX_PATH . '/' . $uniqueName . '.' . $lang . '.xml', $xml);
90
        }
91
        return true;
92
    }
93
94
}