Skip to content

Commit

Permalink
Merge pull request #12 from CPS-IT/task/regex
Browse files Browse the repository at this point in the history
[TASK] Avoid regex when building file name variants
  • Loading branch information
eliashaeussler authored Mar 7, 2024
2 parents 27f9455 + 794ffa4 commit 120fa06
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions Classes/Mail/Transport/QueueableFileTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ public function enqueue(Mime\RawMessage $message, ?Mailer\Envelope $envelope = n
public function dequeue(Mail\Queue\MailQueueItem $item, Mailer\Transport\TransportInterface $transport): bool
{
$path = $this->path . DIRECTORY_SEPARATOR . $item->id;
$sendingPath = $this->getItemVariant($path, self::FILE_SUFFIX_SENDING);
$failurePath = $this->getItemVariant($path, self::FILE_SUFFIX_FAILURE_DATA);
$sendingPath = $this->getFileVariant($path, self::FILE_SUFFIX_SENDING);
$failurePath = $this->getFileVariant($path, self::FILE_SUFFIX_FAILURE_DATA);

// We try a rename, it's an atomic operation, and avoid locking the file
if ($path !== $sendingPath && !rename($path, $sendingPath)) {
Expand Down Expand Up @@ -176,7 +176,7 @@ public function getMailQueue(): Mail\Queue\MailQueue
private function flagFailedTransport(string $file, Mailer\Exception\TransportExceptionInterface $exception): void
{
$failure = Mail\TransportFailure::fromException($exception);
$failurePath = $this->getItemVariant($file, self::FILE_SUFFIX_FAILURE_DATA);
$failurePath = $this->getFileVariant($file, self::FILE_SUFFIX_FAILURE_DATA);

file_put_contents($failurePath, serialize($failure));
}
Expand Down Expand Up @@ -248,7 +248,7 @@ private function restoreItem(SplFileInfo $file): Mail\Queue\MailQueueItem

private function findFailureMetadata(string $file): ?Mail\TransportFailure
{
$failurePath = $this->getItemVariant($file, self::FILE_SUFFIX_FAILURE_DATA);
$failurePath = $this->getFileVariant($file, self::FILE_SUFFIX_FAILURE_DATA);

try {
return Mail\TransportFailure::fromFile($failurePath);
Expand All @@ -257,7 +257,7 @@ private function findFailureMetadata(string $file): ?Mail\TransportFailure
}
}

private function getItemVariant(string $file, string $suffix): string
private function getFileVariant(string $file, string $suffix): string
{
$variants = array_diff(
[
Expand All @@ -268,10 +268,12 @@ private function getItemVariant(string $file, string $suffix): string
[$suffix],
);

return (string)preg_replace(
sprintf('/(%s)$/', implode('|', array_map('preg_quote', $variants))),
$suffix,
$file,
);
foreach ($variants as $variant) {
if (str_ends_with($file, $variant)) {
return substr_replace($file, $suffix, -mb_strlen($variant));
}
}

return $file;
}
}

0 comments on commit 120fa06

Please sign in to comment.