Issues (3641)

Processor/Error/RestCheckoutErrorMapper.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\CheckoutRestApi\Processor\Error;
9
10
use Generated\Shared\Transfer\RestCheckoutErrorTransfer;
11
use Generated\Shared\Transfer\RestErrorMessageTransfer;
12
use Spryker\Glue\CheckoutRestApi\CheckoutRestApiConfig;
13
use Spryker\Glue\CheckoutRestApi\Dependency\Client\CheckoutRestApiToGlossaryStorageClientInterface;
14
15
class RestCheckoutErrorMapper implements RestCheckoutErrorMapperInterface
16
{
17
    /**
18
     * @var \Spryker\Glue\CheckoutRestApi\CheckoutRestApiConfig
19
     */
20
    protected $config;
21
22
    /**
23
     * @var \Spryker\Glue\CheckoutRestApi\Dependency\Client\CheckoutRestApiToGlossaryStorageClientInterface
24
     */
25
    protected $glossaryStorageClient;
26
27
    /**
28
     * @param \Spryker\Glue\CheckoutRestApi\CheckoutRestApiConfig $config
29
     * @param \Spryker\Glue\CheckoutRestApi\Dependency\Client\CheckoutRestApiToGlossaryStorageClientInterface $glossaryStorageClient
30
     */
31
    public function __construct(
32
        CheckoutRestApiConfig $config,
33
        CheckoutRestApiToGlossaryStorageClientInterface $glossaryStorageClient
34
    ) {
35
        $this->config = $config;
36
        $this->glossaryStorageClient = $glossaryStorageClient;
37
    }
38
39
    /**
40
     * @param \Generated\Shared\Transfer\RestCheckoutErrorTransfer $restCheckoutErrorTransfer
41
     * @param \Generated\Shared\Transfer\RestErrorMessageTransfer $restErrorMessageTransfer
42
     *
43
     * @return \Generated\Shared\Transfer\RestErrorMessageTransfer
44
     */
45
    public function mapRestCheckoutErrorTransferToRestErrorTransfer(
46
        RestCheckoutErrorTransfer $restCheckoutErrorTransfer,
47
        RestErrorMessageTransfer $restErrorMessageTransfer
48
    ): RestErrorMessageTransfer {
49
        return $this->mergeErrorDataWithErrorConfiguration(
50
            $restCheckoutErrorTransfer,
51
            $restErrorMessageTransfer,
52
            $restCheckoutErrorTransfer->toArray(),
53
        );
54
    }
55
56
    /**
57
     * @param \Generated\Shared\Transfer\RestCheckoutErrorTransfer $restCheckoutErrorTransfer
58
     * @param \Generated\Shared\Transfer\RestErrorMessageTransfer $restErrorMessageTransfer
59
     * @param string $localeCode
60
     *
61
     * @return \Generated\Shared\Transfer\RestErrorMessageTransfer
62
     */
63
    public function mapLocalizedRestCheckoutErrorTransferToRestErrorTransfer(
64
        RestCheckoutErrorTransfer $restCheckoutErrorTransfer,
65
        RestErrorMessageTransfer $restErrorMessageTransfer,
66
        string $localeCode
67
    ): RestErrorMessageTransfer {
68
        return $this->mergeErrorDataWithErrorConfiguration(
69
            $restCheckoutErrorTransfer,
70
            $restErrorMessageTransfer,
71
            $this->translateCheckoutErrorMessage($restCheckoutErrorTransfer, $localeCode)->toArray(),
72
        );
73
    }
74
75
    /**
76
     * @param \Generated\Shared\Transfer\RestCheckoutErrorTransfer $restCheckoutErrorTransfer
77
     * @param \Generated\Shared\Transfer\RestErrorMessageTransfer $restErrorMessageTransfer
78
     * @param array $errorData
79
     *
80
     * @return \Generated\Shared\Transfer\RestErrorMessageTransfer
81
     */
82
    protected function mergeErrorDataWithErrorConfiguration(
83
        RestCheckoutErrorTransfer $restCheckoutErrorTransfer,
84
        RestErrorMessageTransfer $restErrorMessageTransfer,
85
        array $errorData
86
    ): RestErrorMessageTransfer {
87
        $errorIdentifierMapping = $this->getErrorIdentifierMapping($restCheckoutErrorTransfer);
88
89
        if ($errorIdentifierMapping) {
0 ignored issues
show
$errorIdentifierMapping is an empty array, thus is always false.
Loading history...
90
            $errorData = array_merge($errorIdentifierMapping, array_filter($errorData));
91
        }
92
93
        return $restErrorMessageTransfer->fromArray($errorData, true);
94
    }
95
96
    /**
97
     * @param \Generated\Shared\Transfer\RestCheckoutErrorTransfer $restCheckoutErrorTransfer
98
     *
99
     * @return array
100
     */
101
    protected function getErrorIdentifierMapping(RestCheckoutErrorTransfer $restCheckoutErrorTransfer): array
102
    {
103
        return $this->config->getErrorIdentifierToRestErrorMapping()[$restCheckoutErrorTransfer->getErrorIdentifier()] ?? [];
104
    }
105
106
    /**
107
     * @param \Generated\Shared\Transfer\RestCheckoutErrorTransfer $restCheckoutErrorTransfer
108
     * @param string $localeName
109
     *
110
     * @return \Generated\Shared\Transfer\RestCheckoutErrorTransfer
111
     */
112
    protected function translateCheckoutErrorMessage(
113
        RestCheckoutErrorTransfer $restCheckoutErrorTransfer,
114
        string $localeName
115
    ): RestCheckoutErrorTransfer {
116
        if (!$restCheckoutErrorTransfer->getDetail()) {
117
            return $restCheckoutErrorTransfer;
118
        }
119
120
        $restCheckoutErrorDetail = $this->glossaryStorageClient->translate(
121
            $restCheckoutErrorTransfer->getDetail(),
122
            $localeName,
123
            $restCheckoutErrorTransfer->getParameters(),
124
        );
125
126
        if (!$restCheckoutErrorDetail) {
127
            return $restCheckoutErrorTransfer;
128
        }
129
130
        return $restCheckoutErrorTransfer->setDetail($restCheckoutErrorDetail);
131
    }
132
}
133