Skip to content

Commit c02c335

Browse files
authored
Merge pull request #95 from JerrySmidt/master
3.4.0
2 parents 0527de5 + 9322972 commit c02c335

File tree

21 files changed

+611
-108
lines changed

21 files changed

+611
-108
lines changed

Block/Onepage/LayoutProcessor.php

Lines changed: 76 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
use Magento\Framework\View\Element\AbstractBlock;
66
use Magento\Checkout\Block\Checkout\LayoutProcessorInterface;
77
use Magento\Framework\View\Element\Template\Context;
8+
use Magento\Framework\Exception\LocalizedException;
89

910
class LayoutProcessor extends AbstractBlock implements LayoutProcessorInterface
1011
{
1112
protected $scopeConfig;
13+
protected $jsLayout;
1214

1315
/**
1416
* Constructor
@@ -32,22 +34,25 @@ public function __construct(Context $context, array $data = [])
3234
* @param mixed $jsLayout
3335
* @return array
3436
*/
35-
public function process($jsLayout)
37+
public function process($jsLayout): array
3638
{
3739
$moduleEnabled = $this->scopeConfig->getValue('postcodenl_api/general/enabled', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
3840

3941
if (!$moduleEnabled) {
4042
return $jsLayout;
4143
}
4244

43-
// Shipping fields
44-
$shippingFields = &$jsLayout['components']
45-
['checkout']['children']
46-
['steps']['children']
47-
['shipping-step']['children']
48-
['shippingAddress']['children']
49-
['shipping-address-fieldset']['children'];
45+
$this->jsLayout = $jsLayout;
5046

47+
// Shipping fields
48+
$shippingFields = &$this->_getJsLayoutRef([
49+
'components',
50+
'checkout', 'children',
51+
'steps', 'children',
52+
'shipping-step', 'children',
53+
'shippingAddress', 'children',
54+
'shipping-address-fieldset', 'children'
55+
]);
5156
$shippingFields = $this->_changeAddressFieldsPosition($shippingFields);
5257

5358
// Autofill fields copy
@@ -62,12 +67,18 @@ public function process($jsLayout)
6267
);
6368

6469
// Billing step
65-
$billingConfiguration = &$jsLayout['components']
66-
['checkout']['children']
67-
['steps']['children']
68-
['billing-step']['children']
69-
['payment']['children']
70-
['payments-list']['children'];
70+
try {
71+
$billingConfiguration = &$this->_getJsLayoutRef([
72+
'components',
73+
'checkout', 'children',
74+
'steps', 'children',
75+
'billing-step', 'children',
76+
'payment', 'children',
77+
'payments-list', 'children',
78+
]);
79+
} catch (LocalizedException $e) {
80+
81+
}
7182

7283
if (isset($billingConfiguration)) {
7384
foreach ($billingConfiguration as $key => &$billingForm) {
@@ -88,14 +99,20 @@ public function process($jsLayout)
8899
}
89100

90101
// Billing address on payment page
91-
$billingFields = &$jsLayout['components']
92-
['checkout']['children']
93-
['steps']['children']
94-
['billing-step']['children']
95-
['payment']['children']
96-
['afterMethods']['children']
97-
['billing-address-form']['children']
98-
['form-fields']['children'];
102+
try {
103+
$billingFields = &$this->_getJsLayoutRef([
104+
'components',
105+
'checkout', 'children',
106+
'steps', 'children',
107+
'billing-step', 'children',
108+
'payment', 'children',
109+
'afterMethods', 'children',
110+
'billing-address-form', 'children',
111+
'form-fields', 'children',
112+
]);
113+
} catch (LocalizedException $e) {
114+
115+
}
99116

100117
if (isset($billingFields)) {
101118
$billingFields += $this->_updateCustomScope($autofillFields, 'billingAddressshared');
@@ -104,20 +121,47 @@ public function process($jsLayout)
104121
}
105122

106123
// Compatibility
107-
$magePlazaBillingFields = &$jsLayout['components']
108-
['checkout']['children']
109-
['steps']['children']
110-
['shipping-step']['children']
111-
['billingAddress']['children']
112-
['billing-address-fieldset']['children'];
124+
try {
125+
$magePlazaBillingFields = &$this->_getJsLayoutRef([
126+
'components',
127+
'checkout', 'children',
128+
'steps', 'children',
129+
'shipping-step', 'children',
130+
'billingAddress', 'children',
131+
'billing-address-fieldset', 'children',
132+
]);
133+
} catch (LocalizedException $e) {
134+
135+
}
113136

114137
if (isset($magePlazaBillingFields)) {
115138
$magePlazaBillingFields += $this->_updateCustomScope($autofillFields, 'billingAddress');
116139
$magePlazaBillingFields = $this->_updateDataScope($magePlazaBillingFields, 'billingAddress');
117140
$magePlazaBillingFields = $this->_changeAddressFieldsPosition($magePlazaBillingFields);
118141
}
119142

120-
return $jsLayout;
143+
return $this->jsLayout;
144+
}
145+
146+
/**
147+
* Get a reference to the specified path in $this->jsLayout.
148+
*
149+
* @param array $path - Path in Javascript layout.
150+
* @return array - Reference to path in $this->jsLayout.
151+
* @throws LocalizedException - Throw exception if path wasn't found.
152+
*/
153+
private function &_getJsLayoutRef(array $path): array
154+
{
155+
$result = &$this->jsLayout;
156+
foreach ($path as $key) {
157+
if (isset($result[$key])) {
158+
$result = &$result[$key];
159+
} else {
160+
throw new LocalizedException(__('Invalid path in Javascript layout: `%1`', implode('.', $path)));
161+
}
162+
}
163+
164+
return $result;
121165
}
122166

123167
/**
@@ -128,7 +172,7 @@ public function process($jsLayout)
128172
* @param string $dataScope
129173
* @return array - Fields with modified customScope.
130174
*/
131-
private function _updateCustomScope($fields, $dataScope)
175+
private function _updateCustomScope($fields, $dataScope): array
132176
{
133177
foreach ($fields as $name => $items) {
134178
if (isset($items['config'], $items['config']['customScope'])) {
@@ -153,7 +197,7 @@ private function _updateCustomScope($fields, $dataScope)
153197
* @param string $dataScope
154198
* @return array - Fields with modified customScope.
155199
*/
156-
private function _updateDataScope($fields, $dataScope)
200+
private function _updateDataScope($fields, $dataScope): array
157201
{
158202
foreach ($fields as $name => $items) {
159203
if (isset($items['dataScope'])) {
@@ -175,7 +219,7 @@ private function _updateDataScope($fields, $dataScope)
175219
* @param array $addressFields
176220
* @return array
177221
*/
178-
private function _changeAddressFieldsPosition($addressFields)
222+
private function _changeAddressFieldsPosition($addressFields): array
179223
{
180224
if ($this->scopeConfig->getValue('postcodenl_api/general/change_fields_position') != '1') {
181225
return $addressFields;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Flekto\Postcode\GraphQl\Exception;
4+
5+
use GraphQL\Error\ClientAware;
6+
use Magento\Framework\Exception\LocalizedException;
7+
use Magento\Framework\Phrase;
8+
9+
/**
10+
* Exception for GraphQL to be thrown when user supplies an invalid header
11+
*/
12+
class GraphQlHeaderException extends LocalizedException implements ClientAware
13+
{
14+
/**
15+
* Describing a category of the error
16+
*/
17+
const EXCEPTION_CATEGORY = 'graphql-header';
18+
19+
/**
20+
* @var boolean
21+
*/
22+
private $isSafe;
23+
24+
/**
25+
* @param Phrase $phrase
26+
* @param \Exception $cause
27+
* @param int $code
28+
* @param boolean $isSafe
29+
*/
30+
public function __construct(Phrase $phrase, \Exception $cause = null, $code = 0, $isSafe = true)
31+
{
32+
$this->isSafe = $isSafe;
33+
parent::__construct($phrase, $cause, $code);
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function isClientSafe(): bool
40+
{
41+
return $this->isSafe;
42+
}
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
public function getCategory(): string
48+
{
49+
return self::EXCEPTION_CATEGORY;
50+
}
51+
}

0 commit comments

Comments
 (0)