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

FolderRouting   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 76
Duplicated Lines 28.95 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 22
loc 76
rs 10
c 1
b 0
f 0
wmc 12
lcom 0
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 3 10 7
A newFolderRoute() 6 11 2
A editFolderRoute() 6 22 2
A deleteFolderRoute() 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\document {
7
8
9
    use CloudControl\Cms\cc\Request;
10
    use CloudControl\Cms\components\cms\CmsConstants;
11
    use CloudControl\Cms\components\cms\CmsRouting;
12
    use CloudControl\Cms\components\CmsComponent;
13
14
    class FolderRouting implements CmsRouting
15
    {
16
17
        /**
18
         * CmsRouting constructor.
19
         *
20
         * @param Request $request
21
         * @param string $relativeCmsUri
22
         * @param CmsComponent $cmsComponent
23
         */
24
        public function __construct(Request $request, $relativeCmsUri, CmsComponent $cmsComponent)
25
        {
26
            if ($relativeCmsUri == '/documents/new-folder' && isset($request::$get[CmsConstants::GET_PARAMETER_PATH])) {
27
                $this->newFolderRoute($request, $cmsComponent);
28
            } else if ($relativeCmsUri == '/documents/edit-folder' && isset($request::$get[CmsConstants::GET_PARAMETER_SLUG])) {
29
                $this->editFolderRoute($request, $cmsComponent);
30 View Code Duplication
            } else if ($relativeCmsUri == '/documents/delete-folder' && isset($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...
31
                $this->deleteFolderRoute($request, $cmsComponent);
32
            }
33
        }
34
35
        /**
36
         * @param Request $request
37
         * @param CmsComponent $cmsComponent
38
         */
39
        private function newFolderRoute($request, $cmsComponent)
40
        {
41
            $cmsComponent->subTemplate = 'documents/folder-form';
42
            $cmsComponent->setParameter(CmsConstants::PARAMETER_MAIN_NAV_CLASS, CmsConstants::PARAMETER_DOCUMENTS);
43 View Code Duplication
            if (isset($request::$post[CmsConstants::POST_PARAMETER_TITLE], $request::$post[CmsConstants::GET_PARAMETER_PATH])) {
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...
44
                $cmsComponent->storage->getDocuments()->addDocumentFolder($request::$post);
45
                $cmsComponent->storage->getActivityLog()->add('created folder ' . $request::$post[CmsConstants::POST_PARAMETER_TITLE] . ' in path ' . $request::$get[CmsConstants::GET_PARAMETER_PATH], 'plus');
46
                header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents');
47
                exit;
48
            }
49
        }
50
51
        /**
52
         * @param Request $request
53
         * @param CmsComponent $cmsComponent
54
         */
55
        private function editFolderRoute($request, $cmsComponent)
56
        {
57
            $cmsComponent->subTemplate = 'documents/folder-form';
58
            $folder = $cmsComponent->storage->getDocuments()->getDocumentFolderBySlug($request::$get[CmsConstants::GET_PARAMETER_SLUG]);
59
60
            $path = $request::$get[CmsConstants::GET_PARAMETER_SLUG];
61
            $path = explode('/', $path);
62
            array_pop($path);
63
            $path = implode('/', $path);
64
65
            $request::$get[CmsConstants::GET_PARAMETER_PATH] = '/' . $path;
66
67 View Code Duplication
            if (isset($request::$post[CmsConstants::POST_PARAMETER_TITLE], $request::$post['content'])) {
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...
68
                $cmsComponent->storage->getDocuments()->addDocumentFolder($request::$post);
69
                $cmsComponent->storage->getActivityLog()->add('edited folder ' . $request::$post[CmsConstants::POST_PARAMETER_TITLE] . ' in path ' . $request::$get[CmsConstants::GET_PARAMETER_PATH], 'pencil');
70
                header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents');
71
                exit;
72
            }
73
74
            $cmsComponent->setParameter(CmsConstants::PARAMETER_MAIN_NAV_CLASS, CmsConstants::PARAMETER_DOCUMENTS);
75
            $cmsComponent->setParameter(CmsConstants::PARAMETER_FOLDER, $folder);
76
        }
77
78
        /**
79
         * @param Request $request
80
         * @param CmsComponent $cmsComponent
81
         */
82 View Code Duplication
        private function deleteFolderRoute($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...
83
        {
84
            $cmsComponent->storage->getDocuments()->deleteDocumentFolderBySlug($request::$get[CmsConstants::GET_PARAMETER_SLUG]);
85
            $cmsComponent->storage->getActivityLog()->add('deleted folder /' . $request::$get[CmsConstants::GET_PARAMETER_SLUG], 'trash');
86
            header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents?folder-delete');
87
            exit;
88
        }
89
    }
90
}