Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/src/ClientManager.php

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