Completed
Pull Request — master (#3)
by Oleksandr
32:39 queued 22:11
created

AbstractOrderMapper::buildPayload()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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