Passed
Branch tests1.5 (59f3fd)
by Wanderson
02:32
created

ControllerFactory::formatAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\Mvc;
4
5
use Win\Format\Str;
6
use Win\Mvc\DefaultController;
7
8
/**
9
 * Fábrica de Controllers
10
 * 
11
 * Cria o Controller de acordo com a Página/Rota
12
 */
13
class ControllerFactory {
14
15
	/**
16
	 * Cria um Controller com base na página/rota
17
	 * @param string $page
18
	 * @param string|null $action
19
	 * @return Controller
20
	 */
21
	public static function create($page, $action = null) {
22
		$class = static::formatClass($page);
23
		if (class_exists($class)) {
24
			$action = static::formatAction($action);
25
			return new $class($action);
26
		}
27
		return new DefaultController();
28
	}
29
30
	/**
31
	 * Retorna nome de um Action válido
32
	 * @param string $string
33
	 * @return string
34
	 */
35
	protected static function formatAction($string) {
36
		if (empty($string)) {
37
			$string = Application::app()->getParam(1);
38
		}
39
		return Str::camel($string);
40
	}
41
42
	/**
43
	 * Retorna nome de um Controller válido
44
	 * @param string $page
45
	 * @return string
46
	 */
47
	protected static function formatClass($page) {
48
		return 'controller\\' . str_replace(' ', '', ucwords(str_replace('-', ' ', $page) . 'Controller'));
49
	}
50
51
}
52