Skip to content

Commit 91bd4c2

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-30299-Reduce-Code-Complexity' into develop
2 parents e63ebb7 + 5cd8723 commit 91bd4c2

File tree

5 files changed

+212
-162
lines changed

5 files changed

+212
-162
lines changed

app/code/Magento/Webapi/Controller/Rest.php

Lines changed: 7 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
namespace Magento\Webapi\Controller;
77

8-
use Magento\Authorization\Model\UserContextInterface;
98
use Magento\Framework\AuthorizationInterface;
109
use Magento\Framework\Exception\AuthorizationException;
1110
use Magento\Framework\Webapi\ErrorProcessor;
@@ -14,9 +13,9 @@
1413
use Magento\Framework\Webapi\Rest\Request as RestRequest;
1514
use Magento\Framework\Webapi\Rest\Response as RestResponse;
1615
use Magento\Framework\Webapi\Rest\Response\FieldsFilter;
16+
use Magento\Webapi\Controller\Rest\ParamsOverrider;
1717
use Magento\Webapi\Controller\Rest\Router;
1818
use Magento\Webapi\Controller\Rest\Router\Route;
19-
use Magento\Webapi\Model\Config\Converter;
2019

2120
/**
2221
* Front controller for WebAPI REST area.
@@ -93,9 +92,9 @@ class Rest implements \Magento\Framework\App\FrontControllerInterface
9392
protected $session;
9493

9594
/**
96-
* @var \Magento\Authorization\Model\UserContextInterface
95+
* @var ParamsOverrider
9796
*/
98-
protected $userContext;
97+
protected $paramsOverrider;
9998

10099
/**
101100
* @var ServiceOutputProcessor $serviceOutputProcessor
@@ -116,7 +115,7 @@ class Rest implements \Magento\Framework\App\FrontControllerInterface
116115
* @param PathProcessor $pathProcessor
117116
* @param \Magento\Framework\App\AreaList $areaList
118117
* @param FieldsFilter $fieldsFilter
119-
* @param UserContextInterface $userContext
118+
* @param ParamsOverrider $paramsOverrider
120119
* @param ServiceOutputProcessor $serviceOutputProcessor
121120
*
122121
* TODO: Consider removal of warning suppression
@@ -134,7 +133,7 @@ public function __construct(
134133
PathProcessor $pathProcessor,
135134
\Magento\Framework\App\AreaList $areaList,
136135
FieldsFilter $fieldsFilter,
137-
UserContextInterface $userContext,
136+
ParamsOverrider $paramsOverrider,
138137
ServiceOutputProcessor $serviceOutputProcessor
139138
) {
140139
$this->_router = $router;
@@ -148,7 +147,7 @@ public function __construct(
148147
$this->_pathProcessor = $pathProcessor;
149148
$this->areaList = $areaList;
150149
$this->fieldsFilter = $fieldsFilter;
151-
$this->userContext = $userContext;
150+
$this->paramsOverrider = $paramsOverrider;
152151
$this->serviceOutputProcessor = $serviceOutputProcessor;
153152
}
154153

@@ -174,7 +173,7 @@ public function dispatch(\Magento\Framework\App\RequestInterface $request)
174173
$inputData = $this->_request->getRequestData();
175174
$serviceMethodName = $route->getServiceMethod();
176175
$serviceClassName = $route->getServiceClass();
177-
$inputData = $this->overrideParams($inputData, $route->getParameters());
176+
$inputData = $this->paramsOverrider->override($inputData, $route->getParameters());
178177
$inputParams = $this->serviceInputProcessor->process($serviceClassName, $serviceMethodName, $inputData);
179178
$service = $this->_objectManager->get($serviceClassName);
180179
/** @var \Magento\Framework\Api\AbstractExtensibleObject $outputData */
@@ -195,74 +194,6 @@ public function dispatch(\Magento\Framework\App\RequestInterface $request)
195194
return $this->_response;
196195
}
197196

198-
/**
199-
* Override parameter values based on webapi.xml
200-
*
201-
* @param array $inputData Incoming data from request
202-
* @param array $parameters Contains parameters to replace or default
203-
* @return array Data in same format as $inputData with appropriate parameters added or changed
204-
*/
205-
protected function overrideParams(array $inputData, array $parameters)
206-
{
207-
foreach ($parameters as $name => $paramData) {
208-
$arrayKeys = explode('.', $name);
209-
if ($paramData[Converter::KEY_FORCE] || !$this->isNestedArrayValueSet($inputData, $arrayKeys)) {
210-
if ($paramData[Converter::KEY_VALUE] == '%customer_id%'
211-
&& $this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER
212-
) {
213-
$value = $this->userContext->getUserId();
214-
} else {
215-
$value = $paramData[Converter::KEY_VALUE];
216-
}
217-
$this->setNestedArrayValue($inputData, $arrayKeys, $value);
218-
}
219-
}
220-
return $inputData;
221-
}
222-
223-
/**
224-
* Determine if a nested array value is set.
225-
*
226-
* @param array &$nestedArray
227-
* @param string[] $arrayKeys
228-
* @return bool true if array value is set
229-
*/
230-
protected function isNestedArrayValueSet(&$nestedArray, $arrayKeys)
231-
{
232-
$currentArray = &$nestedArray;
233-
234-
foreach ($arrayKeys as $key) {
235-
if (!isset($currentArray[$key])) {
236-
return false;
237-
}
238-
$currentArray = &$currentArray[$key];
239-
}
240-
return true;
241-
}
242-
243-
/**
244-
* Set a nested array value.
245-
*
246-
* @param array &$nestedArray
247-
* @param string[] $arrayKeys
248-
* @param string $valueToSet
249-
* @return void
250-
*/
251-
protected function setNestedArrayValue(&$nestedArray, $arrayKeys, $valueToSet)
252-
{
253-
$currentArray = &$nestedArray;
254-
$lastKey = array_pop($arrayKeys);
255-
256-
foreach ($arrayKeys as $key) {
257-
if (!isset($currentArray[$key])) {
258-
$currentArray[$key] = [];
259-
}
260-
$currentArray = &$currentArray[$key];
261-
}
262-
263-
$currentArray[$lastKey] = $valueToSet;
264-
}
265-
266197
/**
267198
* Retrieve current route.
268199
*
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Webapi\Controller\Rest;
8+
9+
use Magento\Authorization\Model\UserContextInterface;
10+
use Magento\Webapi\Model\Config\Converter;
11+
12+
/**
13+
* Override parameter values
14+
*/
15+
class ParamsOverrider
16+
{
17+
/**
18+
* @var \Magento\Authorization\Model\UserContextInterface
19+
*/
20+
protected $userContext;
21+
22+
/**
23+
* Initialize dependencies
24+
*
25+
* @param UserContextInterface $userContext
26+
*/
27+
public function __construct(UserContextInterface $userContext)
28+
{
29+
$this->userContext = $userContext;
30+
}
31+
32+
/**
33+
* Override parameter values based on webapi.xml
34+
*
35+
* @param array $inputData Incoming data from request
36+
* @param array $parameters Contains parameters to replace or default
37+
* @return array Data in same format as $inputData with appropriate parameters added or changed
38+
*/
39+
public function override(array $inputData, array $parameters)
40+
{
41+
foreach ($parameters as $name => $paramData) {
42+
$arrayKeys = explode('.', $name);
43+
if ($paramData[Converter::KEY_FORCE] || !$this->isNestedArrayValueSet($inputData, $arrayKeys)) {
44+
if ($paramData[Converter::KEY_VALUE] == '%customer_id%'
45+
&& $this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER
46+
) {
47+
$value = $this->userContext->getUserId();
48+
} else {
49+
$value = $paramData[Converter::KEY_VALUE];
50+
}
51+
$this->setNestedArrayValue($inputData, $arrayKeys, $value);
52+
}
53+
}
54+
return $inputData;
55+
}
56+
57+
/**
58+
* Determine if a nested array value is set.
59+
*
60+
* @param array &$nestedArray
61+
* @param string[] $arrayKeys
62+
* @return bool true if array value is set
63+
*/
64+
protected function isNestedArrayValueSet(&$nestedArray, $arrayKeys)
65+
{
66+
$currentArray = &$nestedArray;
67+
68+
foreach ($arrayKeys as $key) {
69+
if (!isset($currentArray[$key])) {
70+
return false;
71+
}
72+
$currentArray = &$currentArray[$key];
73+
}
74+
return true;
75+
}
76+
77+
/**
78+
* Set a nested array value.
79+
*
80+
* @param array &$nestedArray
81+
* @param string[] $arrayKeys
82+
* @param string $valueToSet
83+
* @return void
84+
*/
85+
protected function setNestedArrayValue(&$nestedArray, $arrayKeys, $valueToSet)
86+
{
87+
$currentArray = &$nestedArray;
88+
$lastKey = array_pop($arrayKeys);
89+
90+
foreach ($arrayKeys as $key) {
91+
if (!isset($currentArray[$key])) {
92+
$currentArray[$key] = [];
93+
}
94+
$currentArray = &$currentArray[$key];
95+
}
96+
97+
$currentArray[$lastKey] = $valueToSet;
98+
}
99+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Webapi\Controller\Rest;
8+
9+
use \Magento\Authorization\Model\UserContextInterface;
10+
11+
/**
12+
* Test Magento\Webapi\Controller\Rest\ParamsOverrider
13+
*/
14+
class ParamsOverriderTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @param array $requestData Data from the request
18+
* @param array $parameters Data from config about which parameters to override
19+
* @param array $expectedOverriddenParams Result of overriding $requestData when applying rules from $parameters
20+
* @param int $userId The id of the user invoking the request
21+
* @param int $userType The type of user invoking the request
22+
*
23+
* @dataProvider overrideParamsDataProvider
24+
*/
25+
public function testOverrideParams($requestData, $parameters, $expectedOverriddenParams, $userId, $userType)
26+
{
27+
$objectManager = new \Magento\TestFramework\Helper\ObjectManager($this);
28+
29+
$userContextMock = $this->getMockBuilder('Magento\Authorization\Model\UserContextInterface')
30+
->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMockForAbstractClass();
31+
$userContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($userId));
32+
$userContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($userType));
33+
34+
/** @var \Magento\Webapi\Controller\Rest\ParamsOverrider $paramsOverrider */
35+
$paramsOverrider = $objectManager->getObject(
36+
'Magento\Webapi\Controller\Rest\ParamsOverrider',
37+
['userContext' => $userContextMock]
38+
);
39+
40+
$this->assertEquals($expectedOverriddenParams, $paramsOverrider->override($requestData, $parameters));
41+
}
42+
43+
/**
44+
* @return array
45+
*/
46+
public function overrideParamsDataProvider()
47+
{
48+
return [
49+
'force false, value present' => [
50+
['Name1' => 'valueIn'],
51+
['Name1' => ['force' => false, 'value' => 'valueOverride']],
52+
['Name1' => 'valueIn'],
53+
1,
54+
UserContextInterface::USER_TYPE_INTEGRATION,
55+
],
56+
'force true, value present' => [
57+
['Name1' => 'valueIn'],
58+
['Name1' => ['force' => true, 'value' => 'valueOverride']],
59+
['Name1' => 'valueOverride'],
60+
1,
61+
UserContextInterface::USER_TYPE_INTEGRATION,
62+
],
63+
'force true, value not present' => [
64+
['Name1' => 'valueIn'],
65+
['Name2' => ['force' => true, 'value' => 'valueOverride']],
66+
['Name1' => 'valueIn', 'Name2' => 'valueOverride'],
67+
1,
68+
UserContextInterface::USER_TYPE_INTEGRATION,
69+
],
70+
'force false, value not present' => [
71+
['Name1' => 'valueIn'],
72+
['Name2' => ['force' => false, 'value' => 'valueOverride']],
73+
['Name1' => 'valueIn', 'Name2' => 'valueOverride'],
74+
1,
75+
UserContextInterface::USER_TYPE_INTEGRATION,
76+
],
77+
'force true, value present, override value is %customer_id%' => [
78+
['Name1' => 'valueIn'],
79+
['Name1' => ['force' => true, 'value' => '%customer_id%']],
80+
['Name1' => '1234'],
81+
1234,
82+
UserContextInterface::USER_TYPE_CUSTOMER,
83+
],
84+
'force true, value present, override value is %customer_id%, not a customer' => [
85+
['Name1' => 'valueIn'],
86+
['Name1' => ['force' => true, 'value' => '%customer_id%']],
87+
['Name1' => '%customer_id%'],
88+
1234,
89+
UserContextInterface::USER_TYPE_INTEGRATION,
90+
],
91+
];
92+
}
93+
}

0 commit comments

Comments
 (0)