Passed
Push — trunk ( 202cbe...265b49 )
by Christian
10:34 queued 12s
created

ScriptController::renderStorefrontForScript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Controller;
4
5
use Shopware\Core\Framework\Log\Package;
6
use Shopware\Core\Framework\Script\Api\ScriptResponseEncoder;
7
use Shopware\Core\PlatformRequest;
8
use Shopware\Core\System\SalesChannel\Api\ResponseFields;
9
use Shopware\Core\System\SalesChannel\SalesChannelContext;
10
use Shopware\Storefront\Framework\Cache\CacheStore;
11
use Shopware\Storefront\Framework\Script\Api\StorefrontHook;
12
use Shopware\Storefront\Page\GenericPageLoaderInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\Routing\Annotation\Route;
16
17
/**
18
 * @internal
19
 * Do not use direct or indirect repository calls in a controller. Always use a store-api route to get or put data
20
 */
21
#[Route(defaults: ['_routeScope' => ['storefront']])]
22
#[Package('core')]
23
class ScriptController extends StorefrontController
24
{
25
    public function __construct(
26
        private readonly GenericPageLoaderInterface $pageLoader,
27
        private readonly ScriptResponseEncoder $scriptResponseEncoder
28
    ) {
29
    }
30
31
    #[Route(path: '/storefront/script/{hook}', name: 'frontend.script_endpoint', requirements: ['hook' => '.+'], defaults: ['XmlHttpRequest' => true], methods: ['GET', 'POST'])]
32
    public function execute(string $hook, Request $request, SalesChannelContext $context): Response
33
    {
34
        //  blog/update =>  blog-update
35
        $hookName = \str_replace('/', '-', $hook);
36
37
        $page = $this->pageLoader->load($request, $context);
38
39
        $hook = new StorefrontHook($hookName, $request->request->all(), $request->query->all(), $page, $context);
40
41
        // hook: storefront-{hook}
42
        $this->hook($hook);
43
44
        $fields = new ResponseFields(
45
            $request->get('includes', [])
46
        );
47
48
        $response = $hook->getScriptResponse();
49
50
        $symfonyResponse = $this->scriptResponseEncoder->encodeToSymfonyResponse(
51
            $response,
52
            $fields,
53
            \str_replace('-', '_', 'storefront_' . $hookName . '_response')
54
        );
55
56
        if ($response->getCache()->isEnabled()) {
57
            $request->attributes->set(PlatformRequest::ATTRIBUTE_HTTP_CACHE, ['maxAge' => $response->getCache()->getMaxAge(), 'states' => $response->getCache()->getInvalidationStates()]);
58
            $symfonyResponse->headers->set(CacheStore::TAG_HEADER, \json_encode($response->getCache()->getCacheTags(), \JSON_THROW_ON_ERROR));
59
        }
60
61
        return $symfonyResponse;
62
    }
63
64
    /**
65
     * @param array<string, mixed> $parameters
66
     */
67
    public function renderStorefrontForScript(string $view, array $parameters = []): Response
68
    {
69
        return $this->renderStorefront($view, $parameters);
70
    }
71
}
72