Passed
Push — master ( f61691...1918f3 )
by Jens
07:18
created

ValuelistRouting   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 83
Duplicated Lines 16.87 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 14
loc 83
rs 10
c 1
b 0
f 0
wmc 13
lcom 0
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 12 7
A valuelistsRoute() 0 6 1
A newValuelistRoute() 0 12 2
A editValuelistRoute() 7 16 2
A deleteValuelistRoute() 7 7 1

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
 * Created by jensk on 4-9-2017.
4
 */
5
6
namespace CloudControl\Cms\components\cms;
7
8
9
use CloudControl\Cms\cc\Request;
10
use CloudControl\Cms\components\CmsComponent;
11
12
class ValuelistRouting implements CmsRouting
13
{
14
15
    /**
16
     * CmsRouting constructor.
17
     *
18
     * @param Request $request
19
     * @param string $relativeCmsUri
20
     * @param CmsComponent $cmsComponent
21
     */
22
    public function __construct(Request $request, $relativeCmsUri, CmsComponent $cmsComponent)
23
    {
24
        if ($relativeCmsUri == '/valuelists') {
25
            $this->valuelistsRoute($cmsComponent);
26
        } elseif ($relativeCmsUri == '/valuelists/new') {
27
            $this->newValuelistRoute($request, $cmsComponent);
28
        } elseif ($relativeCmsUri == '/valuelists/edit' && isset($request::$get[CmsConstants::GET_PARAMETER_SLUG])) {
29
            $this->editValuelistRoute($request, $cmsComponent);
30
        } elseif ($relativeCmsUri == '/valuelists/delete' && isset($request::$get[CmsConstants::GET_PARAMETER_SLUG])) {
31
            $this->deleteValuelistRoute($request, $cmsComponent);
32
        }
33
    }
34
35
    /**
36
     * @param CmsComponent $cmsComponent
37
     */
38
    private function valuelistsRoute($cmsComponent)
39
    {
40
        $cmsComponent->subTemplate = 'valuelists';
41
        $cmsComponent->setParameter(CmsConstants::PARAMETER_VALUELISTS, $cmsComponent->storage->getValuelists()->getValuelists());
42
        $cmsComponent->setParameter(CmsConstants::PARAMETER_MAIN_NAV_CLASS, CmsConstants::PARAMETER_VALUELISTS);
43
    }
44
45
    /**
46
     * @param Request $request
47
     * @param CmsComponent $cmsComponent
48
     */
49
    private function newValuelistRoute($request, $cmsComponent)
50
    {
51
        $cmsComponent->subTemplate = 'valuelists/form';
52
        $cmsComponent->setParameter(CmsConstants::PARAMETER_MAIN_NAV_CLASS, CmsConstants::PARAMETER_VALUELISTS);
53
        if (isset($request::$post[CmsConstants::POST_PARAMETER_TITLE])) {
54
            $slug = $cmsComponent->storage->getValuelists()->addValuelist($request::$post);
55
            $docLink = $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/valuelists/edit?slug=' . $slug;
56
            $cmsComponent->storage->getActivityLog()->add('created valuelist <a href="' . $docLink . '">' . $request::$post[CmsConstants::POST_PARAMETER_TITLE] . '</a>', 'plus');
57
            header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/valuelists');
58
            exit;
59
        }
60
    }
61
62
    /**
63
     * @param Request $request
64
     * @param CmsComponent $cmsComponent
65
     */
66
    private function editValuelistRoute($request, $cmsComponent)
67
    {
68
        $cmsComponent->subTemplate = 'valuelists/form';
69
        $folder = $cmsComponent->storage->getValuelists()->getValuelistBySlug($request::$get[CmsConstants::GET_PARAMETER_SLUG]);
70
71 View Code Duplication
        if (isset($request::$post[CmsConstants::POST_PARAMETER_TITLE], $request::$get[CmsConstants::GET_PARAMETER_SLUG])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
            $cmsComponent->storage->getValuelists()->saveValuelist($request::$get[CmsConstants::GET_PARAMETER_SLUG], $request::$post);
73
            $docLink = $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents/valuelists/edit?slug=' . $request::$get[CmsConstants::GET_PARAMETER_SLUG];
74
            $cmsComponent->storage->getActivityLog()->add('edited valuelist <a href="' . $docLink . '">' . $request::$post[CmsConstants::POST_PARAMETER_TITLE] . '</a>', 'pencil');
75
            header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/valuelists');
76
            exit;
77
        }
78
79
        $cmsComponent->setParameter(CmsConstants::PARAMETER_MAIN_NAV_CLASS, CmsConstants::PARAMETER_VALUELISTS);
80
        $cmsComponent->setParameter(CmsConstants::PARAMETER_VALUELIST, $folder);
81
    }
82
83
    /**
84
     * @param Request $request
85
     * @param CmsComponent $cmsComponent
86
     */
87 View Code Duplication
    private function deleteValuelistRoute($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...
88
    {
89
        $cmsComponent->storage->getValuelists()->deleteValuelistBySlug($request::$get[CmsConstants::GET_PARAMETER_SLUG]);
90
        $cmsComponent->storage->getActivityLog()->add('deleted valuelist ' . $request::$get[CmsConstants::GET_PARAMETER_SLUG], 'trash');
91
        header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/valuelists');
92
        exit;
93
    }
94
}