Skip to content

Commit 5fe6f36

Browse files
author
Denys Rudchenko
committed
MAGETWO-35744: [MX] [Nord] Code coverage - Sprint 20
- MAGETWO-36953: Decrease CRAP of Magento\Newsletter\Block\Adminhtml\Queue\Preview::_toHtml
1 parent fd1e1f9 commit 5fe6f36

File tree

2 files changed

+35
-64
lines changed

2 files changed

+35
-64
lines changed

app/code/Magento/Newsletter/Block/Adminhtml/Queue/Preview.php

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,18 @@
1111
*/
1212
namespace Magento\Newsletter\Block\Adminhtml\Queue;
1313

14-
class Preview extends \Magento\Backend\Block\Widget
14+
class Preview extends \Magento\Newsletter\Block\Adminhtml\Template\Preview
1515
{
1616
/**
17-
* @var \Magento\Newsletter\Model\TemplateFactory
17+
* {@inheritdoc}
1818
*/
19-
protected $_templateFactory;
19+
protected $profilerName = "newsletter_queue_proccessing";
2020

2121
/**
2222
* @var \Magento\Newsletter\Model\QueueFactory
2323
*/
2424
protected $_queueFactory;
2525

26-
/**
27-
* @var \Magento\Newsletter\Model\SubscriberFactory
28-
*/
29-
protected $_subscriberFactory;
30-
3126
/**
3227
* @param \Magento\Backend\Block\Template\Context $context
3328
* @param \Magento\Newsletter\Model\TemplateFactory $templateFactory
@@ -42,64 +37,22 @@ public function __construct(
4237
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
4338
array $data = []
4439
) {
45-
$this->_templateFactory = $templateFactory;
4640
$this->_queueFactory = $queueFactory;
47-
$this->_subscriberFactory = $subscriberFactory;
48-
parent::__construct($context, $data);
41+
parent::__construct($context, $templateFactory, $subscriberFactory, $data);
4942
}
5043

5144
/**
52-
* Get html code
53-
*
54-
* @return string
45+
* @param \Magento\Newsletter\Model\Template $template
46+
* @param string $id
47+
* @return $this
5548
*/
56-
protected function _toHtml()
49+
protected function loadTemplate(\Magento\Newsletter\Model\Template $template, $id)
5750
{
58-
/* @var $template \Magento\Newsletter\Model\Template */
59-
$template = $this->_templateFactory->create();
60-
61-
if ($id = (int)$this->getRequest()->getParam('id')) {
62-
$queue = $this->_queueFactory->create()->load($id);
63-
$template->setTemplateType($queue->getNewsletterType());
64-
$template->setTemplateText($queue->getNewsletterText());
65-
$template->setTemplateStyles($queue->getNewsletterStyles());
66-
} else {
67-
$template->setTemplateType($this->getRequest()->getParam('type'));
68-
$template->setTemplateText($this->getRequest()->getParam('text'));
69-
$template->setTemplateStyles($this->getRequest()->getParam('styles'));
70-
}
71-
72-
$storeId = (int)$this->getRequest()->getParam('store_id');
73-
if (!$storeId) {
74-
$defaultStore = $this->_storeManager->getDefaultStoreView();
75-
if (!$defaultStore) {
76-
$allStores = $this->_storeManager->getStores();
77-
if (isset($allStores[0])) {
78-
$defaultStore = $allStores[0];
79-
}
80-
}
81-
$storeId = $defaultStore ? $defaultStore->getId() : null;
82-
}
83-
84-
\Magento\Framework\Profiler::start("newsletter_queue_proccessing");
85-
$vars = [];
86-
87-
$vars['subscriber'] = $this->_subscriberFactory->create();
88-
89-
$template->emulateDesign($storeId);
90-
$templateProcessed = $this->_appState->emulateAreaCode(
91-
\Magento\Newsletter\Model\Template::DEFAULT_DESIGN_AREA,
92-
[$template, 'getProcessedTemplate'],
93-
[$vars, true]
94-
);
95-
$template->revertDesign();
96-
97-
if ($template->isPlain()) {
98-
$templateProcessed = "<pre>" . htmlspecialchars($templateProcessed) . "</pre>";
99-
}
100-
101-
\Magento\Framework\Profiler::stop("newsletter_queue_proccessing");
102-
103-
return $templateProcessed;
51+
/** @var \Magento\Newsletter\Model\Queue $queue */
52+
$queue = $this->_queueFactory->create()->load($id);
53+
$template->setTemplateType($queue->getNewsletterType());
54+
$template->setTemplateText($queue->getNewsletterText());
55+
$template->setTemplateStyles($queue->getNewsletterStyles());
56+
return $this;
10457
}
10558
}

app/code/Magento/Newsletter/Block/Adminhtml/Template/Preview.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111

1212
class Preview extends \Magento\Backend\Block\Widget
1313
{
14+
/**
15+
* Name for profiler
16+
*
17+
* @var string
18+
*/
19+
protected $profilerName = "newsletter_template_proccessing";
20+
1421
/**
1522
* @var \Magento\Newsletter\Model\TemplateFactory
1623
*/
@@ -49,14 +56,14 @@ protected function _toHtml()
4956
$template = $this->_templateFactory->create();
5057

5158
if ($id = (int)$this->getRequest()->getParam('id')) {
52-
$template->load($id);
59+
$this->loadTemplate($template, $id);
5360
} else {
5461
$template->setTemplateType($this->getRequest()->getParam('type'));
5562
$template->setTemplateText($this->getRequest()->getParam('text'));
5663
$template->setTemplateStyles($this->getRequest()->getParam('styles'));
5764
}
5865

59-
\Magento\Framework\Profiler::start("newsletter_template_proccessing");
66+
\Magento\Framework\Profiler::start($this->profilerName);
6067
$vars = [];
6168

6269
$vars['subscriber'] = $this->_subscriberFactory->create();
@@ -76,7 +83,7 @@ protected function _toHtml()
7683
$templateProcessed = "<pre>" . htmlspecialchars($templateProcessed) . "</pre>";
7784
}
7885

79-
\Magento\Framework\Profiler::stop("newsletter_template_proccessing");
86+
\Magento\Framework\Profiler::stop($this->profilerName);
8087

8188
return $templateProcessed;
8289
}
@@ -103,4 +110,15 @@ protected function getStoreId()
103110

104111
return $defaultStore ? $defaultStore->getId() : null;
105112
}
113+
114+
/**
115+
* @param \Magento\Newsletter\Model\Template $template
116+
* @param string $id
117+
* @return $this
118+
*/
119+
protected function loadTemplate(\Magento\Newsletter\Model\Template $template, $id)
120+
{
121+
$template->load($id);
122+
return $this;
123+
}
106124
}

0 commit comments

Comments
 (0)