Total Complexity | 43 |
Total Lines | 253 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 1 |
Complex classes like ClientManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ClientManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class ClientManager { |
||
23 | |||
24 | /** |
||
25 | * All library config |
||
26 | * |
||
27 | * @var array $config |
||
28 | */ |
||
29 | public static $config = []; |
||
30 | |||
31 | /** |
||
32 | * @var array $accounts |
||
33 | */ |
||
34 | protected $accounts = []; |
||
35 | |||
36 | /** |
||
37 | * ClientManager constructor. |
||
38 | * @param array|string $config |
||
39 | */ |
||
40 | public function __construct($config = []) { |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Dynamically pass calls to the default account. |
||
46 | * @param string $method |
||
47 | * @param array $parameters |
||
48 | * |
||
49 | * @return mixed |
||
50 | * @throws Exceptions\MaskNotFoundException |
||
51 | */ |
||
52 | public function __call(string $method, array $parameters) { |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Safely create a new client instance which is not listed in accounts |
||
60 | * @param array $config |
||
61 | * |
||
62 | * @return Client |
||
63 | * @throws Exceptions\MaskNotFoundException |
||
64 | */ |
||
65 | public function make(array $config): Client { |
||
66 | return new Client($config); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Get a dotted config parameter |
||
71 | * @param string $key |
||
72 | * @param null $default |
||
|
|||
73 | * |
||
74 | * @return mixed|null |
||
75 | */ |
||
76 | public static function get(string $key, $default = null) { |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Resolve a account instance. |
||
100 | * @param string|null $name |
||
101 | * |
||
102 | * @return Client |
||
103 | * @throws Exceptions\MaskNotFoundException |
||
104 | */ |
||
105 | public function account(string $name = null): Client { |
||
106 | $name = $name ?: $this->getDefaultAccount(); |
||
107 | |||
108 | // If the connection has not been resolved we will resolve it now as all |
||
109 | // the connections are resolved when they are actually needed, so we do |
||
110 | // not make any unnecessary connection to the various queue end-points. |
||
111 | if (!isset($this->accounts[$name])) { |
||
112 | $this->accounts[$name] = $this->resolve($name); |
||
113 | } |
||
114 | |||
115 | return $this->accounts[$name]; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Resolve an account. |
||
120 | * @param string $name |
||
121 | * |
||
122 | * @return Client |
||
123 | * @throws Exceptions\MaskNotFoundException |
||
124 | */ |
||
125 | protected function resolve(string $name): Client { |
||
126 | $config = $this->getClientConfig($name); |
||
127 | |||
128 | return new Client($config); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Get the account configuration. |
||
133 | * @param string|null $name |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | protected function getClientConfig($name): array { |
||
138 | if ($name === null || $name === 'null') { |
||
139 | return ['driver' => 'null']; |
||
140 | } |
||
141 | |||
142 | return is_array(self::$config["accounts"][$name]) ? self::$config["accounts"][$name] : []; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Get the name of the default account. |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getDefaultAccount(): string { |
||
151 | return self::$config['default']; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Set the name of the default account. |
||
156 | * @param string $name |
||
157 | * |
||
158 | * @return void |
||
159 | */ |
||
160 | public function setDefaultAccount(string $name) { |
||
161 | self::$config['default'] = $name; |
||
162 | } |
||
163 | |||
164 | |||
165 | /** |
||
166 | * Merge the vendor settings with the local config |
||
167 | * |
||
168 | * The default account identifier will be used as default for any missing account parameters. |
||
169 | * If however the default account is missing a parameter the package default account parameter will be used. |
||
170 | * This can be disabled by setting imap.default in your config file to 'false' |
||
171 | * |
||
172 | * @param array|string $config |
||
173 | * |
||
174 | * @return $this |
||
175 | */ |
||
176 | public function setConfig($config): ClientManager { |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Marge arrays recursively and distinct |
||
213 | * |
||
214 | * Merges any number of arrays / parameters recursively, replacing |
||
215 | * entries with string keys with values from latter arrays. |
||
216 | * If the entry or the next value to be assigned is an array, then it |
||
217 | * automatically treats both arguments as an array. |
||
218 | * Numeric entries are appended, not replaced, but only if they are |
||
219 | * unique |
||
220 | * |
||
221 | * @return array|mixed |
||
222 | * |
||
223 | * @link http://www.php.net/manual/en/function.array-merge-recursive.php#96201 |
||
224 | * @author Mark Roduner <[email protected]> |
||
225 | */ |
||
226 | private function array_merge_recursive_distinct() { |
||
275 | } |
||
276 | } |