SitemapGenerationModel.php 3.68 KB
Newer Older
1
<?php
Tino Goratsch's avatar
Tino Goratsch committed
2

3
/**
4
 * Copyright (c) by the ACP3 Developers.
Tino Goratsch's avatar
Tino Goratsch committed
5
 * See the LICENSE file at the top-level module directory for licensing details.
6 7 8 9 10
 */

namespace ACP3\Modules\ACP3\Seo\Model;

use ACP3\Core\Environment\ApplicationPath;
11
use ACP3\Core\Settings\SettingsInterface;
12
use ACP3\Modules\ACP3\Seo\Exception\SitemapGenerationException;
13
use ACP3\Modules\ACP3\Seo\Installer\Schema;
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
use ACP3\Modules\ACP3\Seo\Utility\SitemapAvailabilityRegistrar;
use Thepixeldeveloper\Sitemap\Output;
use Thepixeldeveloper\Sitemap\Urlset;

class SitemapGenerationModel
{
    /**
     * @var ApplicationPath
     */
    private $applicationPath;
    /**
     * @var SitemapAvailabilityRegistrar
     */
    protected $sitemapRegistrar;
    /**
29
     * @var SettingsInterface
30
     */
31 32 33 34 35 36 37 38 39 40 41
    private $settings;
    /**
     * @var array
     */
    private $filenameMap = [
        0 => [
            ['filename' => 'sitemap.xml', 'secure' => null],
        ],
        1 => [
            ['filename' => 'sitemap_https.xml', 'secure' => true],
            ['filename' => 'sitemap_http.xml', 'secure' => false],
Tino Goratsch's avatar
Tino Goratsch committed
42
        ],
43
    ];
44 45 46

    /**
     * SitemapGenerationModel constructor.
47 48 49
     *
     * @param ApplicationPath              $applicationPath
     * @param SettingsInterface            $settings
50 51 52 53
     * @param SitemapAvailabilityRegistrar $sitemapRegistrar
     */
    public function __construct(
        ApplicationPath $applicationPath,
54
        SettingsInterface $settings,
55 56 57 58
        SitemapAvailabilityRegistrar $sitemapRegistrar
    ) {
        $this->applicationPath = $applicationPath;
        $this->sitemapRegistrar = $sitemapRegistrar;
59
        $this->settings = $settings;
60 61 62 63
    }

    /**
     * @return bool
64 65
     *
     * @throws \ACP3\Modules\ACP3\Seo\Exception\SitemapGenerationException
66 67 68
     */
    public function save()
    {
69 70 71 72
        $separateSitemaps = $this->settings->getSettings(Schema::MODULE_NAME)['sitemap_separate'];

        foreach ($this->filenameMap[$separateSitemaps] as $sitemap) {
            $this->checkSitemapFilePermissions($sitemap['filename']);
73

74 75 76 77
            $urlSet = $this->collectSitemapItems($sitemap['secure']);

            $this->saveSitemap($urlSet, $sitemap['filename']);
        }
78

79
        return true;
80 81 82
    }

    /**
83
     * @param string $filename
84
     *
85 86
     * @throws SitemapGenerationException
     */
87
    protected function checkSitemapFilePermissions(string $filename)
88
    {
89
        $filePath = $this->getSitemapFilePath($filename);
90

Tino Goratsch's avatar
Tino Goratsch committed
91 92
        if (!\is_file($filePath)) {
            \touch($filePath);
93 94
        }

Tino Goratsch's avatar
Tino Goratsch committed
95
        if (!\is_file($filePath) || !\is_writable($filePath)) {
96 97 98 99 100 101 102 103
            throw new SitemapGenerationException(
                'The requested file "' . $filePath . '" either not exists or is not writable.'
                . 'Aborting sitemap generation.'
            );
        }
    }

    /**
104
     * @param string $filename
105
     *
106 107
     * @return string
     */
108
    protected function getSitemapFilePath(string $filename)
109
    {
110
        return ACP3_ROOT_DIR . $filename;
111 112 113
    }

    /**
114
     * @param bool|null $isSecure
115
     *
116 117
     * @return Urlset
     */
118
    protected function collectSitemapItems(?bool $isSecure)
119 120 121
    {
        $urlSet = new Urlset();
        foreach ($this->sitemapRegistrar->getAvailableModules() as $module) {
122
            foreach ($module->getUrls($isSecure) as $sitemapItem) {
123 124 125
                $urlSet->addUrl($sitemapItem);
            }
        }
Tino Goratsch's avatar
Tino Goratsch committed
126

127 128 129 130 131
        return $urlSet;
    }

    /**
     * @param Urlset $urlSet
132
     * @param string $filename
133
     *
134 135
     * @return bool
     */
136
    protected function saveSitemap(Urlset $urlSet, string $filename)
137 138 139
    {
        $output = (new Output())->getOutput($urlSet);

Tino Goratsch's avatar
Tino Goratsch committed
140
        return \file_put_contents($this->getSitemapFilePath($filename), $output) !== false;
141 142
    }
}