Passed
Push — master ( 59abca...fa3542 )
by Mihail
05:32
created

Apps/Model/Front/Sitemap/EntityBuildMap.php (1 issue)

Labels
Severity
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\Helper\Date;
8
use Ffcms\Core\Helper\FileSystem\File;
9
use Ffcms\Core\Helper\Type\Any;
10
use Ffcms\Core\Helper\Type\Obj;
11
use Ffcms\Core\Helper\Url;
0 ignored issues
show
The type Ffcms\Core\Helper\Url was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
/**
14
 * Class EntityBuildMap. Build sitemap data and save to file in xml format
15
 * @package Apps\Model\Front\Sitemap
16
 */
17
class EntityBuildMap extends Model
18
{
19
    private $data;
20
    private $langs;
21
22
    /**
23
     * EntityBuildMap constructor. Pass available languages and data as array inside.
24
     * @param array|null $langs
25
     * @param array|null $data
26
     */
27
    public function __construct(array $langs = null, array $data = null)
28
    {
29
        $this->langs = $langs;
30
31
        if ($data !== null && count($data) > 0) {
32
            foreach ($data as $item) {
33
                if (!Any::isArray($item) || !isset($item['uri'], $item['lastmod'])) {
34
                    continue;
35
                }
36
37
                $this->add($item['uri'], $item['lastmod'], $item['freq'], $item['priority']);
38
            }
39
        }
40
        parent::__construct();
41
    }
42
43
    /**
44
     * Add uri/url item to sitemap builder
45
     * @param string $uri
46
     * @param int|string $lastmod
47
     * @param string $freq
48
     * @param float $priority
49
     */
50
    public function add($uri, $lastmod, $freq = 'weekly', $priority = 0.5)
51
    {
52
        // generate multi-language files
53
        if ($this->langs !== null && Any::isArray($this->langs) && count($this->langs) > 0) {
54
            foreach ($this->langs as $lang) {
55
                // set data to local attribute
56
                $this->data[$lang][] = [
57
                    'uri' => Url::standaloneUrl($uri, $lang),
58
                    'lastmod' => Date::convertToDatetime($lastmod, 'c'),
59
                    'freq' => (string)$freq,
60
                    'priority' => (float)$priority
61
                ];
62
            }
63
        } else { // only one language, multilanguage is disabled
64
            $this->data[App::$Properties->get('singleLanguage')][] = [
65
                'uri' => Url::standaloneUrl($uri),
66
                'lastmod' => Date::convertToDatetime($lastmod, 'c'),
67
                'freq' => (string)$freq,
68
                'priority' => (float)$priority
69
            ];
70
        }
71
    }
72
73
    /**
74
     * Build xml output and save it into sitemap folder
75
     * @param string $uniqueName
76
     * @return bool
77
     * @throws \Ffcms\Core\Exception\SyntaxException
78
     */
79
    public function save($uniqueName)
80
    {
81
        // check if data exists
82
        if ($this->data === null || !Any::isArray($this->data)) {
83
            return false;
84
        }
85
86
        // list data each every language, render xml output and write into file
87
        foreach ($this->data as $lang => $items) {
88
            $xml = App::$View->render('native/sitemap_urlset', [
89
                'items' => $items
90
            ]);
91
            
92
            File::write(EntityIndexList::INDEX_PATH . '/' . $uniqueName . '.' . $lang . '.xml', $xml);
93
        }
94
        return true;
95
    }
96
}
97