Passed
Push — develop ( 39fefa...eeeda6 )
by Jens
03:13
created

SitemapStorage::saveSitemapItem()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: jensk
4
 * Date: 17-3-2017
5
 * Time: 10:29
6
 */
7
8
namespace library\storage\storage;
9
10
11
use library\storage\factories\SitemapItemFactory;
12
13
class SitemapStorage extends Storage
14
{
15
	/**
16
	 * @return array
17
	 */
18
	public function getSitemap()
19
	{
20
		return $this->repository->sitemap;
21
	}
22
23
	/**
24
	 * Add a sitemap item
25
	 *
26
	 * @param $postValues
27
	 *
28
	 * @throws \Exception
29
	 */
30
	public function addSitemapItem($postValues)
31
	{
32
		$sitemapObject = SitemapItemFactory::createSitemapItemFromPostValues($postValues);
33
		$sitemap = $this->repository->sitemap;
34
		$sitemap[] = $sitemapObject;
35
		$this->repository->sitemap = $sitemap;
36
		$this->save();
37
	}
38
39
	/**
40
	 * Save changes to a sitemap item
41
	 *
42
	 * @param $slug
43
	 * @param $postValues
44
	 *
45
	 * @throws \Exception
46
	 */
47
	public function saveSitemapItem($slug, $postValues)
48
	{
49
		$sitemapObject = SitemapItemFactory::createSitemapItemFromPostValues($postValues);
50
51
		$sitemap = $this->repository->sitemap;
52
		foreach ($sitemap as $key => $sitemapItem) {
53
			if ($sitemapItem->slug == $slug) {
54
				$sitemap[$key] = $sitemapObject;
55
			}
56
		}
57
		$this->repository->sitemap = $sitemap;
58
		$this->save();
59
	}
60
61
	/**
62
	 * Delete a sitemap item by its slug
63
	 *
64
	 * @param $slug
65
	 *
66
	 * @throws \Exception
67
	 */
68
	public function deleteSitemapItemBySlug($slug)
69
	{
70
		$sitemap = $this->repository->sitemap;
71
		foreach ($sitemap as $key => $sitemapItem) {
72
			if ($sitemapItem->slug == $slug) {
73
				unset($sitemap[$key]);
74
			}
75
		}
76
		$sitemap = array_values($sitemap);
77
		$this->repository->sitemap = $sitemap;
78
		$this->save();
79
	}
80
81
	/**
82
	 * Save changes to a sitemap item
83
	 *
84
	 * @param $postValues
85
	 *
86
	 * @throws \Exception
87
	 */
88
	public function saveSitemap($postValues)
89
	{
90
		if (isset($postValues['sitemapitem']) && is_array($postValues['sitemapitem'])) {
91
			$sitemap = array();
92
			foreach ($postValues['sitemapitem'] as $sitemapItem) {
93
				$sitemapItemObject = json_decode($sitemapItem);
94
				if (isset($sitemapItemObject->object)) {
95
					unset($sitemapItemObject->object);
96
				}
97
				$sitemap[] = $sitemapItemObject;
98
			}
99
			$this->repository->sitemap = $sitemap;
100
			$this->save();
101
		}
102
	}
103
104
	/**
105
	 * Get a sitemap item by its slug
106
	 *
107
	 * @param $slug
108
	 *
109
	 * @return mixed
110
	 */
111
	public function getSitemapItemBySlug($slug)
112
	{
113
		$sitemap = $this->repository->sitemap;
114
		foreach ($sitemap as $sitemapItem) {
115
			if ($sitemapItem->slug == $slug) {
116
				return $sitemapItem;
117
			}
118
		}
119
120
		return null;
121
	}
122
}