Total Complexity | 49 |
Total Lines | 349 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like ContainerUtils 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 ContainerUtils, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class ContainerUtils |
||
31 | { |
||
32 | use \PHPPgAdmin\Traits\HelperTrait; |
||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | const BASE_PATH = BASE_PATH; |
||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | const SUBFOLDER = SUBFOLDER; |
||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | const DEBUGMODE = DEBUGMODE; |
||
45 | |||
46 | /** |
||
47 | * @var ContainerInterface |
||
48 | */ |
||
49 | protected $_container; |
||
50 | |||
51 | /** |
||
52 | * @var App |
||
53 | */ |
||
54 | protected $_app; |
||
55 | |||
56 | /** |
||
57 | * @var self |
||
58 | */ |
||
59 | protected static $_instance; |
||
60 | |||
61 | /** |
||
62 | * Constructor of the ContainerUtils class. |
||
63 | */ |
||
64 | public function __construct() |
||
117 | } |
||
118 | |||
119 | public static function getContainerInstance() |
||
120 | { |
||
121 | $_instance = self::getInstance(); |
||
122 | |||
123 | if (!$container = $_instance->_container) { |
||
124 | throw new \Exception('Could not get a container'); |
||
125 | } |
||
126 | |||
127 | return $container; |
||
128 | } |
||
129 | |||
130 | public static function getInstance() |
||
131 | { |
||
132 | if (!self::$_instance) { |
||
133 | self::$_instance = new self(); |
||
134 | } |
||
135 | |||
136 | return self::$_instance; |
||
137 | } |
||
138 | |||
139 | public static function createContainer($conf) |
||
196 | } |
||
197 | |||
198 | public function maybeRenderIframes($response, $subject, $query_string) |
||
199 | { |
||
200 | $c = self::getContainerInstance(); |
||
201 | |||
202 | $in_test = $c->view->offsetGet('in_test'); |
||
203 | |||
204 | if ('1' === $in_test) { |
||
205 | $className = '\PHPPgAdmin\Controller\\' . \ucfirst($subject) . 'Controller'; |
||
206 | $controller = new $className($c); |
||
207 | |||
208 | return $controller->render(); |
||
209 | } |
||
210 | |||
211 | $viewVars = [ |
||
212 | 'url' => '/src/views/' . $subject . ($query_string ? '?' . $query_string : ''), |
||
213 | 'headertemplate' => 'header.twig', |
||
214 | ]; |
||
215 | |||
216 | return $c->view->render($response, 'iframe_view.twig', $viewVars); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Gets the theme from |
||
221 | * 1. The $_REQUEST global (when it's chosen from start screen) |
||
222 | * 2. Server specific config theme |
||
223 | * 3.- $_SESSION global (subsequent requests after 1.) |
||
224 | * 4.- $_COOKIE global (mostly fallback for $_SESSION after 1.- and 3.-) |
||
225 | * 5.- theme as set in config |
||
226 | * 6.- 'default' theme. |
||
227 | * |
||
228 | * @param <type> $conf The conf |
||
229 | * @param null|mixed $_server_info |
||
230 | * |
||
231 | * @return string the theme |
||
232 | */ |
||
233 | public function getTheme(array $conf, $_server_info = null) |
||
234 | { |
||
235 | $_theme = null; |
||
236 | // List of themes |
||
237 | $themefolders = $this->getThemeFolders(); |
||
238 | // Check if theme is in $_REQUEST, $_SESSION or $_COOKIE |
||
239 | // 1.- First priority: $_REQUEST, this happens when you use the selector |
||
240 | if (\array_key_exists('theme', $_REQUEST) && |
||
241 | \array_key_exists($_REQUEST['theme'], $themefolders)) { |
||
242 | $_theme = $_REQUEST['theme']; |
||
243 | } elseif ( // otherwise, see if there's a theme associated with this particular server |
||
244 | null !== $_server_info && |
||
245 | \array_key_exists('theme', $_server_info) && |
||
246 | \is_string($_server_info['theme']) && |
||
247 | \array_key_exists($_COOKIE['ppaTheme'], $themefolders)) { |
||
248 | $_theme = $_server_info['theme']; |
||
249 | } elseif (\array_key_exists('ppaTheme', $_SESSION) && |
||
250 | \array_key_exists($_SESSION['ppaTheme'], $themefolders)) { |
||
251 | // otherwise check $_SESSION |
||
252 | $_theme = $_SESSION['ppaTheme']; |
||
253 | } elseif (\array_key_exists('ppaTheme', $_SESSION) && |
||
254 | \array_key_exists($_COOKIE['ppaTheme'], $themefolders)) { |
||
255 | // oterwise check $_COOKIE |
||
256 | $_theme = $_COOKIE['ppaTheme']; |
||
257 | } elseif ( // see if there's a valid theme set in config file |
||
258 | \array_key_exists('theme', $conf) && |
||
259 | \is_string($conf['theme']) && |
||
260 | \array_key_exists($conf['theme'], $themefolders)) { |
||
261 | $_theme = $conf['theme']; |
||
262 | } else { |
||
263 | // okay then, use default theme |
||
264 | $_theme = 'default'; |
||
265 | } |
||
266 | |||
267 | return $_theme; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Determines the redirection url according to query string. |
||
272 | * |
||
273 | * @return string the redirect url |
||
274 | */ |
||
275 | public function getRedirectUrl() |
||
276 | { |
||
277 | $query_string = $this->_container->requestobj->getUri()->getQuery(); |
||
278 | |||
279 | // if server_id isn't set, then you will be redirected to intro |
||
280 | if (null === $this->_container->requestobj->getQueryParam('server')) { |
||
281 | $destinationurl = \SUBFOLDER . '/src/views/intro'; |
||
282 | } else { |
||
283 | // otherwise, you'll be redirected to the login page for that server; |
||
284 | $destinationurl = \SUBFOLDER . '/src/views/login' . ($query_string ? '?' . $query_string : ''); |
||
285 | } |
||
286 | |||
287 | return $destinationurl; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Gets the destination with the last active tab selected for that controller |
||
292 | * Usually used after going through a redirect route. |
||
293 | * |
||
294 | * @param string $subject The subject, usually a view name like 'server' or 'table' |
||
295 | * |
||
296 | * @return string The destination url with last tab set in the query string |
||
297 | */ |
||
298 | public function getDestinationWithLastTab($subject) |
||
299 | { |
||
300 | $_server_info = $this->_container->misc->getServerInfo(); |
||
301 | $this->addFlash($subject, 'getDestinationWithLastTab'); |
||
302 | //$this->prtrace('$_server_info', $_server_info); |
||
303 | // If username isn't set in server_info, you should login |
||
304 | if (!isset($_server_info['username'])) { |
||
305 | $destinationurl = $this->getRedirectUrl(); |
||
306 | } else { |
||
307 | $url = $this->_container->misc->getLastTabURL($subject); |
||
308 | $this->addFlash($url, 'getLastTabURL for ' . $subject); |
||
309 | // Load query vars into superglobal arrays |
||
310 | if (isset($url['urlvars'])) { |
||
311 | $urlvars = []; |
||
312 | |||
313 | foreach ($url['urlvars'] as $key => $urlvar) { |
||
314 | //$this->prtrace($key, $urlvar); |
||
315 | $urlvars[$key] = \PHPPgAdmin\Decorators\Decorator::get_sanitized_value($urlvar, $_REQUEST); |
||
316 | } |
||
317 | $_REQUEST = \array_merge($_REQUEST, $urlvars); |
||
318 | $_GET = \array_merge($_GET, $urlvars); |
||
319 | } |
||
320 | |||
321 | $actionurl = \PHPPgAdmin\Decorators\Decorator::actionurl($url['url'], $_GET); |
||
322 | $destinationurl = $actionurl->value($_GET); |
||
323 | } |
||
324 | $destinationurl = \str_replace('views/?', "views/{$subject}?", $destinationurl); |
||
325 | // $this->prtrace('destinationurl for ' . $subject, $destinationurl); |
||
326 | return $destinationurl; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Adds an error to the errors array property of the container. |
||
331 | * |
||
332 | * @param string $errormsg The error msg |
||
333 | * |
||
334 | * @return \Slim\Container The app container |
||
335 | */ |
||
336 | public function addError($errormsg) |
||
337 | { |
||
338 | $errors = $this->_container->get('errors'); |
||
339 | $errors[] = $errormsg; |
||
340 | $this->_container->offsetSet('errors', $errors); |
||
341 | |||
342 | return $this->_container; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Traverse THEME_PATH, consider as theme folders those which |
||
347 | * contain a `global.css` stylesheet. |
||
348 | * |
||
349 | * @return array the theme folders |
||
350 | */ |
||
351 | private function getThemeFolders() |
||
379 | } |
||
380 | } |
||
381 |