Fix bug: Ensure data['tags'] is normalized

[MAILPOET-5515]
This commit is contained in:
David Remer
2023-09-25 09:09:39 +03:00
committed by Aschepikov
parent f963332506
commit a1e59d5f31

View File

@@ -304,16 +304,32 @@ class SubscriberSaveController {
private function updateTags(array $data, SubscriberEntity $subscriber): void {
$removedTags = [];
/**
* $data['tags'] is either an array of arrays containing name, id etc. of the tag or an array of strings - the names
* of the tag.
*
* Therefore we map it to be only an array of strings, containing the names of the tag.
*/
$tags = array_map(
function($tag): string {
if (is_array($tag)) {
return array_key_exists('name', $tag) ? (string)$tag['name'] : '';
}
return (string)$tag;
},
(array)$data['tags']
);
foreach ($subscriber->getSubscriberTags() as $subscriberTag) {
$tag = $subscriberTag->getTag();
if (!$tag || !in_array($tag->getName(), $data['tags'], true)) {
if (!$tag || !in_array($tag->getName(), $tags, true)) {
$subscriber->getSubscriberTags()->removeElement($subscriberTag);
$removedTags[] = $subscriberTag;
}
}
$newlyAddedTags = [];
foreach ($data['tags'] as $tagName) {
foreach ($tags as $tagName) {
$tag = $this->tagRepository->createOrUpdate(['name' => $tagName]);
$subscriberTag = $subscriber->getSubscriberTag($tag);
if (!$subscriberTag) {