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 | 3 | private function buildConnectionString(array $connection) |
|
77 | { |
||
78 | 3 | if (isset($connection[0]) && count($connection) == 1) { |
|
79 | 3 | return '//' . $this->sanitizeUri($connection[0]); |
|
80 | } |
||
81 | |||
82 | 2 | $uri = '//' . $this->getArgConnectionAuthority($connection); |
|
83 | |||
84 | 2 | if (!empty($connection['database'])) { |
|
85 | $uri .= '/' . $connection['database']; |
||
86 | |||
87 | if (!empty($connection['options'])) { |
||
88 | $uri .= '?' . http_build_query($connection['options']); |
||
89 | } |
||
90 | 2 | } elseif (!empty($connection['options'])) { |
|
91 | $uri .= '/?' . http_build_query($connection['options']); |
||
92 | } |
||
93 | |||
94 | 2 | return $uri; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param array|string $connection |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | 3 | private function buildUri($connection) |
|
103 | { |
||
104 | 3 | return 'mongodb:' . $this->buildConnectionString(is_array($connection) ? $connection : array($connection)); |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param \Pimple\Container $app |
||
109 | * @param string $name |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | 3 | private function getArg(Container $app, $name) |
|
114 | { |
||
115 | 3 | return isset($this->args[$name]) ? $this->args[$name] : $this->getDefaultArg($app, $name); |
|
116 | } |
||
117 | |||
118 | 2 | private function getArgConnectionAuthority(array $connection) |
|
119 | { |
||
120 | 2 | $authority = ''; |
|
121 | |||
122 | 2 | if (!empty($connection['password'])) { |
|
123 | $authority .= $connection['username'] . ':' . rawurlencode($connection['password']) . '@'; |
||
124 | 2 | } elseif (!empty($connection['username'])) { |
|
125 | $authority .= $connection['username'] . '@'; |
||
126 | } |
||
127 | |||
128 | 2 | $authority .= $this->getArgConnectionHost($connection); |
|
129 | |||
130 | 2 | return $authority; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * @return string |
||
135 | */ |
||
136 | 2 | private function getArgConnectionHost(array $connection = array()) |
|
137 | { |
||
138 | 2 | if (empty($connection['host'])) { |
|
139 | 2 | $connection['host'] = '127.0.0.1'; |
|
140 | 2 | } |
|
141 | |||
142 | 2 | return empty($connection['port']) ? $connection['host'] : $connection['host'] . ':' . $connection['port']; |
|
143 | } |
||
144 | |||
145 | private function getArgConnectionHosts(array $connection) |
||
155 | |||
156 | /** |
||
157 | * @return string |
||
158 | */ |
||
159 | 3 | private function getArgUri(Container $app) |
|
160 | { |
||
161 | 3 | if (!empty($this->args['uri'])) { |
|
162 | 1 | return $this->args['uri']; |
|
163 | } |
||
164 | |||
165 | 2 | return $this->buildUri(isset($this->args['connection']) ? $this->args['connection'] : null); |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param \Pimple\Container $app |
||
170 | * @param string $name |
||
171 | * |
||
172 | * @return string|array |
||
173 | */ |
||
174 | 3 | private function getDefaultArg(Container $app, $name) |
|
175 | { |
||
176 | 3 | if (!isset($this->defaultArgs[$name])) { |
|
177 | $this->setDefaultArg($app, $name); |
||
178 | } |
||
179 | |||
180 | 3 | return $this->defaultArgs[$name]; |
|
181 | } |
||
182 | |||
183 | 3 | private function loadParameters(Container $app) |
|
198 | |||
199 | 3 | private function loadSingletonParameters(Container $app) |
|
200 | { |
||
201 | 3 | $this->parameters[0] = array(); |
|
202 | |||
203 | 3 | if (!empty($app['mongodb.uri'])) { |
|
204 | 1 | $this->parameters[0]['uri'] = $app['mongodb.uri']; |
|
205 | 3 | } elseif (!empty($app['mongodb.connection'])) { |
|
206 | $this->parameters[0]['connection'] = $app['pdo.connection']; |
||
207 | } |
||
208 | |||
209 | 3 | if (!empty($app['mongodb.uri_options'])) { |
|
210 | $this->parameters[0]['uri_options'] = $app['mongodb.uri_options']; |
||
211 | } |
||
212 | 3 | if (!empty($app['mongodb.driver_options'])) { |
|
213 | $this->parameters[0]['driver_options'] = $app['mongodb.driver_options']; |
||
214 | } |
||
215 | 3 | } |
|
216 | |||
217 | /** |
||
218 | * @param string $uri |
||
219 | * |
||
220 | * @return string |
||
221 | */ |
||
222 | 3 | private function sanitizeUri($uri) |
|
223 | { |
||
224 | 3 | if ('mongodb:' == substr($uri, 0, 8)) { |
|
225 | 3 | $uri = substr($uri, 8); |
|
226 | 3 | } |
|
227 | |||
228 | 3 | return ('//' == substr($uri, 0, 2)) ? substr($uri, 2) : $uri; |
|
229 | } |
||
230 | |||
231 | /** |
||
232 | * @param \Pimple\Container $app |
||
233 | * @param string $name |
||
234 | */ |
||
235 | private function setDefaultArg(Container $app, $name) |
||
241 | |||
242 | 3 | public function register(Container $app) |
|
243 | { |
||
244 | $app[$this->id] = function () use ($app) { |
||
245 | 3 | $this->loadParameters($app); |
|
246 | |||
247 | 3 | return $app[$this->instances][$this->defaultConnection]; |
|
248 | }; |
||
249 | $app[$this->instances] = function () use ($app) { |
||
250 | 3 | $this->loadParameters($app); |
|
272 | } |
||
273 |