Change NewsletterCoupon::cleanupSensitiveData signature

This change allows passing body array and retuning body array
so that NewsletterEntity instance doesn't need to get passed
around

[MAILPOET-4678]
This commit is contained in:
Sam Najian
2023-01-23 12:09:43 +01:00
committed by Aschepikov
parent 59d91ff7ef
commit 9a2d4e83b2
3 changed files with 16 additions and 19 deletions

View File

@@ -2,28 +2,24 @@
namespace MailPoet\Newsletter;
use MailPoet\Entities\NewsletterEntity;
use MailPoet\Newsletter\Renderer\Blocks\Coupon;
class NewsletterCoupon {
public function cleanupSensitiveData(NewsletterEntity $newsletter): NewsletterEntity {
$body = $newsletter->getBody();
if (!is_array($body) || empty($body['content'])) {
return $newsletter;
public function cleanupBodySensitiveData(array $newsletterBody): array {
if (!is_array($newsletterBody) || empty($newsletterBody['content'])) {
return $newsletterBody;
}
$cleanBlocks = $this->cleanupCouponBlocks($body['content']['blocks']);
$updatedBody = array_merge(
$body,
$cleanBlocks = $this->cleanupCouponBlocks($newsletterBody['content']['blocks']);
return array_merge(
$newsletterBody,
[
'content' => array_merge(
$body['content'],
$newsletterBody['content'],
['blocks' => $cleanBlocks]
),
]
);
$newsletter->setBody($updatedBody);
return $newsletter;
}
private function cleanupCouponBlocks(array &$blocks): array {

View File

@@ -187,7 +187,10 @@ class NewsletterSaveController {
// reset sent at date
$duplicate->setSentAt(null);
$duplicate = $this->newsletterCoupon->cleanupSensitiveData($duplicate);
$body = $duplicate->getBody();
if ($body) {
$duplicate->setBody($this->newsletterCoupon->cleanupBodySensitiveData($body));
}
$this->newslettersRepository->persist($duplicate);
$this->newslettersRepository->flush();

View File

@@ -37,12 +37,10 @@ class NewsletterCouponTest extends \MailPoetUnitTest {
'code' => 'asdasjdkkjaskljdasd',
],
];
$newsletter->setBody(['content' => ['blocks' => $blocks]]);
$result = $newsletterCoupon->cleanupSensitiveData($newsletter);
$body = (array)$result->getBody();
$updatedBody = $newsletterCoupon->cleanupBodySensitiveData(['content' => ['blocks' => $blocks]]);
$cleanBlock = ['type' => Coupon::TYPE, 'code' => Coupon::CODE_PLACEHOLDER];
expect($body['content']['blocks'][0]['blocks'][0])->equals($cleanBlock);
expect($body['content']['blocks'][0]['blocks'][1]['blocks'][0])->equals($cleanBlock);
expect($body['content']['blocks'][1])->equals($cleanBlock);
expect($updatedBody['content']['blocks'][0]['blocks'][0])->equals($cleanBlock);
expect($updatedBody['content']['blocks'][0]['blocks'][1]['blocks'][0])->equals($cleanBlock);
expect($updatedBody['content']['blocks'][1])->equals($cleanBlock);
}
}