Complex classes like MongoDbServiceProvider 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 MongoDbServiceProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class MongoDbServiceProvider implements ServiceProviderInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var array Buffer. |
||
23 | */ |
||
24 | private $args = array(); |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | private $defaultArgs = array( |
||
30 | 'uri_options' => array(), |
||
31 | 'driver_options' => array(), |
||
32 | ); |
||
33 | |||
34 | /** |
||
35 | * @var int|string |
||
36 | */ |
||
37 | private $defaultConnection = 0; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $id = 'mongodb'; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $instances = 'mongodb.clients'; |
||
48 | |||
49 | /** |
||
50 | * @var bool |
||
51 | */ |
||
52 | private $isLoaded = false; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | private $parameters = array(); |
||
58 | |||
59 | /** |
||
60 | * @param string $id |
||
61 | * @param string $instances |
||
62 | */ |
||
63 | 3 | public function __construct($id = null, $instances = null) |
|
72 | |||
73 | /** |
||
74 | * @return string |
||
75 | */ |
||
76 | private function buildConnectionString(array $connection) |
||
77 | { |
||
78 | $uri = '//'; |
||
79 | |||
80 | if (isset($connection[0]) && count($connection) == 1) { |
||
81 | $uri .= $this->sanitizeUri($connection[0]); |
||
82 | } else { |
||
83 | $uri .= $this->getArgConnectionAuthority($connection); |
||
84 | |||
85 | if (!empty($connection['database'])) { |
||
86 | $uri .= '/' . $connection['database']; |
||
87 | |||
88 | if (!empty($connection['options'])) { |
||
89 | $uri .= '?' . http_build_query($connection['options']); |
||
90 | } |
||
91 | } elseif (!empty($connection['options'])) { |
||
92 | $uri .= '/?' . http_build_query($connection['options']); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | return $uri; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param array|string $connection |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | private function buildUri($connection) |
||
105 | { |
||
106 | return 'mongodb:' . $this->buildConnectionString(is_array($connection) ? $connection : array($connection)); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param \Pimple\Container $app |
||
111 | * @param string $name |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | private function getArg(Container $app, $name) |
||
116 | { |
||
117 | return isset($this->args[$name]) ? $this->args[$name] : $this->getDefaultArg($app, $name); |
||
118 | } |
||
119 | |||
120 | private function getArgConnectionAuthority(array $connection) |
||
121 | { |
||
122 | $authority = ''; |
||
123 | |||
124 | if (!empty($connection['password'])) { |
||
125 | $authority .= $connection['username'] . ':' . rawurlencode($connection['password']) . '@'; |
||
126 | } elseif (!empty($connection['username'])) { |
||
127 | $authority .= $connection['username'] . '@'; |
||
128 | } |
||
129 | |||
130 | $authority .= $this->getArgConnectionHost($connection); |
||
131 | |||
132 | return $authority; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @return string |
||
137 | */ |
||
138 | private function getArgConnectionHost(array $connection = array()) |
||
139 | { |
||
140 | if (empty($connection['host'])) { |
||
141 | $connection['host'] = '127.0.0.1'; |
||
142 | } |
||
143 | |||
144 | return empty($connection['port']) ? $connection['host'] : $connection['host'] . ':' . $connection['port']; |
||
145 | } |
||
146 | |||
147 | private function getArgConnectionHosts(array $connection) |
||
157 | |||
158 | /** |
||
159 | * @return string |
||
160 | */ |
||
161 | 2 | private function getArgUri(Container $app) |
|
169 | |||
170 | /** |
||
171 | * @param \Pimple\Container $app |
||
172 | * @param string $name |
||
173 | * |
||
174 | * @return string|array |
||
175 | */ |
||
176 | private function getDefaultArg(Container $app, $name) |
||
177 | { |
||
178 | if (!isset($this->defaultArgs[$name])) { |
||
179 | $this->setDefaultArg($app, $name); |
||
180 | } |
||
181 | |||
182 | return $this->defaultArgs[$name]; |
||
183 | } |
||
184 | |||
185 | 2 | private function loadParameters(Container $app) |
|
200 | |||
201 | 2 | private function loadSingletonParameters(Container $app) |
|
202 | { |
||
203 | 2 | $this->parameters[0] = array(); |
|
204 | |||
205 | 2 | if (!empty($app['mongodb.uri'])) { |
|
206 | $this->parameters[0]['uri'] = $app['mongodb.uri']; |
||
207 | 2 | } elseif (!empty($app['mongodb.connection'])) { |
|
208 | $this->parameters[0]['connection'] = $app['pdo.connection']; |
||
209 | } |
||
210 | |||
211 | 2 | if (!empty($app['mongodb.uri_options'])) { |
|
212 | $this->parameters[0]['uri_options'] = $app['mongodb.uri_options']; |
||
213 | } |
||
214 | 2 | if (!empty($app['mongodb.driver_options'])) { |
|
215 | $this->parameters[0]['driver_options'] = $app['mongodb.driver_options']; |
||
216 | } |
||
217 | 2 | } |
|
218 | |||
219 | /** |
||
220 | * @param string $uri |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | private function sanitizeUri($uri) |
||
225 | { |
||
226 | if ('mongodb:' == substr($uri, 0, 8)) { |
||
227 | $uri = substr($uri, 8); |
||
228 | } |
||
229 | |||
230 | return ('//' == substr($uri, 0, 2)) ? substr($uri, 2) : $uri; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param \Pimple\Container $app |
||
235 | * @param string $name |
||
236 | */ |
||
237 | private function setDefaultArg(Container $app, $name) |
||
243 | |||
244 | 3 | public function register(Container $app) |
|
245 | { |
||
246 | $app[$this->id] = function () use ($app) { |
||
247 | 2 | $this->loadParameters($app); |
|
248 | |||
249 | 2 | return $app[$this->instances][$this->defaultConnection]; |
|
250 | }; |
||
251 | $app[$this->instances] = function () use ($app) { |
||
274 | } |
||
275 |