Skip to content

Commit 34830d9

Browse files
committed
Imap client timeout can be modified and read #186
1 parent 5be71b4 commit 34830d9

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/IMAP/Client.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Webklex\IMAP\Exceptions\ConnectionFailedException;
1616
use Webklex\IMAP\Exceptions\GetMessagesFailedException;
17+
use Webklex\IMAP\Exceptions\InvalidImapTimeoutTypeException;
1718
use Webklex\IMAP\Exceptions\MessageSearchValidationException;
1819
use Webklex\IMAP\Support\FolderCollection;
1920
use Webklex\IMAP\Support\MessageCollection;
@@ -115,6 +116,18 @@ class Client {
115116
*/
116117
protected $validConfigKeys = ['host', 'port', 'encryption', 'validate_cert', 'username', 'password','protocol'];
117118

119+
/**
120+
* All available timeout types
121+
*
122+
* @var array $timeout_type
123+
*/
124+
protected $timeout_type = [
125+
'IMAP_OPENTIMEOUT' => 1,
126+
'IMAP_READTIMEOUT' => 2,
127+
'IMAP_WRITETIMEOUT' => 3,
128+
'IMAP_CLOSETIMEOUT' => 4
129+
];
130+
118131
/**
119132
* Client constructor.
120133
*
@@ -569,4 +582,43 @@ public function checkCurrentMailbox() {
569582
$this->checkConnection();
570583
return imap_check($this->connection);
571584
}
585+
586+
/**
587+
* Set the imap timeout for a given operation type
588+
* @param $type
589+
* @param $timeout
590+
*
591+
* @return mixed
592+
* @throws InvalidImapTimeoutTypeException
593+
*/
594+
public function setTimeout($type, $timeout) {
595+
if(is_numeric($type)) {
596+
$type = (int) $type;
597+
}elseif (isset($this->timeout_type[$type])){
598+
$type = $this->timeout_type[$type];
599+
}else{
600+
throw new InvalidImapTimeoutTypeException("Invalid imap timeout type provided.");
601+
}
602+
603+
return imap_timeout($type, $timeout);
604+
}
605+
606+
/**
607+
* Get the timeout for a certain operation
608+
* @param $type
609+
*
610+
* @return mixed
611+
* @throws InvalidImapTimeoutTypeException
612+
*/
613+
public function getTimeout($type){
614+
if(is_numeric($type)) {
615+
$type = (int) $type;
616+
}elseif (isset($this->timeout_type[$type])){
617+
$type = $this->timeout_type[$type];
618+
}else{
619+
throw new InvalidImapTimeoutTypeException("Invalid imap timeout type provided.");
620+
}
621+
622+
return imap_timeout($type);
623+
}
572624
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/*
3+
* File: InvalidImapTimeoutTypeException.php
4+
* Category: Exception
5+
* Author: M. Goldenbaum
6+
* Created: 12.03.19 18:41
7+
* Updated: -
8+
*
9+
* Description:
10+
* -
11+
*/
12+
13+
namespace Webklex\IMAP\Exceptions;
14+
15+
use \Exception;
16+
17+
/**
18+
* Class InvalidImapTimeoutTypeException
19+
*
20+
* @package Webklex\IMAP\Exceptions
21+
*/
22+
class InvalidImapTimeoutTypeException extends Exception {
23+
24+
}

0 commit comments

Comments
 (0)