Skip to content

Commit c6f5f1a

Browse files
committed
corrections, add routine to get total email count
1 parent 60572c5 commit c6f5f1a

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

decode/MailReader.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,28 @@ public function readEmail($src = 'php://stdin')
274274
return $this->saved_files;
275275
}
276276

277+
public function getMessageCount(string $email)
278+
{
279+
$dbc = $this->pdo;
280+
if ($dbc instanceof \PDO) {
281+
$messages = $dbc->prepare("SELECT count(*) FROM emails WHERE toaddr=?");
282+
if (!is_bool($messages)) {
283+
$messages->execute([$email]);
284+
$count = $messages->fetchColumn();
285+
unset($messages);
286+
return $count;
287+
}
288+
}
289+
290+
return false;
291+
}
292+
277293
public function getMessages(string $email, int $offset = 0, int $max = 20)
278294
{
279295
$dbc = $this->pdo;
280296
if ($dbc instanceof \PDO) {
281297
$emails = $dbc->prepare("SELECT * FROM emails WHERE toaddr=:mail LIMIT :offset, :max");
282-
if (!is_bool($email)) {
298+
if (!is_bool($emails)) {
283299
$emails->bindValue(':mail', $email);
284300
$emails->bindValue(':offset', $offset, \PDO::PARAM_INT);
285301
$emails->bindValue(':max', $max, \PDO::PARAM_INT);
@@ -293,6 +309,8 @@ public function getMessages(string $email, int $offset = 0, int $max = 20)
293309
unset($emails);
294310
if (\count($records) > 0)
295311
return $records;
312+
313+
return 0;
296314
}
297315
}
298316

tests/MailReaderTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ public function testMailPipe()
5959
$this->assertNull($output);
6060

6161
$mr = new MailReader($this->handle, '..');
62-
$this->assertFalse($mr->getMessages('no@email.com'));
62+
$this->assertEquals(0, $mr->getMessages('no@email.com'));
63+
$this->assertEquals(0, $mr->getMessageCount('no@email.com'));
6364
$emails = $mr->getMessages('name@company2.com');
65+
$this->assertEquals(1, $mr->getMessageCount('name@company2.com'));
6466

6567
$emailID = $emails[0]->getId();
6668
$this->assertTrue(\is_int($emailID));

0 commit comments

Comments
 (0)