Passed
Pull Request — master (#11)
by Anton
03:24
created

Sitemap::getPages()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 3
nop 0
1
<?php
2
3
namespace Modules\Tools\Handler {
4
5
	use Frames, Modules\Entitizer, Modules\Tools, Date, DB, Explorer;
6
7
	class Sitemap extends Frames\Tools\Sitemap {
8
9
		# Get last modified
10
11 View Code Duplication
		private function getLastModified() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
13
			$selection = 'MAX(time_modified) as last_modified';
14
15
			if (!(DB::select(TABLE_PAGES, $selection) && (DB::getLast()->rows === 1))) return 0;
16
17
			# ------------------------
18
19
			return intval(DB::getLast()->getRow()['last_modified']);
20
		}
21
22
		# Get pages
23
24
		private function getPages() {
25
26
			$selection = ['id', 'slug', 'time_modified'];
27
28
			$condition = ['visibility' => VISIBILITY_PUBLISHED, 'access' => ACCESS_PUBLIC, 'locked' => 0];
29
30
			if (!(DB::select(TABLE_PAGES, $selection, $condition, 'slug') && DB::getLast()->status)) return;
31
32
			# ------------------------
33
34
			while (null !== ($data = DB::getLast()->getRow())) yield Entitizer::dataset(TABLE_PAGES, $data);
35
		}
36
37
		# Handle request
38
39
		protected function handle() {
40
41
			# Create sitemap
42
43
			$sitemap = new Tools\Sitemap();
44
45
			# Load sitemap
46
47
			$file_name = (DIR_SYSTEM_DATA . 'Sitemap.xml');
48
49
			$modified = Explorer::getModified($file_name);
50
51
			if ((false !== $modified) && ($modified > $this->getLastModified())) {
52
53
				if (false !== $sitemap->load($file_name)) return $sitemap;
54
			}
55
56
			# Fill sitemap
57
58
			foreach ($this->getPages() as $page) {
59
60
				$loc = $page->canonical; $lastmod = Date::get(DATE_FORMAT_W3C, $page->time_modified);
61
62
				$sitemap->add($loc, $lastmod, FREQUENCY_WEEKLY, 0.5);
63
			}
64
65
			# Save sitemap
66
67
			$sitemap->save($file_name);
68
69
			# ------------------------
70
71
			return $sitemap;
72
		}
73
	}
74
}
75