Test Failed
Push — master ( e59e29...f69517 )
by Jean-Christophe
14:19
created

ControllerSeo::setRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 1
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
class ControllerSeo {
13
	private $name;
14
	private $urlsFile;
15
	private $siteMapTemplate;
16
	private $route;
17
	private $inRobots;
18
19
	public function __construct($className = null) {
20
		if (isset ( $className ) && \class_exists ( $className )) {
21
			$route = Router::getRouteInfoByControllerAction ( $className, "index" );
22
			if ($route) {
23
				$this->route = $route ["path"];
24
			}
25
			$ctrl = new $className ();
26
			$this->name = $className;
27
			$this->urlsFile = $ctrl->_getUrlsFilename ();
28
			$this->siteMapTemplate = $ctrl->_getSeoTemplateFilename ();
29
		}
30
	}
31
32
	/**
33
	 *
34
	 * @return mixed
35
	 */
36
	public function getName() {
37
		return $this->name;
38
	}
39
40
	/**
41
	 *
42
	 * @return mixed
43
	 */
44
	public function getUrlsFile() {
45
		return $this->urlsFile;
46
	}
47
48
	/**
49
	 *
50
	 * @return mixed
51
	 */
52
	public function getSiteMapTemplate() {
53
		return $this->siteMapTemplate;
54
	}
55
56
	/**
57
	 *
58
	 * @return mixed
59
	 */
60
	public function getRoute() {
61
		return $this->route;
62
	}
63
64
	/**
65
	 *
66
	 * @param mixed $name
67
	 */
68
	public function setName($name) {
69
		$this->name = $name;
70
	}
71
72
	/**
73
	 *
74
	 * @param mixed $urlsFile
75
	 */
76
	public function setUrlsFile($urlsFile) {
77
		$this->urlsFile = $urlsFile;
78
	}
79
80
	/**
81
	 *
82
	 * @param mixed $siteMapTemplate
83
	 */
84
	public function setSiteMapTemplate($siteMapTemplate) {
85
		$this->siteMapTemplate = $siteMapTemplate;
86
	}
87
88
	/**
89
	 *
90
	 * @param mixed $route
91
	 */
92
	public function setRoute($route) {
93
		$this->route = $route;
94
	}
95
96
	public function getPath() {
97
		if (UString::isNotNull ( $this->route ))
98
			return $this->route;
99
		$parts = \explode ( "\\", $this->name );
100
		return end ( $parts );
101
	}
102
103
	public function urlExists() {
104
		return CacheManager::$cache->exists ( $this->urlsFile );
105
	}
106
107
	public static function init() {
108
		$result = [ ];
109
		$config = Startup::getConfig ();
110
111
		$robotsContent = "";
112
		$robotsFile = Startup::getApplicationDir () . \DS . 'robots.txt';
113
		if (\file_exists ( $robotsFile )) {
114
			$robotsContent = UFileSystem::load ( $robotsFile );
115
		}
116
		$files = CacheManager::getControllersFiles ( $config, true );
117
		try {
118
			$restCtrls = CacheManager::getRestCache ();
119
		} catch ( \Exception $e ) {
120
			$restCtrls = [ ];
121
		}
122
123
		foreach ( $files as $file ) {
124
			if (is_file ( $file )) {
125
				$controllerClass = ClassUtils::getClassFullNameFromFile ( $file );
126
				if (isset ( $restCtrls [$controllerClass] ) === false) {
127
					if (\class_exists ( $controllerClass, true )) {
128
						$reflect = new \ReflectionClass ( $controllerClass );
129
						if (! $reflect->isAbstract () && $reflect->isSubclassOf ( 'Ubiquity\controllers\seo\SeoController' )) {
130
							$ctrlSeo = new ControllerSeo ( $controllerClass );
131
							$path = $ctrlSeo->getPath ();
132
							$ctrlSeo->setInRobots ( \strpos ( $robotsContent, $path ) !== false );
0 ignored issues
show
Bug introduced by
It seems like $robotsContent can also be of type false; however, parameter $haystack of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

132
							$ctrlSeo->setInRobots ( \strpos ( /** @scrutinizer ignore-type */ $robotsContent, $path ) !== false );
Loading history...
133
							$result [] = $ctrlSeo;
134
						}
135
					}
136
				}
137
			}
138
		}
139
		return $result;
140
	}
141
142
	/**
143
	 *
144
	 * @return mixed
145
	 */
146
	public function getInRobots() {
147
		return $this->inRobots;
148
	}
149
150
	/**
151
	 *
152
	 * @param mixed $inRobots
153
	 */
154
	public function setInRobots($inRobots) {
155
		$this->inRobots = $inRobots;
156
	}
157
}
158