Skip to content

Commit

Permalink
MAGETWO-65047: [GitHub][PR] Refactor contact module #8420
Browse files Browse the repository at this point in the history
  • Loading branch information
Magento CICD authored Feb 23, 2017
2 parents 0b25aec + b8b5a6b commit c5f2c4d
Show file tree
Hide file tree
Showing 21 changed files with 672 additions and 297 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function execute()
$this->attributeHelper->getStoreWebsiteId($storeId)
);
if (!$stockItemDo->getProductId()) {
$inventoryData[] = $productId;
$inventoryData['product_id'] = $productId;
}

$stockItemId = $stockItemDo->getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ switch ($type = $block->getType()) {

case 'crosssell':
/** @var \Magento\Catalog\Block\Product\ProductList\Crosssell $block */
if ($exist = $block->getItemCount()) {
if ($exist = count($block->getItems())) {
$type = 'crosssell';
$class = $type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ protected function _getAllowedCurrencies()
)
);
}
return $this->getData('groups/options/fields/allow/value');

return (array)$this->getData('groups/options/fields/allow/value');
}

/**
Expand Down
55 changes: 16 additions & 39 deletions app/code/Magento/Contact/Controller/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,74 +5,51 @@
*/
namespace Magento\Contact\Controller;

use Magento\Framework\Exception\NotFoundException;
use Magento\Contact\Model\ConfigInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\RequestInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\Exception\NotFoundException;

/**
* Contact index controller
* Contact module base controller
*/
abstract class Index extends \Magento\Framework\App\Action\Action
{
/**
* Recipient email config path
*/
const XML_PATH_EMAIL_RECIPIENT = 'contact/email/recipient_email';
const XML_PATH_EMAIL_RECIPIENT = ConfigInterface::XML_PATH_EMAIL_RECIPIENT;

/**
* Sender email config path
*/
const XML_PATH_EMAIL_SENDER = 'contact/email/sender_email_identity';
const XML_PATH_EMAIL_SENDER = ConfigInterface::XML_PATH_EMAIL_SENDER;

/**
* Email template config path
*/
const XML_PATH_EMAIL_TEMPLATE = 'contact/email/email_template';
const XML_PATH_EMAIL_TEMPLATE = ConfigInterface::XML_PATH_EMAIL_TEMPLATE;

/**
* Enabled config path
*/
const XML_PATH_ENABLED = 'contact/contact/enabled';

/**
* @var \Magento\Framework\Mail\Template\TransportBuilder
*/
protected $_transportBuilder;

/**
* @var \Magento\Framework\Translate\Inline\StateInterface
*/
protected $inlineTranslation;

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $scopeConfig;
const XML_PATH_ENABLED = ConfigInterface::XML_PATH_ENABLED;

/**
* @var \Magento\Store\Model\StoreManagerInterface
* @var ConfigInterface
*/
protected $storeManager;
private $contactsConfig;

/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
* @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param Context $context
* @param ConfigInterface $contactsConfig
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Store\Model\StoreManagerInterface $storeManager
Context $context,
ConfigInterface $contactsConfig
) {
parent::__construct($context);
$this->_transportBuilder = $transportBuilder;
$this->inlineTranslation = $inlineTranslation;
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
$this->contactsConfig = $contactsConfig;
}

/**
Expand All @@ -84,7 +61,7 @@ public function __construct(
*/
public function dispatch(RequestInterface $request)
{
if (!$this->scopeConfig->isSetFlag(self::XML_PATH_ENABLED, ScopeInterface::SCOPE_STORE)) {
if (!$this->contactsConfig->isEnabled()) {
throw new NotFoundException(__('Page not found.'));
}
return parent::dispatch($request);
Expand Down
141 changes: 87 additions & 54 deletions app/code/Magento/Contact/Controller/Index/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
*/
namespace Magento\Contact\Controller\Index;

use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Contact\Model\ConfigInterface;
use Magento\Contact\Model\MailInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\HTTP\PhpEnvironment\Request;

class Post extends \Magento\Contact\Controller\Index
{
Expand All @@ -16,75 +22,60 @@ class Post extends \Magento\Contact\Controller\Index
*/
private $dataPersistor;

/**
* @var Context
*/
private $context;

/**
* @var MailInterface
*/
private $mail;

/**
* @param Context $context
* @param ConfigInterface $contactsConfig
* @param MailInterface $mail
* @param DataPersistorInterface $dataPersistor
*/
public function __construct(
Context $context,
ConfigInterface $contactsConfig,
MailInterface $mail,
DataPersistorInterface $dataPersistor
) {
parent::__construct($context, $contactsConfig);
$this->context = $context;
$this->mail = $mail;
$this->dataPersistor = $dataPersistor;
}

/**
* Post user question
*
* @return void
* @throws \Exception
* @return Redirect
*/
public function execute()
{
$post = $this->getRequest()->getPostValue();
if (!$post) {
$this->_redirect('*/*/');
return;
if (!$this->isPostRequest()) {
return $this->resultRedirectFactory->create()->setPath('*/*/');
}

$this->inlineTranslation->suspend();
try {
$postObject = new \Magento\Framework\DataObject();
$postObject->setData($post);

$error = false;

if (!\Zend_Validate::is(trim($post['name']), 'NotEmpty')) {
$error = true;
}
if (!\Zend_Validate::is(trim($post['comment']), 'NotEmpty')) {
$error = true;
}
if (!\Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
$error = true;
}
if (\Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
$error = true;
}
if ($error) {
throw new \Exception();
}

$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
$transport = $this->_transportBuilder
->setTemplateIdentifier($this->scopeConfig->getValue(self::XML_PATH_EMAIL_TEMPLATE, $storeScope))
->setTemplateOptions(
[
'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
]
)
->setTemplateVars(['data' => $postObject])
->setFrom($this->scopeConfig->getValue(self::XML_PATH_EMAIL_SENDER, $storeScope))
->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
->setReplyTo($post['email'])
->getTransport();

$transport->sendMessage();
$this->inlineTranslation->resume();
$this->sendEmail($this->validatedParams());
$this->messageManager->addSuccess(
__('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')
);
$this->getDataPersistor()->clear('contact_us');
$this->_redirect('contact/index');
return;
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->getDataPersistor()->set('contact_us', $this->getRequest()->getParams());
} catch (\Exception $e) {
$this->inlineTranslation->resume();
$this->messageManager->addError(
$this->messageManager->addErrorMessage(
__('We can\'t process your request right now. Sorry, that\'s all we know.')
);
$this->getDataPersistor()->set('contact_us', $post);
$this->_redirect('contact/index');
return;
$this->getDataPersistor()->set('contact_us', $this->getRequest()->getParams());
}
return $this->resultRedirectFactory->create()->setPath('contact/index');
}

/**
Expand All @@ -101,4 +92,46 @@ private function getDataPersistor()

return $this->dataPersistor;
}

/**
* @param array $post Post data from contact form
* @return void
*/
private function sendEmail($post)
{
$this->mail->send($post['email'], ['data' => new \Magento\Framework\DataObject($post)]);
}

/**
* @return bool
*/
private function isPostRequest()
{
/** @var Request $request */
$request = $this->getRequest();
return !empty($request->getPostValue());
}

/**
* @return array
* @throws \Exception
*/
private function validatedParams()
{
$request = $this->getRequest();
if (trim($request->getParam('name')) === '') {
throw new LocalizedException(__('Name is missing'));
}
if (trim($request->getParam('comment')) === '') {
throw new LocalizedException(__('Comment is missing'));
}
if (false === \strpos($request->getParam('email'), '@')) {
throw new LocalizedException(__('Invalid email address'));
}
if (trim($request->getParam('hideit')) !== '') {
throw new \Exception();
}

return $request->getParams();
}
}
4 changes: 3 additions & 1 deletion app/code/Magento/Contact/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Magento\Contact\Helper;

use Magento\Contact\Model\ConfigInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Helper\View as CustomerViewHelper;
use Magento\Framework\App\Request\DataPersistorInterface;
Expand All @@ -16,7 +17,7 @@
*/
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
const XML_PATH_ENABLED = 'contact/contact/enabled';
const XML_PATH_ENABLED = ConfigInterface::XML_PATH_ENABLED;

/**
* Customer session
Expand Down Expand Up @@ -59,6 +60,7 @@ public function __construct(
* Check if enabled
*
* @return string|null
* @deprecated use \Magento\Contact\Api\ConfigInterface::isEnabled() instead
*/
public function isEnabled()
{
Expand Down
Loading

0 comments on commit c5f2c4d

Please sign in to comment.