Passed
Push — master ( be71ba...d26f6c )
by Jean-Christophe
03:55
created

UrlParser::getUrls()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Ubiquity\seo;
4
5
use Ubiquity\cache\CacheManager;
6
use Ubiquity\utils\base\UIntrospection;
7
use Ubiquity\utils\base\UFileSystem;
8
use Ubiquity\controllers\Startup;
9
10
class UrlParser {
11
	public static $frequencies = [ 'always','hourly','daily','weekly','monthly','yearly','never' ];
12
	private $urls;
13
	private $config;
14
15
	/**
16
	 *
17
	 * @return mixed
18
	 */
19
	public function getConfig() {
20
		return $this->config;
21
	}
22
23 1
	public function __construct() {
24 1
		$this->urls = [ ];
25 1
		$this->config = Startup::getConfig ();
26 1
	}
27
28 1
	public function parse() {
29 1
		$routes = CacheManager::getRoutes ();
30 1
		foreach ( $routes as $path => $route ) {
31 1
			$url = $this->parseUrl ( $path, $route );
32 1
			if (isset ( $url )) {
33 1
				if (! isset ( $this->urls [$path] ))
34 1
					$this->urls [$path] = $url;
35
			}
36
		}
37 1
	}
38
39 1
	public function parseArray($array, $existing = true) {
40 1
		foreach ( $array as $url ) {
41
			$this->urls [$url ['location']] = Url::fromArray ( $url, $existing );
42
		}
43 1
	}
44
45 1
	protected function parseUrl($path, $route) {
46 1
		if (isset ( $route ["controller"] )) {
47 1
			$controller = $route ["controller"];
48 1
			$action = $route ["action"];
49
		} elseif (isset ( $route ["get"] )) {
50
			return $this->parseUrl ( $path, $route ["get"] );
51
		} else {
52
			return;
53
		}
54 1
		$lastModified = self::getLastModified ( $controller, $action );
55 1
		if ($lastModified !== false) {
56 1
			$url = new Url ( $path, $lastModified );
57 1
			return $url;
58
		}
59
		return;
60
	}
61
62 1
	public static function getLastModified($controller, $action) {
63 1
		if (\class_exists ( $controller )) {
64 1
			$classCode = UIntrospection::getClassCode ( $controller );
65 1
			$lastModified = UFileSystem::lastModified ( UIntrospection::getFileName ( $controller ) );
66 1
			if (\is_array ( $classCode )) {
67 1
				$reflexAction = new \ReflectionMethod ( $controller . '::' . $action );
68 1
				$views = UIntrospection::getLoadedViews ( $reflexAction, $classCode );
69 1
				$baseView=\ROOT.\DS."views".\DS;
70 1
				foreach ( $views as $view ) {
71
					$file = $baseView. $view;
72
					$viewDate = UFileSystem::lastModified ( $file );
73
					if ($viewDate > $lastModified)
74
						$lastModified = $viewDate;
75
				}
76
			}
77 1
			return $lastModified;
78
		}
79
		return false;
80
	}
81
82
	/**
83
	 *
84
	 * @return array
85
	 */
86 1
	public function getUrls() {
87 1
		return $this->urls;
88
	}
89
}
90
91