Test Failed
Pull Request — master (#97)
by Gildonei
03:38
created

ControllerSeo::init()   B

Complexity

Conditions 10
Paths 24

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 10.2217

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 33
ccs 20
cts 23
cp 0.8696
rs 7.6666
cc 10
nc 24
nop 0
crap 10.2217

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Ubiquity\seo;
4
5
use Ubiquity\controllers\Startup;
6
use Ubiquity\cache\CacheManager;
7
use Ubiquity\cache\ClassUtils;
8
use Ubiquity\controllers\Router;
9
use Ubiquity\utils\base\UFileSystem;
10
use Ubiquity\utils\base\UString;
11
12
/**
13
 * Base class for SEO controllers
14
 * Ubiquity\seo$ControllerSeo
15
 * This class is part of Ubiquity
16
 *
17
 * @author jcheron <[email protected]>
18
 * @version 1.0.1
19
 *
20
 */
21
class ControllerSeo {
22
	private $name;
23
	private $urlsFile;
24
	private $siteMapTemplate;
25
	private $route;
26
	private $inRobots;
27
28 1
	public function __construct($className = null) {
29 1
		if (isset ( $className ) && \class_exists ( $className )) {
30 1
			$route = Router::getRouteInfoByControllerAction ( $className, "index" );
31 1
			if ($route) {
32
				$this->route = $route ["path"];
33
			}
34 1
			$ctrl = new $className ();
35 1
			$this->name = $className;
36 1
			$this->urlsFile = $ctrl->_getUrlsFilename ();
37 1
			$this->siteMapTemplate = $ctrl->_getSeoTemplateFilename ();
38
		}
39 1
	}
40
41
	/**
42
	 *
43
	 * @return mixed
44
	 */
45 1
	public function getName() {
46 1
		return $this->name;
47
	}
48
49
	/**
50
	 *
51
	 * @return mixed
52
	 */
53
	public function getUrlsFile() {
54
		return $this->urlsFile;
55
	}
56
57
	/**
58
	 *
59
	 * @return mixed
60
	 */
61
	public function getSiteMapTemplate() {
62
		return $this->siteMapTemplate;
63
	}
64
65
	/**
66
	 *
67
	 * @return mixed
68
	 */
69 1
	public function getRoute() {
70 1
		return $this->route;
71
	}
72
73
	/**
74
	 *
75
	 * @param mixed $name
76
	 */
77
	public function setName($name) {
78
		$this->name = $name;
79
	}
80
81
	/**
82
	 *
83
	 * @param mixed $urlsFile
84
	 */
85
	public function setUrlsFile($urlsFile) {
86
		$this->urlsFile = $urlsFile;
87
	}
88
89
	/**
90
	 *
91
	 * @param mixed $siteMapTemplate
92
	 */
93
	public function setSiteMapTemplate($siteMapTemplate) {
94
		$this->siteMapTemplate = $siteMapTemplate;
95
	}
96
97
	/**
98
	 *
99
	 * @param mixed $route
100
	 */
101
	public function setRoute($route) {
102
		$this->route = $route;
103
	}
104
105 1
	public function getPath() {
106 1
		if (UString::isNotNull ( $this->route ))
107
			return $this->route;
108 1
		$parts = \explode ( "\\", $this->name );
109 1
		return end ( $parts );
110
	}
111
112 1
	public function urlExists() {
113 1
		return CacheManager::$cache->exists ( $this->urlsFile );
114
	}
115
116 1
	public static function init() {
117 1
		$result = [ ];
118 1
		$config = Startup::getConfig ();
119
120 1
		$robotsContent = "";
121 1
		$robotsFile = Startup::getApplicationDir () . \DS . 'robots.txt';
122 1
		if (\file_exists ( $robotsFile )) {
123
			$robotsContent = UFileSystem::load ( $robotsFile );
124
		}
125 1
		$files = CacheManager::getControllersFiles ( $config, true );
126
		try {
127 1
			$restCtrls = CacheManager::getRestCache ();
128
		} catch ( \Exception $e ) {
129
			$restCtrls = [ ];
130
		}
131
132 1
		foreach ( $files as $file ) {
133 1
			if (is_file ( $file )) {
134 1
				$controllerClass = ClassUtils::getClassFullNameFromFile ( $file );
135 1
				if (isset ( $restCtrls [$controllerClass] ) === false) {
136 1
					if (\class_exists ( $controllerClass, true )) {
137 1
						$reflect = new \ReflectionClass ( $controllerClass );
138 1
						if (! $reflect->isAbstract () && $reflect->isSubclassOf ( 'Ubiquity\controllers\seo\SeoController' )) {
139 1
							$ctrlSeo = new ControllerSeo ( $controllerClass );
140 1
							$path = $ctrlSeo->getPath ();
141 1
							$ctrlSeo->setInRobots ( $robotsContent !== false && (\strpos ( $robotsContent, $path ) !== false) );
142 1
							$result [] = $ctrlSeo;
143
						}
144
					}
145
				}
146
			}
147
		}
148 1
		return $result;
149
	}
150
151
	/**
152
	 *
153
	 * @return mixed
154
	 */
155
	public function getInRobots() {
156
		return $this->inRobots;
157
	}
158
159
	/**
160
	 *
161
	 * @param mixed $inRobots
162
	 */
163 1
	public function setInRobots($inRobots) {
164 1
		$this->inRobots = $inRobots;
165 1
	}
166
}
167