Passed
Push — master ( 8a8c4e...760901 )
by Christian
20:05 queued 09:47
created

SitemapRoute::load()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 22
rs 9.9332
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Content\Sitemap\SalesChannel;
4
5
use OpenApi\Annotations as OA;
6
use Shopware\Core\Content\Sitemap\Exception\AlreadyLockedException;
7
use Shopware\Core\Content\Sitemap\Service\SitemapExporterInterface;
8
use Shopware\Core\Content\Sitemap\Service\SitemapListerInterface;
9
use Shopware\Core\Content\Sitemap\Struct\SitemapCollection;
10
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
11
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
12
use Shopware\Core\System\SalesChannel\SalesChannelContext;
13
use Shopware\Core\System\SystemConfig\SystemConfigService;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Routing\Annotation\Route;
16
17
/**
18
 * @RouteScope(scopes={"store-api"})
19
 */
20
class SitemapRoute extends AbstractSitemapRoute
21
{
22
    /**
23
     * @var SitemapListerInterface
24
     */
25
    private $sitemapLister;
26
27
    /**
28
     * @var SystemConfigService
29
     */
30
    private $systemConfigService;
31
32
    /**
33
     * @var SitemapExporterInterface
34
     */
35
    private $sitemapExporter;
36
37
    public function __construct(SitemapListerInterface $sitemapLister, SystemConfigService $systemConfigService, SitemapExporterInterface $sitemapExporter)
38
    {
39
        $this->sitemapLister = $sitemapLister;
40
        $this->systemConfigService = $systemConfigService;
41
        $this->sitemapExporter = $sitemapExporter;
42
    }
43
44
    /**
45
     * @OA\Get(
46
     *      path="/sitemap",
47
     *      description="Sitemap",
48
     *      operationId="readSitemap",
49
     *      tags={"Store API", "Sitemap"},
50
     *      @OA\Response(
51
     *          response="200",
52
     *          description="",
53
     *          @OA\JsonContent(type="array", @OA\Items(ref="#/definitions/Sitemap"))
54
     *     )
55
     * )
56
     * @Route(path="/store-api/v{version}/sitemap", name="store-api.sitemap", methods={"GET", "POST"})
57
     */
58
    public function load(Request $request, SalesChannelContext $context): SitemapRouteResponse
59
    {
60
        $sitemaps = $this->sitemapLister->getSitemaps($context);
61
62
        if ($this->systemConfigService->getInt('core.sitemap.sitemapRefreshStrategy') !== SitemapExporterInterface::STRATEGY_LIVE) {
63
            return new SitemapRouteResponse(new SitemapCollection($sitemaps));
64
        }
65
66
        // Close session to prevent session locking from waiting in case there is another request coming in
67
        if ($request->hasSession()) {
68
            $request->getSession()->save();
69
        }
70
71
        try {
72
            $this->generateSitemap($context, true);
73
        } catch (AlreadyLockedException $exception) {
74
            // Silent catch, lock couldn't be acquired. Some other process already generates the sitemap.
75
        }
76
77
        $sitemaps = $this->sitemapLister->getSitemaps($context);
78
79
        return new SitemapRouteResponse(new SitemapCollection($sitemaps));
80
    }
81
82
    public function getDecorated(): AbstractSitemapRoute
83
    {
84
        throw new DecorationPatternException(self::class);
85
    }
86
87
    private function generateSitemap(SalesChannelContext $salesChannelContext, bool $force, ?string $lastProvider = null, ?int $offset = null): void
88
    {
89
        $result = $this->sitemapExporter->generate($salesChannelContext, $force, $lastProvider, $offset);
90
        if ($result->isFinish() === false) {
91
            $this->generateSitemap($salesChannelContext, $force, $result->getProvider(), $result->getOffset());
92
        }
93
    }
94
}
95