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

View File

@@ -187,7 +187,10 @@ class NewsletterSaveController {
// reset sent at date // reset sent at date
$duplicate->setSentAt(null); $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->persist($duplicate);
$this->newslettersRepository->flush(); $this->newslettersRepository->flush();

View File

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