Passed
Push — develop ( bdd28d...3cabcb )
by Jens
04:17
created

SitemapRouting::redirectEditRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by IntelliJ IDEA.
4
 * User: Jens
5
 * Date: 29-1-2017
6
 * Time: 16:30
7
 */
8
9
namespace library\components\cms;
10
11
12
use library\components\CmsComponent;
13
14
class SitemapRouting implements CmsRouting
15
{
16
17
    /**
18
     * SitemapRouting constructor.
19
     * @param \library\cc\Request $request
20
     * @param mixed|string $relativeCmsUri
21
     * @param CmsComponent $cmsComponent
22
     */
23 View Code Duplication
    public function __construct($request, $relativeCmsUri, $cmsComponent)
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...
24
    {
25
        if ($relativeCmsUri == '/sitemap') {
26
			$this->overviewRoute($request, $cmsComponent);
27
        } elseif ($relativeCmsUri == '/sitemap/new') {
28
			$this->newRoute($request, $cmsComponent);
29
        } elseif ($relativeCmsUri == '/sitemap/edit' && isset($request::$get[CmsComponent::GET_PARAMETER_SLUG])) {
30
			$this->editRoute($request, $cmsComponent);
31
        } elseif ($relativeCmsUri == '/sitemap/delete' && isset($request::$get[CmsComponent::GET_PARAMETER_SLUG])) {
32
			$this->deleteRoute($request, $cmsComponent);
33
        } else {
34
            $this->redirectRoutes($relativeCmsUri, $request, $cmsComponent);
35
        }
36
    }
37
38
	/**
39
	 * @param $request
40
	 * @param CmsComponent $cmsComponent
41
	 */
42
	private function overviewRoute($request, $cmsComponent)
43
	{
44
		$cmsComponent->subTemplate = 'cms/sitemap';
45
		if (isset($request::$post[CmsComponent::POST_PARAMETER_SAVE])) {
46
			$cmsComponent->storage->getSitemap()->saveSitemap($request::$post);
47
		}
48
		$cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_SITEMAP);
49
		$cmsComponent->setParameter(CmsComponent::PARAMETER_SITEMAP, $cmsComponent->storage->getSitemap()->getSitemap());
50
	}
51
52
	/**
53
	 * @param $request
54
	 * @param CmsComponent $cmsComponent
55
	 */
56 View Code Duplication
	private function newRoute($request, $cmsComponent)
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...
57
	{
58
		$cmsComponent->subTemplate = 'cms/sitemap/form';
59
		$cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_SITEMAP);
60
		if (isset($request::$post[CmsComponent::POST_PARAMETER_TITLE], $request::$post[CmsComponent::POST_PARAMETER_TEMPLATE], $request::$post[CmsComponent::POST_PARAMETER_COMPONENT])) {
61
			$cmsComponent->storage->getSitemap()->addSitemapItem($request::$post);
62
			header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsComponent::PARAMETER_CMS_PREFIX) . '/sitemap');
63
			exit;
64
		}
65
	}
66
67
	/**
68
	 * @param $request
69
	 * @param CmsComponent $cmsComponent
70
	 */
71 View Code Duplication
	private function editRoute($request, $cmsComponent)
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...
72
	{
73
		$cmsComponent->subTemplate = 'cms/sitemap/form';
74
		$cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_SITEMAP);
75
		$sitemapItem = $cmsComponent->storage->getSitemap()->getSitemapItemBySlug($request::$get[CmsComponent::GET_PARAMETER_SLUG]);
76
		if (isset($request::$post[CmsComponent::POST_PARAMETER_TITLE], $request::$post[CmsComponent::POST_PARAMETER_TEMPLATE], $request::$post[CmsComponent::POST_PARAMETER_COMPONENT])) {
77
			$cmsComponent->storage->getSitemap()->saveSitemapItem($request::$get[CmsComponent::GET_PARAMETER_SLUG], $request::$post);
78
			header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsComponent::PARAMETER_CMS_PREFIX) . '/sitemap');
79
			exit;
80
		}
81
		$cmsComponent->setParameter(CmsComponent::PARAMETER_SITEMAP_ITEM, $sitemapItem);
82
	}
83
84
	/**
85
	 * @param $request
86
	 * @param CmsComponent $cmsComponent
87
	 */
88
	private function deleteRoute($request, $cmsComponent)
89
	{
90
		$cmsComponent->storage->getSitemap()->deleteSitemapItemBySlug($request::$get[CmsComponent::GET_PARAMETER_SLUG]);
91
		header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsComponent::PARAMETER_CMS_PREFIX) . '/sitemap');
92
		exit;
93
	}
94
95
    private function redirectRoutes($relativeCmsUri, $request, $cmsComponent)
96
    {
97
        if ($relativeCmsUri == '/sitemap/redirects') {
98
            $this->redirectsOverviewRoute($cmsComponent);
99
        } elseif ($relativeCmsUri == '/sitemap/redirects/new') {
100
            $this->redirectsNewRoute($request, $cmsComponent);
101
        } elseif ($relativeCmsUri == '/sitemap/redirects/edit' && isset($request::$get[CmsComponent::GET_PARAMETER_SLUG])) {
102
            $this->redirectEditRoute($request, $cmsComponent);
103
        } elseif ($relativeCmsUri == '/sitemap/redirects/delete' && isset($request::$get[CmsComponent::GET_PARAMETER_SLUG])) {
104
            $this->redirectDeleteRoute($request, $cmsComponent);
105
        }
106
    }
107
108
    private function redirectsOverviewRoute($cmsComponent)
109
    {
110
        $cmsComponent->subTemplate = 'cms/sitemap/redirects';
111
        $cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_SITEMAP);
112
        $cmsComponent->setParameter(CmsComponent::PARAMETER_REDIRECTS, $cmsComponent->storage->getRedirects()->getRedirects());
113
    }
114
115 View Code Duplication
    private function redirectsNewRoute($request, $cmsComponent)
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...
116
    {
117
        $cmsComponent->subTemplate = 'cms/sitemap/redirects-form';
118
        $cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_SITEMAP);
119
        if (isset($request::$post[CmsComponent::POST_PARAMETER_TITLE], $request::$post[CmsComponent::POST_PARAMETER_FROM_URL], $request::$post[CmsComponent::POST_PARAMETER_TO_URL])) {
120
            $cmsComponent->storage->getRedirects()->addRedirect($request::$post);
121
            header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsComponent::PARAMETER_CMS_PREFIX) . '/sitemap/redirects');
122
            exit;
123
        }
124
    }
125
126 View Code Duplication
    private function redirectEditRoute($request, $cmsComponent)
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...
127
    {
128
        $cmsComponent->subTemplate = 'cms/sitemap/redirects-form';
129
        $cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_SITEMAP);
130
        $redirect = $cmsComponent->storage->getRedirects()->getRedirectBySlug($request::$get[CmsComponent::GET_PARAMETER_SLUG]);
131
        if (isset($request::$post[CmsComponent::POST_PARAMETER_TITLE], $request::$post[CmsComponent::POST_PARAMETER_FROM_URL], $request::$post[CmsComponent::POST_PARAMETER_TO_URL])) {
132
            $cmsComponent->storage->getRedirects()->saveRedirect($request::$get[CmsComponent::GET_PARAMETER_SLUG], $request::$post);
133
            header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsComponent::PARAMETER_CMS_PREFIX) . '/sitemap/redirects');
134
            exit;
135
        }
136
        $cmsComponent->setParameter(CmsComponent::PARAMETER_REDIRECT, $redirect);
137
    }
138
139
    private function redirectDeleteRoute($request, $cmsComponent)
140
    {
141
        $cmsComponent->storage->getRedirects()->deleteRedirectBySlug($request::$get[CmsComponent::GET_PARAMETER_SLUG]);
142
        header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsComponent::PARAMETER_CMS_PREFIX) . '/sitemap/redirects');
143
        exit;
144
    }
145
}