Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chuncked Message Loop #308

Open
jack-2729 opened this issue Oct 27, 2022 · 0 comments
Open

Chuncked Message Loop #308

jack-2729 opened this issue Oct 27, 2022 · 0 comments

Comments

@jack-2729
Copy link

Describe the bug
When the messages are getting by chunked function and one of this has problem the counter used in the function is wrong and the while condition looped

Code to Reproduce
The troubling code section which produces the reported bug.

        /** @var Webklex\PHPIMAP\ClientManager $cm */
        $client_manager = new ClientManager($options = []);

        /** @var \Webklex\PHPIMAP\Client $client */
        $client = $client_manager->make([
            'host' => "outlook.office365.com",
            'port' => 993,
            'encryption' => "ssl",
            'validate_cert' => true,
            'username' => "username",
            'password' => "access_token",
            'protocol' => 'imap',
            'authentication' => "oauth"
        ]);

        $client->connect();

        if (!isset($client) || !$client->isConnected()) {
            throw new \Exception("Cannot connect IMAP service");
        }

        $client->checkConnection();

        /** @var Webklex\PHPIMAP\Folder $folder*/
        $folder = $client->getFolderByName("INBOX");
        
        /** @var string $date */
        $date = (new DateTime())->sub(DateInterval::createFromDateString('1 day') )->format('d-m-Y');
        
        $query = $folder->query()->setFetchBody(false)->setFetchFlags(false)->since( $date)->all();
        
        /** @var \Webklex\PHPIMAP\Support\MessageCollection $messages */
        $messages = $query->softFail()->chunked(
			/**
			* Chunk callback
			* @param  \Webklex\PHPIMAP\Support\MessageCollection $messages
			* @param int $chunk
			*/
			function($messages, $chunk){	                		
				$messages->each(
					/**
					* Message callback
					* @param \Webklex\PHPIMAP\Message $message
					*/
					function($message_fetch){
                                              // do something
                                        }
                                );
                        }
        );

Expected behavior
Chuncked function doesn't loop

Possibile solution

public function chunked(callable $callback, int $chunk_size = 10, int $start_chunk = 1) {
        $available_messages = $this->search();
        if (($available_messages_count = $available_messages->count()) > 0) {
            $old_limit = $this->limit;
            $old_page = $this->page;

            $this->limit = $chunk_size;
            $this->page = $start_chunk;
            $handled_messages_count = 0;
            do {
                $messages = $this->populate($available_messages);
                $handled_messages_count += $chunk_size;
                $callback($messages, $this->page);
                $this->page++;
            } while ($handled_messages_count < $available_messages_count);
            $this->limit = $old_limit;
            $this->page = $old_page;
        }
    }

Desktop / Server (please complete the following information):

  • OS: RockyLinux
  • PHP: 8.0
  • Version: 4.0.2
  • Provider: Outlook
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant