forked from Webklex/php-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientManager.php
131 lines (114 loc) · 3.24 KB
/
ClientManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/*
* File: ClientManager.php
* Category: -
* Author: M. Goldenbaum
* Created: 19.01.17 22:21
* Updated: -
*
* Description:
* -
*/
namespace Webklex\PHPIMAP;
/**
* Class ClientManager
*
* @package Webklex\IMAP
*/
class ClientManager {
/**
* All library config
*
* @var Config $config
*/
public Config $config;
/**
* @var array $accounts
*/
protected array $accounts = [];
/**
* ClientManager constructor.
* @param array|string|Config $config
*/
public function __construct(array|string|Config $config = []) {
$this->setConfig($config);
}
/**
* Dynamically pass calls to the default account.
* @param string $method
* @param array $parameters
*
* @return mixed
* @throws Exceptions\MaskNotFoundException
*/
public function __call(string $method, array $parameters) {
$callable = [$this->account(), $method];
return call_user_func_array($callable, $parameters);
}
/**
* Safely create a new client instance which is not listed in accounts
* @param array $config
*
* @return Client
* @throws Exceptions\MaskNotFoundException
*/
public function make(array $config): Client {
$name = $this->config->getDefaultAccount();
$clientConfig = $this->config->all();
$clientConfig["accounts"] = [$name => $config];
return new Client(Config::make($clientConfig));
}
/**
* Resolve a account instance.
* @param string|null $name
*
* @return Client
* @throws Exceptions\MaskNotFoundException
*/
public function account(?string $name = null): Client {
$name = $name ?: $this->config->getDefaultAccount();
// If the connection has not been resolved we will resolve it now as all
// the connections are resolved when they are actually needed, so we do
// not make any unnecessary connection to the various queue end-points.
if (!isset($this->accounts[$name])) {
$this->accounts[$name] = $this->resolve($name);
}
return $this->accounts[$name];
}
/**
* Resolve an account.
* @param string $name
*
* @return Client
* @throws Exceptions\MaskNotFoundException
*/
protected function resolve(string $name): Client {
$config = $this->config->getClientConfig($name);
return new Client($config);
}
/**
* Merge the vendor settings with the local config
*
* The default account identifier will be used as default for any missing account parameters.
* If however the default account is missing a parameter the package default account parameter will be used.
* This can be disabled by setting imap.default in your config file to 'false'
*
* @param array|string|Config $config
*
* @return $this
*/
public function setConfig(array|string|Config $config): ClientManager {
if (!$config instanceof Config) {
$config = Config::make($config);
}
$this->config = $config;
return $this;
}
/**
* Get the config instance
* @return Config
*/
public function getConfig(): Config {
return $this->config;
}
}