php-imap icon indicating copy to clipboard operation
php-imap copied to clipboard

Chuncked Message Loop

Open jack-2729 opened this issue 3 years ago • 0 comments

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

jack-2729 avatar Oct 27 '22 09:10 jack-2729