Skip to content

Commit 54556ef

Browse files
committed
Merge remote-tracking branch 'github-magento2ce/MAGETWO-91599' into EPAM-PR-9
2 parents 360f428 + 572fe3a commit 54556ef

File tree

5 files changed

+122
-35
lines changed

5 files changed

+122
-35
lines changed

app/code/Magento/OfflineShipping/Model/ResourceModel/Carrier/Tablerate/CSV/RowParser.php

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Magento\Framework\Phrase;
1010
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\LocationDirectory;
1111

12+
/**
13+
* Row parser.
14+
*/
1215
class RowParser
1316
{
1417
/**
@@ -26,6 +29,8 @@ public function __construct(LocationDirectory $locationDirectory)
2629
}
2730

2831
/**
32+
* Retrieve columns.
33+
*
2934
* @return array
3035
*/
3136
public function getColumns()
@@ -42,6 +47,8 @@ public function getColumns()
4247
}
4348

4449
/**
50+
* Parse provided row data.
51+
*
4552
* @param array $rowData
4653
* @param int $rowNumber
4754
* @param int $websiteId
@@ -71,23 +78,30 @@ public function parse(
7178
}
7279

7380
$countryId = $this->getCountryId($rowData, $rowNumber, $columnResolver);
74-
$regionId = $this->getRegionId($rowData, $rowNumber, $columnResolver, $countryId);
81+
$regionIds = $this->getRegionIds($rowData, $rowNumber, $columnResolver, $countryId);
7582
$zipCode = $this->getZipCode($rowData, $columnResolver);
7683
$conditionValue = $this->getConditionValue($rowData, $rowNumber, $conditionFullName, $columnResolver);
7784
$price = $this->getPrice($rowData, $rowNumber, $columnResolver);
7885

79-
return [
80-
'website_id' => $websiteId,
81-
'dest_country_id' => $countryId,
82-
'dest_region_id' => $regionId,
83-
'dest_zip' => $zipCode,
84-
'condition_name' => $conditionShortName,
85-
'condition_value' => $conditionValue,
86-
'price' => $price,
87-
];
86+
$rates = [];
87+
foreach ($regionIds as $regionId) {
88+
$rates[] = [
89+
'website_id' => $websiteId,
90+
'dest_country_id' => $countryId,
91+
'dest_region_id' => $regionId,
92+
'dest_zip' => $zipCode,
93+
'condition_name' => $conditionShortName,
94+
'condition_value' => $conditionValue,
95+
'price' => $price,
96+
];
97+
}
98+
99+
return $rates;
88100
}
89101

90102
/**
103+
* Get country id from provided row data.
104+
*
91105
* @param array $rowData
92106
* @param int $rowNumber
93107
* @param ColumnResolver $columnResolver
@@ -116,21 +130,23 @@ private function getCountryId(array $rowData, $rowNumber, ColumnResolver $column
116130
}
117131

118132
/**
133+
* Retrieve region id from provided row data.
134+
*
119135
* @param array $rowData
120136
* @param int $rowNumber
121137
* @param ColumnResolver $columnResolver
122138
* @param int $countryId
123-
* @return int|string
139+
* @return array
124140
* @throws ColumnNotFoundException
125141
* @throws RowException
126142
*/
127-
private function getRegionId(array $rowData, $rowNumber, ColumnResolver $columnResolver, $countryId)
143+
private function getRegionIds(array $rowData, $rowNumber, ColumnResolver $columnResolver, $countryId)
128144
{
129145
$regionCode = $columnResolver->getColumnValue(ColumnResolver::COLUMN_REGION, $rowData);
130146
if ($countryId !== '0' && $this->locationDirectory->hasRegionId($countryId, $regionCode)) {
131-
$regionId = $this->locationDirectory->getRegionId($countryId, $regionCode);
147+
$regionIds = $this->locationDirectory->getRegionIds($countryId, $regionCode);
132148
} elseif ($regionCode === '*' || $regionCode === '') {
133-
$regionId = 0;
149+
$regionIds = [0];
134150
} else {
135151
throw new RowException(
136152
__(
@@ -141,10 +157,12 @@ private function getRegionId(array $rowData, $rowNumber, ColumnResolver $columnR
141157
)
142158
);
143159
}
144-
return $regionId;
160+
return $regionIds;
145161
}
146162

147163
/**
164+
* Retrieve zip code from provided row data.
165+
*
148166
* @param array $rowData
149167
* @param ColumnResolver $columnResolver
150168
* @return float|int|null|string
@@ -160,6 +178,8 @@ private function getZipCode(array $rowData, ColumnResolver $columnResolver)
160178
}
161179

162180
/**
181+
* Get condition value form provided row data.
182+
*
163183
* @param array $rowData
164184
* @param int $rowNumber
165185
* @param string $conditionFullName
@@ -187,6 +207,8 @@ private function getConditionValue(array $rowData, $rowNumber, $conditionFullNam
187207
}
188208

189209
/**
210+
* Retrieve price from provided row data.
211+
*
190212
* @param array $rowData
191213
* @param int $rowNumber
192214
* @param ColumnResolver $columnResolver
@@ -212,6 +234,7 @@ private function getPrice(array $rowData, $rowNumber, ColumnResolver $columnReso
212234

213235
/**
214236
* Parse and validate positive decimal value
237+
*
215238
* Return false if value is not decimal or is not positive
216239
*
217240
* @param string $value

app/code/Magento/OfflineShipping/Model/ResourceModel/Carrier/Tablerate/Import.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Store\Model\StoreManagerInterface;
1818

1919
/**
20+
* Import offline shipping.
2021
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2122
*/
2223
class Import
@@ -87,6 +88,8 @@ public function __construct(
8788
}
8889

8990
/**
91+
* Check if there are errors.
92+
*
9093
* @return bool
9194
*/
9295
public function hasErrors()
@@ -95,6 +98,8 @@ public function hasErrors()
9598
}
9699

97100
/**
101+
* Get errors.
102+
*
98103
* @return array
99104
*/
100105
public function getErrors()
@@ -103,6 +108,8 @@ public function getErrors()
103108
}
104109

105110
/**
111+
* Retrieve columns.
112+
*
106113
* @return array
107114
*/
108115
public function getColumns()
@@ -111,6 +118,8 @@ public function getColumns()
111118
}
112119

113120
/**
121+
* Get data from file.
122+
*
114123
* @param ReadInterface $file
115124
* @param int $websiteId
116125
* @param string $conditionShortName
@@ -135,7 +144,7 @@ public function getData(ReadInterface $file, $websiteId, $conditionShortName, $c
135144
if (empty($csvLine)) {
136145
continue;
137146
}
138-
$rowData = $this->rowParser->parse(
147+
$rowsData = $this->rowParser->parse(
139148
$csvLine,
140149
$rowNumber,
141150
$websiteId,
@@ -144,20 +153,25 @@ public function getData(ReadInterface $file, $websiteId, $conditionShortName, $c
144153
$columnResolver
145154
);
146155

147-
// protect from duplicate
148-
$hash = $this->dataHashGenerator->getHash($rowData);
149-
if (array_key_exists($hash, $this->uniqueHash)) {
150-
throw new RowException(
151-
__(
152-
'Duplicate Row #%1 (duplicates row #%2)',
153-
$rowNumber,
154-
$this->uniqueHash[$hash]
155-
)
156-
);
156+
foreach ($rowsData as $rowData) {
157+
// protect from duplicate
158+
$hash = $this->dataHashGenerator->getHash($rowData);
159+
if (array_key_exists($hash, $this->uniqueHash)) {
160+
throw new RowException(
161+
__(
162+
'Duplicate Row #%1 (duplicates row #%2)',
163+
$rowNumber,
164+
$this->uniqueHash[$hash]
165+
)
166+
);
167+
}
168+
$this->uniqueHash[$hash] = $rowNumber;
169+
170+
$items[] = $rowData;
171+
}
172+
if (count($rowsData) > 1) {
173+
$bunchSize += count($rowsData) - 1;
157174
}
158-
$this->uniqueHash[$hash] = $rowNumber;
159-
160-
$items[] = $rowData;
161175
if (count($items) === $bunchSize) {
162176
yield $items;
163177
$items = [];
@@ -172,6 +186,8 @@ public function getData(ReadInterface $file, $websiteId, $conditionShortName, $c
172186
}
173187

174188
/**
189+
* Retrieve column headers.
190+
*
175191
* @param ReadInterface $file
176192
* @return array|bool
177193
* @throws LocalizedException

app/code/Magento/OfflineShipping/Model/ResourceModel/Carrier/Tablerate/LocationDirectory.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@
66

77
namespace Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate;
88

9+
/**
10+
* Location directory.
11+
*/
912
class LocationDirectory
1013
{
1114
/**
1215
* @var array
1316
*/
1417
protected $regions;
1518

19+
/**
20+
* @var array
21+
*/
22+
private $regionsByCode;
23+
1624
/**
1725
* @var array
1826
*/
@@ -47,6 +55,8 @@ public function __construct(
4755
}
4856

4957
/**
58+
* Retrieve country id.
59+
*
5060
* @param string $countryCode
5161
* @return null|string
5262
*/
@@ -88,6 +98,8 @@ protected function loadCountries()
8898
}
8999

90100
/**
101+
* Check if there is country id with provided country code.
102+
*
91103
* @param string $countryCode
92104
* @return bool
93105
*/
@@ -98,6 +110,8 @@ public function hasCountryId($countryCode)
98110
}
99111

100112
/**
113+
* Check if there is region id with provided region code and country id.
114+
*
101115
* @param string $countryId
102116
* @param string $regionCode
103117
* @return bool
@@ -115,29 +129,50 @@ public function hasRegionId($countryId, $regionCode)
115129
*/
116130
protected function loadRegions()
117131
{
118-
if ($this->regions !== null) {
132+
if ($this->regions !== null && $this->regionsByCode !== null) {
119133
return $this;
120134
}
121135

122136
$this->regions = [];
137+
$this->regionsByCode = [];
123138

124139
/** @var $collection \Magento\Directory\Model\ResourceModel\Region\Collection */
125140
$collection = $this->_regionCollectionFactory->create();
126141
foreach ($collection->getData() as $row) {
127142
$this->regions[$row['country_id']][$row['code']] = (int)$row['region_id'];
143+
if (empty($this->regionsByCode[$row['country_id']][$row['code']])) {
144+
$this->regionsByCode[$row['country_id']][$row['code']] = [];
145+
}
146+
$this->regionsByCode[$row['country_id']][$row['code']][] = (int)$row['region_id'];
128147
}
129148

130149
return $this;
131150
}
132151

133152
/**
153+
* Retrieve region id.
154+
*
134155
* @param int $countryId
135156
* @param string $regionCode
136157
* @return string
158+
* @deprecated
137159
*/
138160
public function getRegionId($countryId, $regionCode)
139161
{
140162
$this->loadRegions();
141163
return $this->regions[$countryId][$regionCode];
142164
}
165+
166+
/**
167+
* Return region ids for country and region
168+
*
169+
* @param int $countryId
170+
* @param string $regionCode
171+
* @return array
172+
*/
173+
public function getRegionIds($countryId, $regionCode)
174+
{
175+
$this->loadRegions();
176+
return $this->regionsByCode[$countryId][$regionCode];
177+
}
143178
}

app/code/Magento/OfflineShipping/Test/Unit/Model/ResourceModel/Carrier/Tablerate/CSV/RowParserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class RowParserTest extends \PHPUnit\Framework\TestCase
3737
protected function setUp()
3838
{
3939
$this->locationDirectoryMock = $this->getMockBuilder(LocationDirectory::class)
40-
->setMethods(['hasCountryId', 'getCountryId', 'hasRegionId', 'getRegionId'])
40+
->setMethods(['hasCountryId', 'getCountryId', 'hasRegionId', 'getRegionIds'])
4141
->disableOriginalConstructor()
4242
->getMock();
4343
$this->columnResolverMock = $this->getMockBuilder(ColumnResolver::class)
@@ -92,7 +92,7 @@ public function testParse()
9292
$conditionShortName,
9393
$columnValueMap
9494
);
95-
$this->assertEquals($expectedResult, $result);
95+
$this->assertEquals([$expectedResult], $result);
9696
}
9797

9898
/**

app/code/Magento/OfflineShipping/Test/Unit/Model/ResourceModel/Carrier/Tablerate/ImportTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ protected function setUp()
7777
->getMock();
7878
$this->dataHashGeneratorMock = $this->getMockBuilder(DataHashGenerator::class)
7979
->getMock();
80-
$this->rowParserMock->expects($this->any())
81-
->method('parse')
82-
->willReturnArgument(0);
8380
$this->dataHashGeneratorMock->expects($this->any())
8481
->method('getHash')
8582
->willReturnCallback(
@@ -124,6 +121,15 @@ public function testGetData()
124121
['a4', 'b4', 'c4', 'd4', 'e4'],
125122
['a5', 'b5', 'c5', 'd5', 'e5'],
126123
];
124+
$this->rowParserMock->expects($this->any())
125+
->method('parse')
126+
->willReturn(
127+
[['a1', 'b1', 'c1', 'd1', 'e1']],
128+
[['a2', 'b2', 'c2', 'd2', 'e2']],
129+
[['a3', 'b3', 'c3', 'd3', 'e3']],
130+
[['a4', 'b4', 'c4', 'd4', 'e4']],
131+
[['a5', 'b5', 'c5', 'd5', 'e5']]
132+
);
127133
$file = $this->createFileMock($lines);
128134
$expectedResult = [
129135
[
@@ -167,6 +173,13 @@ public function testGetDataWithDuplicatedLine()
167173
[],
168174
['a2', 'b2', 'c2', 'd2', 'e2'],
169175
];
176+
$this->rowParserMock->expects($this->any())
177+
->method('parse')
178+
->willReturn(
179+
[['a1', 'b1', 'c1', 'd1', 'e1']],
180+
[['a1', 'b1', 'c1', 'd1', 'e1']],
181+
[['a2', 'b2', 'c2', 'd2', 'e2']]
182+
);
170183
$file = $this->createFileMock($lines);
171184
$expectedResult = [
172185
[

0 commit comments

Comments
 (0)