Issues (3641)

...estCheckoutDataResourceRelationshipExpander.php (1 issue)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace Spryker\Glue\CartsRestApi\Processor\Expander;
9
10
use Generated\Shared\Transfer\RestCheckoutDataTransfer;
11
use Spryker\Glue\CartsRestApi\CartsRestApiConfig;
12
use Spryker\Glue\CartsRestApi\Processor\RestResponseBuilder\CartRestResponseBuilderInterface;
13
use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface;
14
use Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface;
15
16
class CartByRestCheckoutDataResourceRelationshipExpander implements CartByQuoteResourceRelationshipExpanderInterface
17
{
18
    /**
19
     * @var \Spryker\Glue\CartsRestApi\Processor\RestResponseBuilder\CartRestResponseBuilderInterface
20
     */
21
    protected CartRestResponseBuilderInterface $cartRestResponseBuilder;
22
23
    /**
24
     * @param \Spryker\Glue\CartsRestApi\Processor\RestResponseBuilder\CartRestResponseBuilderInterface $cartRestResponseBuilder
25
     */
26
    public function __construct(
27
        CartRestResponseBuilderInterface $cartRestResponseBuilder
28
    ) {
29
        $this->cartRestResponseBuilder = $cartRestResponseBuilder;
30
    }
31
32
    /**
33
     * @param array<\Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface> $resources
34
     * @param \Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface $restRequest
35
     *
36
     * @return void
37
     */
38
    public function addResourceRelationships(array $resources, RestRequestInterface $restRequest): void
39
    {
40
        $customerReference = $restRequest->getRestUser() ? $restRequest->getRestUser()->getNaturalIdentifier() : null;
41
42
        if (!$customerReference || !$this->isAuthorisedUser($restRequest)) {
43
            return;
44
        }
45
46
        foreach ($resources as $resource) {
47
            $restCheckoutDataTransfer = $resource->getPayload();
48
            if (
49
                !$restCheckoutDataTransfer instanceof RestCheckoutDataTransfer
50
                || $restCheckoutDataTransfer->getQuote() === null
0 ignored issues
show
The method getQuote() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
                || $restCheckoutDataTransfer->/** @scrutinizer ignore-call */ getQuote() === null

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...
51
            ) {
52
                continue;
53
            }
54
            $this->addCartResourceRelationships($resource, $restRequest->getMetadata()->getLocale());
55
        }
56
    }
57
58
    /**
59
     * @param \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface $resource
60
     * @param string $locale
61
     *
62
     * @return void
63
     */
64
    protected function addCartResourceRelationships(RestResourceInterface $resource, string $locale): void
65
    {
66
        /** @var \Generated\Shared\Transfer\RestCheckoutDataTransfer $restCheckoutDataTransfer */
67
        $restCheckoutDataTransfer = $resource->getPayload();
68
69
        $quoteTransfer = $restCheckoutDataTransfer->getQuote();
70
        $cartRestResponse = $this->cartRestResponseBuilder->createCartRestResponse($quoteTransfer, $locale);
71
72
        foreach ($cartRestResponse->getResources() as $cartRestResource) {
73
            $resource->addRelationship($cartRestResource);
74
        }
75
    }
76
77
    /**
78
     * @param \Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface $restRequest
79
     *
80
     * @return bool
81
     */
82
    protected function isAuthorisedUser(RestRequestInterface $restRequest): bool
83
    {
84
        return $restRequest->getHttpRequest()->headers->has(CartsRestApiConfig::HEADER_AUTHORIZATION);
85
    }
86
}
87