AbstractOrderMapper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\Episerver\Business\Mapper\Order;
9
10
use Generated\Shared\Transfer\CustomerTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CustomerTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Generated\Shared\Transfer\EpiserverRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\EpiserverRequestTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Generated\Shared\Transfer\OrderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\OrderTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Spryker\Shared\Kernel\Store;
14
use Spryker\Shared\Shipment\ShipmentConstants;
0 ignored issues
show
Bug introduced by
The type Spryker\Shared\Shipment\ShipmentConstants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use SprykerEco\Zed\Episerver\Dependency\Facade\EpiserverToLocaleFacadeInterface;
16
use SprykerEco\Zed\Episerver\Dependency\Facade\EpiserverToMoneyFacadeInterface;
17
use SprykerEco\Zed\Episerver\EpiserverConfig;
18
19
abstract class AbstractOrderMapper implements OrderMapperInterface
20
{
21
    /**
22
     * @var \SprykerEco\Zed\Episerver\EpiserverConfig
23
     */
24
    protected $config;
25
26
    /**
27
     * @var \SprykerEco\Zed\Episerver\Dependency\Facade\EpiserverToMoneyFacadeInterface
28
     */
29
    protected $moneyFacade;
30
31
    /**
32
     * @var \SprykerEco\Zed\Episerver\Dependency\Facade\EpiserverToLocaleFacadeInterface
33
     */
34
    protected $localeFacade;
35
36
    /**
37
     * @var \Spryker\Shared\Kernel\Store
38
     */
39
    protected $store;
40
41
    /**
42
     * @param \SprykerEco\Zed\Episerver\EpiserverConfig $config
43
     * @param \SprykerEco\Zed\Episerver\Dependency\Facade\EpiserverToMoneyFacadeInterface $moneyFacade
44
     * @param \SprykerEco\Zed\Episerver\Dependency\Facade\EpiserverToLocaleFacadeInterface $localeFacade
45
     * @param \Spryker\Shared\Kernel\Store $store
46
     */
47
    public function __construct(
48
        EpiserverConfig $config,
49
        EpiserverToMoneyFacadeInterface $moneyFacade,
50
        EpiserverToLocaleFacadeInterface $localeFacade,
51
        Store $store
52
    ) {
53
        $this->config = $config;
54
        $this->moneyFacade = $moneyFacade;
55
        $this->localeFacade = $localeFacade;
56
        $this->store = $store;
57
    }
58
59
    /**
60
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
61
     * @param \Generated\Shared\Transfer\EpiserverRequestTransfer $requestTransfer
62
     *
63
     * @return \Generated\Shared\Transfer\EpiserverRequestTransfer
64
     */
65
    public function mapOrderTransferToEpiserverRequestTransfer(OrderTransfer $orderTransfer, EpiserverRequestTransfer $requestTransfer): EpiserverRequestTransfer
66
    {
67
        $requestTransfer->setAuthorizationCode($this->config->getOrderListAuthorizationCode());
68
        $requestTransfer->setOperationType($this->config->getOperationSendTransactionEmail());
69
        $requestTransfer->setPayload($this->buildPayload($orderTransfer));
70
71
        return $requestTransfer;
72
    }
73
74
    /**
75
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
76
     *
77
     * @return array
78
     */
79
    protected function buildPayload(OrderTransfer $orderTransfer): array
80
    {
81
        $locale = $this->getLocale($orderTransfer->getCustomer());
82
83
        $payload = [
84
            static::KEY_MAILING_ID => $this->getMailingId(),
85
            static::KEY_EMAIL => $orderTransfer->getEmail(),
86
            static::KEY_SALUTATION => $orderTransfer->getSalutation(),
87
            static::KEY_FIRSTNAME => $orderTransfer->getFirstName(),
88
            static::KEY_LASTNAME => $orderTransfer->getLastName(),
89
            static::KEY_SPRYKER_ID => $orderTransfer->getFkCustomer(),
90
            static::KEY_CUSTOMER_SHOP_LOCALE => $locale,
91
            static::KEY_CUSTOMER_SHOP_URL => $this->config->getHostYves(),
92
            static::KEY_CUSTOMER_LOGIN_URL => $this->config->getHostYves() . static::URL_LOGIN,
93
            static::KEY_CUSTOMER_RESET_LINK => '',
94
            static::KEY_LANGUAGE => $locale,
95
            static::KEY_ORDER_NUMBER => $orderTransfer->getOrderReference(),
96
            static::KEY_ORDER_COMMENT => $orderTransfer->getCartNote(),
97
            static::KEY_ORDER_ORDERDATE => $orderTransfer->getCreatedAt(),
98
            static::KEY_ORDER_TOTAL_DELIVERY_COSTS => $this->calculateDeliveryCosts($orderTransfer),
99
            static::KEY_ORDER_TOTAL_PAYMENT_COSTS => $this->calculatePaymentMethodsTotal($orderTransfer),
100
        ];
101
102
        $totalsTransfer = $orderTransfer->getTotals();
103
104
        if ($totalsTransfer !== null) {
105
            $payload += [
106
                static::KEY_ORDER_SUBTOTAL => $totalsTransfer->getSubtotal(),
107
                static::KEY_ORDER_DISCOUNT => $totalsTransfer->getDiscountTotal(),
108
                static::KEY_ORDER_GRAND_TOTAL => $totalsTransfer->getGrandTotal(),
109
            ];
110
        }
111
112
        if ($totalsTransfer !== null && $totalsTransfer->getTaxTotal() !== null) {
113
            $payload[static::KEY_ORDER_TAX] = $totalsTransfer->getTaxTotal()->getAmount();
114
        }
115
116
        return $payload;
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    abstract protected function getMailingId(): string;
123
124
    /**
125
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
126
     *
127
     * @return int
128
     */
129
    protected function calculatePaymentMethodsTotal(OrderTransfer $orderTransfer): int
130
    {
131
        $sum = 0;
132
133
        $methods = $orderTransfer->getPayments();
134
135
        foreach ($methods as $method) {
136
            $sum += $method->getAmount();
137
        }
138
139
        return $sum;
140
    }
141
142
    /**
143
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
144
     *
145
     * @return int
146
     */
147
    protected function calculateDeliveryCosts(OrderTransfer $orderTransfer): int
148
    {
149
        $expenses = $orderTransfer->getExpenses();
150
        foreach ($expenses as $expense) {
151
            if ($expense->getType() === ShipmentConstants::SHIPMENT_EXPENSE_TYPE) {
152
                if ($expense->getSumPriceToPayAggregation() === null) {
153
                    return 0;
154
                }
155
156
                return $expense->getSumPriceToPayAggregation();
157
            }
158
        }
159
160
        return 0;
161
    }
162
163
    /**
164
     * @param \Generated\Shared\Transfer\CustomerTransfer|null $customerTransfer
165
     *
166
     * @return string
167
     */
168
    protected function getLocale(?CustomerTransfer $customerTransfer): string
169
    {
170
        if ($customerTransfer !== null &&
171
            $customerTransfer->getLocale() !== null &&
172
            $customerTransfer->getLocale()->getLocaleName() !== null) {
173
            return $this->getLocaleShortName($customerTransfer->getLocale()->getLocaleName());
174
        }
175
176
        return $this->getLocaleShortName($this->localeFacade->getCurrentLocaleName());
177
    }
178
179
    /**
180
     * @param string $localeName
181
     *
182
     * @return string
183
     */
184
    protected function getLocaleShortName(string $localeName): string
185
    {
186
        return (string)array_search($localeName, $this->store->getLocales());
187
    }
188
}
189