Skip to content

Commit b2e6eb6

Browse files
committed
MAGETWO-38857: Decrease crap
1 parent 72f8dc1 commit b2e6eb6

File tree

2 files changed

+166
-82
lines changed

2 files changed

+166
-82
lines changed

app/code/Magento/Widget/Model/Widget.php

Lines changed: 94 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
* Copyright © 2015 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
7-
// @codingStandardsIgnoreFile
8-
96
namespace Magento\Widget\Model;
107

118
/**
@@ -16,39 +13,37 @@ class Widget
1613
/**
1714
* @var \Magento\Widget\Model\Config\Data
1815
*/
19-
protected $_dataStorage;
16+
protected $dataStorage;
2017

2118
/**
2219
* @var \Magento\Framework\App\Cache\Type\Config
2320
*/
24-
protected $_configCacheType;
21+
protected $configCacheType;
2522

2623
/**
2724
* @var \Magento\Framework\View\Asset\Repository
2825
*/
29-
protected $_assetRepo;
26+
protected $assetRepo;
3027

3128
/**
3229
* @var \Magento\Framework\View\Asset\Source
3330
*/
34-
protected $_assetSource;
31+
protected $assetSource;
3532

3633
/**
3734
* @var \Magento\Framework\View\FileSystem
3835
*/
39-
protected $_viewFileSystem;
36+
protected $viewFileSystem;
4037

4138
/**
42-
* Core data
43-
*
4439
* @var \Magento\Framework\Escaper
4540
*/
46-
protected $_escaper;
41+
protected $escaper;
4742

4843
/**
4944
* @var array
5045
*/
51-
protected $_widgetsArray = [];
46+
protected $widgetsArray = [];
5247

5348
/**
5449
* @var \Magento\Widget\Helper\Conditions
@@ -71,11 +66,11 @@ public function __construct(
7166
\Magento\Framework\View\FileSystem $viewFileSystem,
7267
\Magento\Widget\Helper\Conditions $conditionsHelper
7368
) {
74-
$this->_escaper = $escaper;
75-
$this->_dataStorage = $dataStorage;
76-
$this->_assetRepo = $assetRepo;
77-
$this->_assetSource = $assetSource;
78-
$this->_viewFileSystem = $viewFileSystem;
69+
$this->escaper = $escaper;
70+
$this->dataStorage = $dataStorage;
71+
$this->assetRepo = $assetRepo;
72+
$this->assetSource = $assetSource;
73+
$this->viewFileSystem = $viewFileSystem;
7974
$this->conditionsHelper = $conditionsHelper;
8075
}
8176

@@ -118,8 +113,6 @@ public function getConfigAsXml($type)
118113
*
119114
* @param string $type Widget type
120115
* @return \Magento\Framework\Object
121-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
122-
* @SuppressWarnings(PHPMD.NPathComplexity)
123116
*/
124117
public function getConfigAsObject($type)
125118
{
@@ -129,54 +122,92 @@ public function getConfigAsObject($type)
129122
if ($widget === null) {
130123
return $object;
131124
}
132-
$widget = $this->_getAsCanonicalArray($widget);
125+
$widget = $this->getAsCanonicalArray($widget);
133126

134127
// Save all nodes to object data
135128
$object->setType($type);
136129
$object->setData($widget);
137130

138131
// Correct widget parameters and convert its data to objects
132+
$newParams = $this->prepareWidgetParameters($object);
133+
$object->setData('parameters', $newParams);
134+
135+
return $object;
136+
}
137+
138+
/**
139+
* Prepare widget parameters
140+
*
141+
* @param \Magento\Framework\Object $object
142+
* @return array
143+
*/
144+
protected function prepareWidgetParameters(\Magento\Framework\Object $object)
145+
{
139146
$params = $object->getData('parameters');
140147
$newParams = [];
141148
if (is_array($params)) {
142149
$sortOrder = 0;
143150
foreach ($params as $key => $data) {
144151
if (is_array($data)) {
145-
$data['key'] = $key;
146-
$data['sort_order'] = isset($data['sort_order']) ? (int)$data['sort_order'] : $sortOrder;
147-
148-
// prepare values (for drop-dawns) specified directly in configuration
149-
$values = [];
150-
if (isset($data['values']) && is_array($data['values'])) {
151-
foreach ($data['values'] as $value) {
152-
if (isset($value['label']) && isset($value['value'])) {
153-
$values[] = $value;
154-
}
155-
}
156-
}
157-
$data['values'] = $values;
158-
159-
// prepare helper block object
160-
if (isset($data['helper_block'])) {
161-
$helper = new \Magento\Framework\Object();
162-
if (isset($data['helper_block']['data']) && is_array($data['helper_block']['data'])) {
163-
$helper->addData($data['helper_block']['data']);
164-
}
165-
if (isset($data['helper_block']['type'])) {
166-
$helper->setType($data['helper_block']['type']);
167-
}
168-
$data['helper_block'] = $helper;
169-
}
152+
$data = $this->prepareDropDownValues($data, $key, $sortOrder);
153+
$data = $this->prepareHelperBlock($data);
170154

171155
$newParams[$key] = new \Magento\Framework\Object($data);
172156
$sortOrder++;
173157
}
174158
}
175159
}
176-
uasort($newParams, [$this, '_sortParameters']);
177-
$object->setData('parameters', $newParams);
160+
uasort($newParams, [$this, 'sortParameters']);
178161

179-
return $object;
162+
return $newParams;
163+
}
164+
165+
/**
166+
* Prepare drop-down values
167+
*
168+
* @param array $data
169+
* @param string $key
170+
* @param int $sortOrder
171+
* @return array
172+
*/
173+
protected function prepareDropDownValues(array $data, $key, $sortOrder)
174+
{
175+
$data['key'] = $key;
176+
$data['sort_order'] = isset($data['sort_order']) ? (int)$data['sort_order'] : $sortOrder;
177+
178+
$values = [];
179+
if (isset($data['values']) && is_array($data['values'])) {
180+
foreach ($data['values'] as $value) {
181+
if (isset($value['label']) && isset($value['value'])) {
182+
$values[] = $value;
183+
}
184+
}
185+
}
186+
$data['values'] = $values;
187+
188+
return $data;
189+
}
190+
191+
/**
192+
* Prepare helper block
193+
*
194+
* @param array $data
195+
* @return array
196+
*/
197+
protected function prepareHelperBlock(array $data)
198+
{
199+
if (isset($data['helper_block'])) {
200+
$helper = new \Magento\Framework\Object();
201+
if (isset($data['helper_block']['data']) && is_array($data['helper_block']['data'])) {
202+
$helper->addData($data['helper_block']['data']);
203+
}
204+
if (isset($data['helper_block']['type'])) {
205+
$helper->setType($data['helper_block']['type']);
206+
}
207+
$data['helper_block'] = $helper;
208+
}
209+
210+
return $data;
180211
}
181212

182213
/**
@@ -188,7 +219,7 @@ public function getConfigAsObject($type)
188219
*/
189220
public function getWidgets($filters = [])
190221
{
191-
$widgets = $this->_dataStorage->get();
222+
$widgets = $this->dataStorage->get();
192223
$result = $widgets;
193224

194225
// filter widgets by params
@@ -219,7 +250,7 @@ public function getWidgets($filters = [])
219250
*/
220251
public function getWidgetsArray($filters = [])
221252
{
222-
if (empty($this->_widgetsArray)) {
253+
if (empty($this->widgetsArray)) {
223254
$result = [];
224255
foreach ($this->getWidgets($filters) as $code => $widget) {
225256
$result[$widget['name']] = [
@@ -229,10 +260,10 @@ public function getWidgetsArray($filters = [])
229260
'description' => __((string)$widget['description']),
230261
];
231262
}
232-
usort($result, [$this, "_sortWidgets"]);
233-
$this->_widgetsArray = $result;
263+
usort($result, [$this, "sortWidgets"]);
264+
$this->widgetsArray = $result;
234265
}
235-
return $this->_widgetsArray;
266+
return $this->widgetsArray;
236267
}
237268

238269
/**
@@ -274,9 +305,9 @@ public function getWidgetDeclaration($type, $params = [], $asIs = true)
274305

275306
$html = sprintf(
276307
'<img id="%s" src="%s" title="%s">',
277-
$this->_idEncode($directive),
308+
$this->idEncode($directive),
278309
$this->getPlaceholderImageUrl($type),
279-
$this->_escaper->escapeUrl($directive)
310+
$this->escaper->escapeUrl($directive)
280311
);
281312
return $html;
282313
}
@@ -295,13 +326,13 @@ public function getPlaceholderImageUrl($type)
295326
$placeholder = (string)$widget['placeholder_image'];
296327
}
297328
if ($placeholder) {
298-
$asset = $this->_assetRepo->createAsset($placeholder);
299-
$placeholder = $this->_assetSource->getFile($asset);
329+
$asset = $this->assetRepo->createAsset($placeholder);
330+
$placeholder = $this->assetSource->getFile($asset);
300331
if ($placeholder) {
301332
return $asset->getUrl();
302333
}
303334
}
304-
return $this->_assetRepo->getUrl('Magento_Widget::placeholder.gif');
335+
return $this->assetRepo->getUrl('Magento_Widget::placeholder.gif');
305336
}
306337

307338
/**
@@ -333,7 +364,7 @@ public function getPlaceholderImageUrls()
333364
* @param array $inputArray
334365
* @return array
335366
*/
336-
protected function _getAsCanonicalArray($inputArray)
367+
protected function getAsCanonicalArray($inputArray)
337368
{
338369
if (array_key_exists('@', $inputArray)) {
339370
unset($inputArray['@']);
@@ -342,7 +373,7 @@ protected function _getAsCanonicalArray($inputArray)
342373
if (!is_array($value)) {
343374
continue;
344375
}
345-
$inputArray[$key] = $this->_getAsCanonicalArray($value);
376+
$inputArray[$key] = $this->getAsCanonicalArray($value);
346377
}
347378
return $inputArray;
348379
}
@@ -353,7 +384,7 @@ protected function _getAsCanonicalArray($inputArray)
353384
* @param string $string
354385
* @return string
355386
*/
356-
protected function _idEncode($string)
387+
protected function idEncode($string)
357388
{
358389
return strtr(base64_encode($string), '+/=', ':_-');
359390
}
@@ -365,7 +396,7 @@ protected function _idEncode($string)
365396
* @param array $secondElement
366397
* @return bool
367398
*/
368-
protected function _sortWidgets($firstElement, $secondElement)
399+
protected function sortWidgets($firstElement, $secondElement)
369400
{
370401
return strcmp($firstElement["name"], $secondElement["name"]);
371402
}
@@ -377,7 +408,7 @@ protected function _sortWidgets($firstElement, $secondElement)
377408
* @param \Magento\Framework\Object $secondElement
378409
* @return int
379410
*/
380-
protected function _sortParameters($firstElement, $secondElement)
411+
protected function sortParameters($firstElement, $secondElement)
381412
{
382413
$aOrder = (int)$firstElement->getData('sort_order');
383414
$bOrder = (int)$secondElement->getData('sort_order');

0 commit comments

Comments
 (0)