Add comments

[MAILPOET-5555]
This commit is contained in:
David Remer
2023-09-05 09:34:39 +03:00
committed by Aschepikov
parent 79999f53c3
commit 974559876b

View File

@@ -81,6 +81,7 @@ class SubscriberSegmentRepository extends Repository {
} }
public function resetSubscriptions(SubscriberEntity $subscriber, array $segments): void { public function resetSubscriptions(SubscriberEntity $subscriber, array $segments): void {
// Already existing subscriptions are stored in $existingSegments. Their IDs in $existingSegmentIds.
$existingSegments = array_values(array_filter(array_map( $existingSegments = array_values(array_filter(array_map(
function(SubscriberSegmentEntity $subscriberSegmentEntity): ?SegmentEntity { function(SubscriberSegmentEntity $subscriberSegmentEntity): ?SegmentEntity {
return $subscriberSegmentEntity->getSegment(); return $subscriberSegmentEntity->getSegment();
@@ -93,23 +94,33 @@ class SubscriberSegmentRepository extends Repository {
}, },
$existingSegments $existingSegments
); );
// $segmentIds are the IDs of the segments we want the user to be subscribed to.
$segmentIds = array_map( $segmentIds = array_map(
function(SegmentEntity $segment): int { function(SegmentEntity $segment): int {
return $segment->getId() ?? 0; return $segment->getId() ?? 0;
}, },
$segments $segments
); );
// $unsubscribedSegments are the segment IDs to which we need to unsubscribe.
$unsubscribedSegments = array_diff($existingSegmentIds, $segmentIds); $unsubscribedSegments = array_diff($existingSegmentIds, $segmentIds);
// $newlySubscribedSegments are the segment IDs to which we need to newly subscribe.
$newlySubscribedSegments = array_diff($segmentIds, $existingSegmentIds); $newlySubscribedSegments = array_diff($segmentIds, $existingSegmentIds);
if (!$newlySubscribedSegments && !$unsubscribedSegments) { if (!$newlySubscribedSegments && !$unsubscribedSegments) {
return; return;
} }
// The segments we need to unsubscribe.
$unsubscribe = array_filter( $unsubscribe = array_filter(
$existingSegments, $existingSegments,
function(SegmentEntity $segment) use ($unsubscribedSegments): bool { function(SegmentEntity $segment) use ($unsubscribedSegments): bool {
return in_array($segment->getId(), $unsubscribedSegments); return in_array($segment->getId(), $unsubscribedSegments);
} }
); );
// The segments we need to newly subscribe.
$subscribe = array_filter( $subscribe = array_filter(
$segments, $segments,
function(SegmentEntity $segment) use ($newlySubscribedSegments): bool { function(SegmentEntity $segment) use ($newlySubscribedSegments): bool {