Skip to content

Commit

Permalink
Merged PR 48049: Refactor getRequest() in EmailCapturePlugin
Browse files Browse the repository at this point in the history
## What's being changed

A plugin to a controller in the Email module.

## Why it's being changed

- Using `$this->getRequest()` in controllers is not allowed - we have to use `$context->getRequest()`.

## How to review / test this change

- Put a breakpoint in the plugin
- Start a chat
- Enter an email in the newsletter input
- After submit the email should be picked up in the plugin via an AJAX request

Related work items: #223644
  • Loading branch information
sta1r committed Sep 22, 2023
1 parent 3c59cbc commit c8e5b5a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
20 changes: 17 additions & 3 deletions Plugin/EmailcapturePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Dotdigitalgroup\Chat\Model\Config;
use Dotdigitalgroup\Chat\Model\Profile\UpdateChatProfile;
use Dotdigitalgroup\Email\Controller\Ajax\Emailcapture;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Stdlib\Cookie\CookieReaderInterface;

class EmailcapturePlugin
Expand All @@ -14,6 +17,11 @@ class EmailcapturePlugin
*/
private $chatProfile;

/**
* @var RequestInterface
*/
private $request;

/**
* @var CookieReaderInterface
*/
Expand All @@ -24,25 +32,31 @@ class EmailcapturePlugin
*
* @param UpdateChatProfile $chatProfile
* @param CookieReaderInterface $cookieReader
* @param Context $context
*/
public function __construct(
UpdateChatProfile $chatProfile,
CookieReaderInterface $cookieReader
CookieReaderInterface $cookieReader,
Context $context
) {
$this->chatProfile = $chatProfile;
$this->cookieReader = $cookieReader;
$this->request = $context->getRequest();
}

/**
* After email capture execute
*
* @param Emailcapture $emailcapture
* @param ResultInterface $result
*/
public function afterExecute(Emailcapture $emailcapture)
public function afterExecute(Emailcapture $emailcapture, $result)
{
// if a chat profile ID is present, update chat profile data
if ($chatProfileId = $this->cookieReader->getCookie(Config::COOKIE_CHAT_PROFILE, null)) {
$this->chatProfile->update($chatProfileId, $emailcapture->getRequest()->getParam('email'));
$this->chatProfile->update($chatProfileId, $this->request->getParam('email'));
}

return $result;
}
}
3 changes: 2 additions & 1 deletion Test/Integration/Controller/Ajax/EmailcaptureUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Dotdigitalgroup\Chat\Model\Config;
use Dotdigitalgroup\Chat\Model\Profile\UpdateChatProfile;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\TestFramework\TestCase\AbstractController;

class EmailcaptureUpdateTest extends AbstractController
Expand Down Expand Up @@ -32,7 +33,7 @@ public function testUpdateChatProfileCookie()
->method('update')
->with($profileId, $email);

$this->getRequest()->setParam('email', $email);
$this->getRequest()->setParam('email', $email)->setMethod(HttpRequest::METHOD_POST);

$this->dispatch('/connector/ajax/emailcapture');
}
Expand Down

0 comments on commit c8e5b5a

Please sign in to comment.