setConnection($connection) ->setAttachments($attachments) ->setUsername($username); } protected ConnectionInterface $connection; protected Attachments $attachments; protected string $username; public function getConnection(): ConnectionInterface { return $this->connection; } public function getAttachments(): Attachments { return $this->attachments; } public function getUsername(): string { return $this->username; } public function setConnection(ConnectionInterface $connection): Mailboxes { $this->connection = $connection; return $this; } public function setAttachments(Attachments $attachments): Mailboxes { $this->attachments = $attachments; return $this; } public function setUsername(string $username): Mailboxes { $this->username = $username; return $this; } protected array $mailboxes; public function getAll(): array { if (!isset($this->mailboxes)) { $this->mailboxes = $this->getConnection()->getMailboxes(); } return $this->mailboxes; } public function get(string $mailbox): MailboxInterface { if (!$this->getConnection()->hasMailbox($mailbox)) { throw new Invalid(); } return $this->getConnection()->getMailbox($mailbox); } // Messages protected function advanceIterator(Iterator $iterator, int $up_to): Iterator { for ($i = 0; $i < $up_to; $i ++) { $iterator->next(); } return $iterator; } public function getMessages(MailboxInterface $mailbox, int $start = 0, ?int $amount = null): Generator { if ($mailbox->count() === 0) { $this->getLogger()->notice('No mails found.'); throw new EmptyMailbox(); } $it = $mailbox->getIterator(); if ($amount === null) { $amount = $mailbox->count() - $start; } $it = $this->advanceIterator($it, $start); for ($i = $start; $i < min($start + $amount, $mailbox->count()); $i ++) { yield $it->key() => $it->current(); $it->next(); } } public function countValid(MailboxInterface $mailbox): int { $cnt = 0; foreach ($mailbox->getIterator() as $message) { if ($this->hasAttachments($message) and $this->validAttachments($message)) { $cnt ++; } } return $cnt; } /** * @param MailboxInterface $mailbox * @param int $uid * @return MessageInterface */ public function getMessage(MailboxInterface $mailbox, int $uid): MessageInterface { return $mailbox->getMessage($uid); } protected function validateAddress(array $to): bool { foreach ($to as $address) { if (strtolower($address->getAddress()) === strtolower($this->getUsername())) { return true; } } return false; } public function hasAttachments(MessageInterface $message): bool { return ($message->hasAttachments() and $this->validateAddress($message->getTo())); } protected function validateAttachment(AttachmentInterface $attachment): bool { return str_contains($attachment->getFilename(), '.pdf'); } public function validAttachments(MessageInterface $message): bool { foreach ($message->getAttachments() as $attachment) { if ($this->validateAttachment($attachment)) { return true; } } return false; } public function downloadAttachments(MessageInterface $message) { foreach ($message->getAttachments() as $attachment) { if ($this->validateAttachment($attachment)) { $attachment->getContent(); } } } }