Skip to content

Commit

Permalink
Merge pull request #11 from CPS-IT/feature/file-iterator
Browse files Browse the repository at this point in the history
[!!!][FEATURE] Resolve dependency between iterator and transport
  • Loading branch information
eliashaeussler authored Mar 7, 2024
2 parents e0ae314 + 0c900f0 commit 27f9455
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,44 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace CPSIT\Typo3Mailqueue\Mail\Iterator;
namespace CPSIT\Typo3Mailqueue\Iterator;

use CPSIT\Typo3Mailqueue\Mail;
use FilterIterator;
use Iterator;
use SplFileInfo;
use Traversable;

/**
* FileIterator
* LimitedFileIterator
*
* @author Elias Häußler <[email protected]>
* @license GPL-2.0-or-later
*
* @template-extends FilterIterator<int, SplFileInfo, Traversable<SplFileInfo>>
* @extends FilterIterator<int, SplFileInfo, Traversable<SplFileInfo>>
*/
final class FileIterator extends FilterIterator
final class LimitedFileIterator extends FilterIterator
{
/**
* @param list<string> $acceptedSuffixes
*/
public function __construct(
Iterator $iterator,
private readonly array $acceptedSuffixes,
) {
parent::__construct($iterator);
}

public function accept(): bool
{
$file = $this->getInnerIterator()->current();
$path = $file->getRealPath();

return str_ends_with($path, Mail\Transport\QueueableFileTransport::FILE_SUFFIX_QUEUED)
|| str_ends_with($path, Mail\Transport\QueueableFileTransport::FILE_SUFFIX_SENDING)
;
foreach ($this->acceptedSuffixes as $suffix) {
if (str_ends_with($path, $suffix)) {
return true;
}
}

return false;
}
}
15 changes: 11 additions & 4 deletions Classes/Mail/Transport/QueueableFileTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use CPSIT\Typo3Mailqueue\Enums;
use CPSIT\Typo3Mailqueue\Exception;
use CPSIT\Typo3Mailqueue\Iterator;
use CPSIT\Typo3Mailqueue\Mail;
use DateTimeImmutable;
use DirectoryIterator;
Expand All @@ -44,9 +45,9 @@
*/
final class QueueableFileTransport extends Core\Mail\FileSpool implements RecoverableTransport
{
public const FILE_SUFFIX_QUEUED = '.message';
public const FILE_SUFFIX_SENDING = '.message.sending';
public const FILE_SUFFIX_FAILURE_DATA = '.message.failure';
private const FILE_SUFFIX_QUEUED = '.message';
private const FILE_SUFFIX_SENDING = '.message.sending';
private const FILE_SUFFIX_FAILURE_DATA = '.message.failure';

private readonly Core\Context\Context $context;

Expand Down Expand Up @@ -185,7 +186,13 @@ private function flagFailedTransport(string $file, Mailer\Exception\TransportExc
*/
private function initializeQueueFromFilePath(): Generator
{
$iterator = new Mail\Iterator\FileIterator(new DirectoryIterator($this->path));
$iterator = new Iterator\LimitedFileIterator(
new DirectoryIterator($this->path),
[
self::FILE_SUFFIX_QUEUED,
self::FILE_SUFFIX_SENDING,
],
);

foreach ($iterator as $file) {
yield $this->restoreItem($file);
Expand Down

0 comments on commit 27f9455

Please sign in to comment.