Skip to content

Commit f71f6da

Browse files
committed
Folder status retrieval fixed and missing methods added
1 parent 27d911c commit f71f6da

File tree

4 files changed

+58
-42
lines changed

4 files changed

+58
-42
lines changed

src/Connection/Protocols/ImapProtocol.php

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -536,25 +536,6 @@ public function getCapabilities(): Response {
536536
return $response->setResult($response->validatedData()[0]);
537537
}
538538

539-
/**
540-
* Get an array of available STATUS items
541-
*
542-
* @return Response list of STATUS items
543-
*
544-
* @throws ImapBadRequestException
545-
* @throws ImapServerErrorException
546-
* @throws RuntimeException
547-
* @throws ResponseException
548-
*/
549-
public function getStatus(): Response {
550-
$s = implode(" ", $properties);
551-
$response = $this->requestAndResponse('STATUS', array($this->escapeString($folder), "(" . $s . ")"));
552-
553-
if (!$response->getResponse()) return $response;
554-
555-
return $response->setResult($response->validatedData()[0]);
556-
}
557-
558539
/**
559540
* Examine and select have the same response.
560541
* @param string $command can be 'EXAMINE' or 'SELECT'
@@ -628,6 +609,42 @@ public function examineFolder(string $folder = 'INBOX'): Response {
628609
return $this->examineOrSelect('EXAMINE', $folder);
629610
}
630611

612+
/**
613+
* Get the status of a given folder
614+
*
615+
* @param string $folder
616+
* @param string[] $arguments
617+
* @return Response list of STATUS items
618+
*
619+
* @throws ImapBadRequestException
620+
* @throws ImapServerErrorException
621+
* @throws ResponseException
622+
* @throws RuntimeException
623+
*/
624+
public function folderStatus(string $folder = 'INBOX', $arguments = ['MESSAGES', 'UNSEEN', 'RECENT', 'UIDNEXT', 'UIDVALIDITY']): Response {
625+
$response = $this->requestAndResponse('STATUS', [$this->escapeString($folder), $this->escapeList($arguments)], false);
626+
$data = $response->validatedData();
627+
628+
if (!isset($data[0]) || !isset($data[0][2])) {
629+
throw new RuntimeException("folder status could not be fetched");
630+
}
631+
632+
$result = [];
633+
$key = null;
634+
foreach($data[0][2] as $value) {
635+
if ($key === null) {
636+
$key = $value;
637+
} else {
638+
$result[$key] = (int)$value;
639+
$key = null;
640+
}
641+
}
642+
643+
$response->setResult($result);
644+
645+
return $response;
646+
}
647+
631648
/**
632649
* Fetch one or more items of one or more messages
633650
* @param array|string $items items to fetch [RFC822.HEADER, FLAGS, RFC822.TEXT, etc]

src/Connection/Protocols/LegacyProtocol.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,16 @@ public function examineFolder(string $folder = 'INBOX'): Response {
236236
});
237237
}
238238

239+
/**
240+
* Get the status of a given folder
241+
*
242+
* @return Response list of STATUS items
243+
* @throws MethodNotSupportedException
244+
*/
245+
public function folderStatus(string $folder = 'INBOX', $arguments = ['MESSAGES', 'UNSEEN', 'RECENT', 'UIDNEXT', 'UIDVALIDITY']): Response {
246+
throw new MethodNotSupportedException();
247+
}
248+
239249
/**
240250
* Fetch message content
241251
* @param int|array $uids

src/Connection/Protocols/ProtocolInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ public function selectFolder(string $folder = 'INBOX'): Response;
117117
*/
118118
public function examineFolder(string $folder = 'INBOX'): Response;
119119

120+
/**
121+
* Get the status of a given folder
122+
*
123+
* @return Response list of STATUS items
124+
*
125+
* @throws ImapBadRequestException
126+
* @throws ImapServerErrorException
127+
* @throws RuntimeException
128+
*/
129+
public function folderStatus(string $folder = 'INBOX', $arguments = ['MESSAGES', 'UNSEEN', 'RECENT', 'UIDNEXT', 'UIDVALIDITY']): Response;
130+
120131
/**
121132
* Fetch message headers
122133
* @param int|array $uids

src/Folder.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -491,28 +491,6 @@ public function idle(callable $callback, int $timeout = 300): void {
491491
}
492492
}
493493

494-
/**
495-
* Get the standard folder status items from STATUS command
496-
*
497-
* @return array
498-
* @throws ConnectionFailedException
499-
* @throws ImapbadRequestException
500-
* @throws ImapServerErrorException
501-
* @throws RuntimeException
502-
* @throws AuthFailedException
503-
* @throws ResponseException
504-
*/
505-
public function getFolderStatus(string $folder, array $properties): array {
506-
$status = $client->getConnection()->getStatus($folder->full_name, array('MESSAGES', 'UNSEEN', 'RECENT', 'UIDNEXT', 'UIDVALIDITY'))->validatedData();
507-
$c = count($status[2]);
508-
$s = array();
509-
for ($i = 0; $i < $c; $i++) {
510-
$a = $i++;
511-
$s[$status[2][$a]] = $status[2][$i];
512-
}
513-
return $s;
514-
}
515-
516494
/**
517495
* Get folder status information from the EXAMINE command
518496
*
@@ -525,7 +503,7 @@ public function getFolderStatus(string $folder, array $properties): array {
525503
* @throws ResponseException
526504
*/
527505
public function getStatus(): array {
528-
return $this->examine();
506+
return $this->client->getConnection()->folderStatus($this->path, ['MESSAGES', 'UNSEEN', 'RECENT', 'UIDNEXT', 'UIDVALIDITY'])->validatedData();
529507
}
530508

531509
/**

0 commit comments

Comments
 (0)