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

Sitemap   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 67
Duplicated Lines 14.93 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 6
dl 10
loc 67
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLastModified() 10 10 3
A getPages() 0 12 4
B handle() 0 34 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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