Issues (3877)

Processor/ProductTaxSet/ProductTaxSetReader.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\ProductTaxSetsRestApi\Processor\ProductTaxSet;
9
10
use Generated\Shared\Transfer\RestErrorMessageTransfer;
11
use Generated\Shared\Transfer\RestProductTaxSetsAttributesTransfer;
12
use Spryker\Glue\GlueApplication\Rest\JsonApi\RestLinkInterface;
13
use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceBuilderInterface;
14
use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface;
15
use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface;
16
use Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface;
17
use Spryker\Glue\ProductTaxSetsRestApi\Dependency\Client\ProductTaxSetsRestApiToTaxProductStorageClientInterface;
18
use Spryker\Glue\ProductTaxSetsRestApi\Dependency\Client\ProductTaxSetsRestApiToTaxStorageClientInterface;
19
use Spryker\Glue\ProductTaxSetsRestApi\Processor\Mapper\ProductTaxSetResourceMapperInterface;
20
use Spryker\Glue\ProductTaxSetsRestApi\ProductTaxSetsRestApiConfig;
21
use Symfony\Component\HttpFoundation\Response;
22
23
class ProductTaxSetReader implements ProductTaxSetReaderInterface
24
{
25
    /**
26
     * @var \Spryker\Glue\ProductTaxSetsRestApi\Dependency\Client\ProductTaxSetsRestApiToTaxProductStorageClientInterface
27
     */
28
    protected $taxProductStorageClient;
29
30
    /**
31
     * @var \Spryker\Glue\ProductTaxSetsRestApi\Dependency\Client\ProductTaxSetsRestApiToTaxStorageClientInterface
32
     */
33
    protected $taxStorageClient;
34
35
    /**
36
     * @var \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceBuilderInterface
37
     */
38
    protected $restResourceBuilder;
39
40
    /**
41
     * @var \Spryker\Glue\ProductTaxSetsRestApi\Processor\Mapper\ProductTaxSetResourceMapperInterface
42
     */
43
    protected $productTaxSetsResourceMapper;
44
45
    /**
46
     * @param \Spryker\Glue\ProductTaxSetsRestApi\Dependency\Client\ProductTaxSetsRestApiToTaxProductStorageClientInterface $taxProductStorageClient
47
     * @param \Spryker\Glue\ProductTaxSetsRestApi\Dependency\Client\ProductTaxSetsRestApiToTaxStorageClientInterface $taxStorageClient
48
     * @param \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceBuilderInterface $restResourceBuilder
49
     * @param \Spryker\Glue\ProductTaxSetsRestApi\Processor\Mapper\ProductTaxSetResourceMapperInterface $productTaxSetsResourceMapper
50
     */
51
    public function __construct(
52
        ProductTaxSetsRestApiToTaxProductStorageClientInterface $taxProductStorageClient,
53
        ProductTaxSetsRestApiToTaxStorageClientInterface $taxStorageClient,
54
        RestResourceBuilderInterface $restResourceBuilder,
55
        ProductTaxSetResourceMapperInterface $productTaxSetsResourceMapper
56
    ) {
57
        $this->taxProductStorageClient = $taxProductStorageClient;
58
        $this->taxStorageClient = $taxStorageClient;
59
        $this->restResourceBuilder = $restResourceBuilder;
60
        $this->productTaxSetsResourceMapper = $productTaxSetsResourceMapper;
61
    }
62
63
    /**
64
     * @param \Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface $restRequest
65
     *
66
     * @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface
67
     */
68
    public function getTaxSets(RestRequestInterface $restRequest): RestResponseInterface
69
    {
70
        $restResponse = $this->restResourceBuilder->createRestResponse();
71
72
        $parentResource = $restRequest->findParentResourceByType(ProductTaxSetsRestApiConfig::RESOURCE_ABSTRACT_PRODUCTS);
73
        if (!$parentResource) {
74
            return $this->createProductAbstractNotFoundError();
75
        }
76
77
        $restResource = $this->findProductAbstractTaxSetsByProductAbstractSku($parentResource->getId(), $restRequest);
0 ignored issues
show
It seems like $parentResource->getId() can also be of type null; however, parameter $productAbstractSku of Spryker\Glue\ProductTaxS...sByProductAbstractSku() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

77
        $restResource = $this->findProductAbstractTaxSetsByProductAbstractSku(/** @scrutinizer ignore-type */ $parentResource->getId(), $restRequest);
Loading history...
78
        if (!$restResource) {
79
            return $this->createProductTaxSetNotFoundError();
80
        }
81
82
        return $restResponse->addResource($restResource);
83
    }
84
85
    /**
86
     * @param string $productAbstractSku
87
     * @param \Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface $restRequest
88
     *
89
     * @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface|null
90
     */
91
    public function findProductAbstractTaxSetsByProductAbstractSku(
92
        string $productAbstractSku,
93
        RestRequestInterface $restRequest
94
    ): ?RestResourceInterface {
95
        $taxProductStorageTransfer = $this->taxProductStorageClient->findTaxProductStorageByProductAbstractSku($productAbstractSku);
96
        if (!$taxProductStorageTransfer) {
97
            return null;
98
        }
99
100
        $taxStorageTransfer = $this->taxStorageClient->findTaxSetStorageByIdTaxSet($taxProductStorageTransfer->getIdTaxSet());
101
        if (!$taxStorageTransfer) {
102
            return null;
103
        }
104
105
        $restProductTaxSetsAttributesTransfer = $this->productTaxSetsResourceMapper->mapTaxSetStorageTransferToRestProductTaxSetsAttributesTransfer(
106
            $taxStorageTransfer,
107
            new RestProductTaxSetsAttributesTransfer(),
108
        );
109
110
        return $this->formatRestResource($restProductTaxSetsAttributesTransfer, $taxStorageTransfer->getUuid(), $productAbstractSku);
111
    }
112
113
    /**
114
     * @param \Generated\Shared\Transfer\RestProductTaxSetsAttributesTransfer $restTaxSetsAttributesTransfer
115
     * @param string $uuid
116
     * @param string $parentResourceId
117
     *
118
     * @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface
119
     */
120
    protected function formatRestResource(
121
        RestProductTaxSetsAttributesTransfer $restTaxSetsAttributesTransfer,
122
        string $uuid,
123
        string $parentResourceId
124
    ): RestResourceInterface {
125
        $restResource = $this->restResourceBuilder->createRestResource(
126
            ProductTaxSetsRestApiConfig::RESOURCE_PRODUCT_TAX_SETS,
127
            $uuid,
128
            $restTaxSetsAttributesTransfer,
129
        );
130
131
        $selfLink = sprintf(
132
            '%s/%s/%s',
133
            ProductTaxSetsRestApiConfig::RESOURCE_ABSTRACT_PRODUCTS,
134
            $parentResourceId,
135
            ProductTaxSetsRestApiConfig::RESOURCE_PRODUCT_TAX_SETS,
136
        );
137
138
        $restResource->addLink(RestLinkInterface::LINK_SELF, $selfLink);
139
140
        return $restResource;
141
    }
142
143
    /**
144
     * @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface
145
     */
146
    protected function createProductAbstractNotFoundError(): RestResponseInterface
147
    {
148
        $restResponse = $this->restResourceBuilder->createRestResponse();
149
150
        $errorTransfer = (new RestErrorMessageTransfer())
151
            ->setCode(ProductTaxSetsRestApiConfig::RESPONSE_CODE_CANT_FIND_ABSTRACT_PRODUCT)
152
            ->setStatus(Response::HTTP_NOT_FOUND)
153
            ->setDetail(ProductTaxSetsRestApiConfig::RESPONSE_DETAIL_CANT_FIND_ABSTRACT_PRODUCT);
154
155
        return $restResponse->addError($errorTransfer);
156
    }
157
158
    /**
159
     * @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface
160
     */
161
    protected function createProductTaxSetNotFoundError(): RestResponseInterface
162
    {
163
        $restResponse = $this->restResourceBuilder->createRestResponse();
164
165
        $restErrorTransfer = (new RestErrorMessageTransfer())
166
            ->setCode(ProductTaxSetsRestApiConfig::RESPONSE_CODE_CANT_FIND_PRODUCT_TAX_SETS)
167
            ->setStatus(Response::HTTP_NOT_FOUND)
168
            ->setDetail(ProductTaxSetsRestApiConfig::RESPONSE_DETAIL_CANT_FIND_PRODUCT_TAX_SETS);
169
170
        return $restResponse->addError($restErrorTransfer);
171
    }
172
}
173