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\ProductImageSetsRestApi\Processor\Reader; |
||
9 | |||
10 | use Generated\Shared\Transfer\ProductConcreteStorageTransfer; |
||
11 | use Generated\Shared\Transfer\RestErrorMessageTransfer; |
||
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\ProductImageSetsRestApi\Dependency\Client\ProductImageSetsRestApiToProductImageStorageClientInterface; |
||
18 | use Spryker\Glue\ProductImageSetsRestApi\Dependency\Client\ProductImageSetsRestApiToProductStorageClientInterface; |
||
19 | use Spryker\Glue\ProductImageSetsRestApi\Processor\Mapper\ConcreteProductImageSetsMapperInterface; |
||
20 | use Spryker\Glue\ProductImageSetsRestApi\ProductImageSetsRestApiConfig; |
||
21 | use Spryker\Glue\ProductsRestApi\ProductsRestApiConfig; |
||
22 | use Symfony\Component\HttpFoundation\Response; |
||
23 | |||
24 | class ConcreteProductImageSetsReader implements ConcreteProductImageSetsReaderInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected const PRODUCT_CONCRETE_MAPPING_TYPE = 'sku'; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected const SELF_LINK_FORMAT = '%s/%s/%s'; |
||
35 | |||
36 | /** |
||
37 | * @var \Spryker\Glue\ProductImageSetsRestApi\Dependency\Client\ProductImageSetsRestApiToProductStorageClientInterface |
||
38 | */ |
||
39 | protected $productStorageClient; |
||
40 | |||
41 | /** |
||
42 | * @var \Spryker\Glue\ProductImageSetsRestApi\Dependency\Client\ProductImageSetsRestApiToProductImageStorageClientInterface |
||
43 | */ |
||
44 | protected $productImageStorageClient; |
||
45 | |||
46 | /** |
||
47 | * @var \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceBuilderInterface |
||
48 | */ |
||
49 | protected $restResourceBuilder; |
||
50 | |||
51 | /** |
||
52 | * @var \Spryker\Glue\ProductImageSetsRestApi\Processor\Mapper\ConcreteProductImageSetsMapperInterface |
||
53 | */ |
||
54 | protected $concreteProductImageSetsMapper; |
||
55 | |||
56 | /** |
||
57 | * @param \Spryker\Glue\ProductImageSetsRestApi\Dependency\Client\ProductImageSetsRestApiToProductStorageClientInterface $productStorageClient |
||
58 | * @param \Spryker\Glue\ProductImageSetsRestApi\Dependency\Client\ProductImageSetsRestApiToProductImageStorageClientInterface $productImageStorageClient |
||
59 | * @param \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceBuilderInterface $restResponseBuilder |
||
60 | * @param \Spryker\Glue\ProductImageSetsRestApi\Processor\Mapper\ConcreteProductImageSetsMapperInterface $concreteProductImageSetsMapper |
||
61 | */ |
||
62 | public function __construct( |
||
63 | ProductImageSetsRestApiToProductStorageClientInterface $productStorageClient, |
||
64 | ProductImageSetsRestApiToProductImageStorageClientInterface $productImageStorageClient, |
||
65 | RestResourceBuilderInterface $restResponseBuilder, |
||
66 | ConcreteProductImageSetsMapperInterface $concreteProductImageSetsMapper |
||
67 | ) { |
||
68 | $this->productStorageClient = $productStorageClient; |
||
69 | $this->productImageStorageClient = $productImageStorageClient; |
||
70 | $this->restResourceBuilder = $restResponseBuilder; |
||
71 | $this->concreteProductImageSetsMapper = $concreteProductImageSetsMapper; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param \Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface $restRequest |
||
76 | * |
||
77 | * @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface |
||
78 | */ |
||
79 | public function getConcreteProductImageSets(RestRequestInterface $restRequest): RestResponseInterface |
||
80 | { |
||
81 | $restResponse = $this->restResourceBuilder->createRestResponse(); |
||
82 | |||
83 | $parentResource = $restRequest->findParentResourceByType(ProductsRestApiConfig::RESOURCE_CONCRETE_PRODUCTS); |
||
84 | if (!$parentResource) { |
||
85 | return $restResponse->addError( |
||
86 | $this->createConcreteProductNotFoundError(), |
||
87 | ); |
||
88 | } |
||
89 | |||
90 | $concreteSku = $parentResource->getId(); |
||
91 | $restResource = $this->findConcreteProductImageSetsBySku($concreteSku, $restRequest); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
92 | |||
93 | if ($restResource === null) { |
||
94 | return $restResponse->addError( |
||
95 | $this->createConcreteProductImageSetsNotFoundError(), |
||
96 | ); |
||
97 | } |
||
98 | |||
99 | return $restResponse->addResource($restResource); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param string $sku |
||
104 | * @param \Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface $restRequest |
||
105 | * |
||
106 | * @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface|null |
||
107 | */ |
||
108 | public function findConcreteProductImageSetsBySku(string $sku, RestRequestInterface $restRequest): ?RestResourceInterface |
||
109 | { |
||
110 | $concreteProductStorageData = $this->productStorageClient |
||
111 | ->findProductConcreteStorageDataByMapping( |
||
112 | static::PRODUCT_CONCRETE_MAPPING_TYPE, |
||
113 | $sku, |
||
114 | $restRequest->getMetadata()->getLocale(), |
||
115 | ); |
||
116 | |||
117 | if (!$concreteProductStorageData) { |
||
118 | return null; |
||
119 | } |
||
120 | |||
121 | $concreteProductStorageTransfer = (new ProductConcreteStorageTransfer())->fromArray( |
||
122 | $concreteProductStorageData, |
||
123 | true, |
||
124 | ); |
||
125 | |||
126 | $productImageConcreteStorageTransfers = $this->productImageStorageClient |
||
127 | ->resolveProductImageSetStorageTransfers( |
||
128 | $concreteProductStorageTransfer->getIdProductConcrete(), |
||
129 | $concreteProductStorageTransfer->getIdProductAbstract(), |
||
130 | $restRequest->getMetadata()->getLocale(), |
||
131 | ); |
||
132 | |||
133 | if (!$productImageConcreteStorageTransfers) { |
||
134 | $productImageConcreteStorageTransfers = []; |
||
135 | } |
||
136 | |||
137 | return $this->buildProductImageSetsResource($sku, $productImageConcreteStorageTransfers); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param string $sku |
||
142 | * @param array<\Generated\Shared\Transfer\ProductImageSetStorageTransfer> $productImageConcreteStorageTransfers |
||
143 | * |
||
144 | * @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface |
||
145 | */ |
||
146 | protected function buildProductImageSetsResource(string $sku, array $productImageConcreteStorageTransfers): RestResourceInterface |
||
147 | { |
||
148 | $restProductImageSetsAttributesTransfer = $this->concreteProductImageSetsMapper |
||
149 | ->mapProductImageSetStorageTransfersToRestProductImageSetsAttributesTransfer($productImageConcreteStorageTransfers); |
||
150 | |||
151 | $restResource = $this->restResourceBuilder->createRestResource( |
||
152 | ProductImageSetsRestApiConfig::RESOURCE_CONCRETE_PRODUCT_IMAGE_SETS, |
||
153 | $sku, |
||
154 | $restProductImageSetsAttributesTransfer, |
||
155 | ); |
||
156 | |||
157 | $restResourceSelfLink = sprintf( |
||
158 | static::SELF_LINK_FORMAT, |
||
159 | ProductsRestApiConfig::RESOURCE_CONCRETE_PRODUCTS, |
||
160 | $sku, |
||
161 | ProductImageSetsRestApiConfig::RESOURCE_CONCRETE_PRODUCT_IMAGE_SETS, |
||
162 | ); |
||
163 | $restResource->addLink(RestLinkInterface::LINK_SELF, $restResourceSelfLink); |
||
164 | |||
165 | return $restResource; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @return \Generated\Shared\Transfer\RestErrorMessageTransfer |
||
170 | */ |
||
171 | protected function createConcreteProductNotFoundError(): RestErrorMessageTransfer |
||
172 | { |
||
173 | return (new RestErrorMessageTransfer()) |
||
174 | ->setCode(ProductsRestApiConfig::RESPONSE_CODE_CANT_FIND_CONCRETE_PRODUCT) |
||
175 | ->setStatus(Response::HTTP_NOT_FOUND) |
||
176 | ->setDetail(ProductsRestApiConfig::RESPONSE_DETAIL_CANT_FIND_CONCRETE_PRODUCT); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return \Generated\Shared\Transfer\RestErrorMessageTransfer |
||
181 | */ |
||
182 | protected function createConcreteProductImageSetsNotFoundError(): RestErrorMessageTransfer |
||
183 | { |
||
184 | $restErrorTransfer = (new RestErrorMessageTransfer()) |
||
185 | ->setCode(ProductImageSetsRestApiConfig::RESPONSE_CODE_CONCRETE_PRODUCT_IMAGE_SETS_NOT_FOUND) |
||
186 | ->setStatus(Response::HTTP_NOT_FOUND) |
||
187 | ->setDetail(ProductImageSetsRestApiConfig::RESPONSE_DETAIL_CONCRETE_PRODUCT_IMAGE_SETS_NOT_FOUND); |
||
188 | |||
189 | return $restErrorTransfer; |
||
190 | } |
||
191 | } |
||
192 |