-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add logging to contact us form #9343
Changes from 2 commits
127a134
e25dbb2
b69ee25
cd0a6e1
4fe72fb
0be49aa
060e2e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
use Magento\Framework\Controller\Result\Redirect; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\HTTP\PhpEnvironment\Request; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class Post extends \Magento\Contact\Controller\Index | ||
{ | ||
|
@@ -32,22 +33,30 @@ class Post extends \Magento\Contact\Controller\Index | |
*/ | ||
private $mail; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @param Context $context | ||
* @param ConfigInterface $contactsConfig | ||
* @param MailInterface $mail | ||
* @param DataPersistorInterface $dataPersistor | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct( | ||
Context $context, | ||
ConfigInterface $contactsConfig, | ||
MailInterface $mail, | ||
DataPersistorInterface $dataPersistor | ||
DataPersistorInterface $dataPersistor, | ||
LoggerInterface $logger | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to backwards compatibility policy we can not add a new required argument to the constructor. public function __construct(\NewDependencyInterface $newDependency = null) {
$this->newDependency = $newDependency ?: \Magento\Framework\App\ObjectManager::getInstance()->get(\NewDependencyInterface::class);
}
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instantiate the object directly with object manager? My understanding is that is a big no no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regardless, updated. Would like clarification on when it is ok to use object manager. Maybe only in core code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right, using ObjectManager directly is indeed a "big no no". However, some existing extension or customization may possibly extend this class, thus, by changing the constructor signature you would break it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Such direct usages of the ObjectManager should be refactored eventually, but that may only happen we module version is bumped. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, thanks for the clarification! |
||
) { | ||
parent::__construct($context, $contactsConfig); | ||
$this->context = $context; | ||
$this->mail = $mail; | ||
$this->dataPersistor = $dataPersistor; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
|
@@ -70,8 +79,10 @@ public function execute() | |
$this->messageManager->addErrorMessage($e->getMessage()); | ||
$this->getDataPersistor()->set('contact_us', $this->getRequest()->getParams()); | ||
} catch (\Exception $e) { | ||
$this->logger->addError('An error occurred processing a posted message to contact us in Controller\\Index\\Post: ' . $e->getMessage()); | ||
$this->logger->critical($e); | ||
$this->messageManager->addErrorMessage( | ||
__('We can\'t process your request right now. Sorry, that\'s all we know.') | ||
__('An error occurred while processing your form. Please try again later.') | ||
); | ||
$this->getDataPersistor()->set('contact_us', $this->getRequest()->getParams()); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
current parameter should be optional as well.
$dataPersistor = null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reasoning behind this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed. I'm concerned about the future look of M2 if these kind of changes continue though. Is the plan just to have optionally parameterized constructors on every class with direct calls to object manager?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have next requirements dictated by Backward Compatibility policy:
Add the new optional parameter to the constructor at the end of arguments list.
In the constructor body, if new dependency is not provided (value of introduced argument is null) fetch the dependency using
Magento\Framework\App\ObjectionManager::getInstance().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JamesonNetworks oh sorry, my fault
I didn't notice that this parameter has already been there before.
DataPersistorInterface $dataPersistor
Sorry, for misleading you.
I was wrong. No need to make it optional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rgr, no worries, corrected and pushed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not confused about what you are doing, I'm confused as to why. It seems to me you would just mark the PRs that change the constructors and tag them to be released in releases that have the potential to break compatibility. Instead, we're building in tech debt here and expecting a maintainer to come in behind and remove these problems at the time of releases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding an optional parameter to the constructor doesn't bring Backward Incompatible changes. Because code will work according to its contract nevertheless dependency was passed or not (in this case we will instantiate one with ObjectManager::getInstance static call)