CloningPresenter   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 224
Duplicated Lines 11.16 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 16.94%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 26
c 3
b 0
f 0
lcom 1
cbo 6
dl 25
loc 224
ccs 21
cts 124
cp 0.1694
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A renderDefault() 0 4 1
A setPagePath() 14 14 3
A pageCreateBoxes() 0 11 2
A cloneData() 0 11 4
A removeData() 0 11 2
A createNewPage() 0 16 1
A createNewBox() 0 12 1
B createComponentCloningForm() 11 32 5
B cloningFormSubmitted() 0 54 6
A normalizeValues() 0 8 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
/**
4
 * Webcms2 admin module package.
5
 */
6
7
namespace AdminModule;
8
9
use Nette\Application\UI;
10
11
/**
12
 * @author Tomáš Voslař <tomas.voslar at webcook.cz>
13
 * @package WebCMS2
14
 */
15
class CloningPresenter extends BasePresenter
16
{
17 1
    public function renderDefault()
18
    {
19 1
        $this->reloadContent();
20 1
    }
21
22 1
    public function createComponentCloningForm()
23
    {
24 1
        $form = $this->createForm();
25
26 1
        $langs = $this->getAllLanguages();
27 1
        $packages = \WebCMS\Helpers\SystemHelper::getPackages();
28
29 1
        $form->addGroup('Copy structures');
30
31 1
        $form->addSelect('languageFrom', 'Copy from', $langs)->setRequired('Please pick up language.')->setAttribute('class', 'form-control');
0 ignored issues
show
Documentation introduced by
'form-control' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
32 1
        $form->addSelect('languageTo', 'Copy to', $langs)->setRequired('Please pick up language.')->setAttribute('class', 'form-control');
0 ignored issues
show
Documentation introduced by
'form-control' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
33 1
        $form->addCheckbox('removeData', 'Remove data?');
34
35 1
        $form->addGroup('Copy data from modules');
36
37 1 View Code Duplication
        foreach ($packages as $key => $package) {
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...
38
            if ($package['vendor'] === 'webcms2' && $package['package'] !== 'webcms2') {
39
                $object = $this->createObject($package['package']);
40
41
                if ($object->isCloneable()) {
42
                    $form->addCheckbox(str_replace('-', '_', $package['package']), $package['package']);
43
                } else {
44
                    $form->addCheckbox(str_replace('-', '_', $package['package']), $package['package'].' not clonable.')->setDisabled(true);
45
                }
46
            }
47 1
        }
48
49 1
        $form->onSuccess[] = callback($this, 'cloningFormSubmitted');
50 1
        $form->addSubmit('send', 'Clone');
51
52 1
        return $form;
53
    }
54
55
    public function cloningFormSubmitted(UI\Form $form)
56
    {
57
        $values = $form->getValues();
58
59
        $languageFrom = $this->em->getRepository('WebCMS\Entity\Language')->find($values->languageFrom);
60
        $languageTo = $this->em->getRepository('WebCMS\Entity\Language')->find($values->languageTo);
61
        $removeData = $values->removeData;
62
        
63
        $values = $this->normalizeValues($values);
64
65
        // remove data first
66
        if ($removeData) {
67
            $this->removeData($languageTo);
68
        }
69
70
        // clone page structure
71
        $transformTable = array();
72
73
        $pages = $this->em->getRepository('WebCMS\Entity\Page')->findBy(array(
74
            'language' => $languageFrom,
75
            ), array('lft' => 'asc'));
76
77
        foreach ($pages as $page) {
78
            $new = $this->createNewPage($languageTo, $page);
79
80
            if ($page->getParent()) {
81
                $new->setParent($transformTable[$page->getParent()->getId()]);
82
            }
83
84
            $this->em->persist($new);
85
            $this->em->flush();
86
87
            $new = $this->setPagePath($new);
88
89
            $this->em->flush();
90
91
            $transformTable[$page->getId()] = $new;
92
        }
93
94
        foreach ($pages as $page) {
95
            $this->pageCreateBoxes($page, $transformTable);
96
        }
97
98
        // clone all data
99
        $this->cloneData($values, $languageFrom, $languageTo, $transformTable);
100
101
        // persist all changes into database
102
        $this->em->flush();
103
104
        $this->flashMessage('Cloning has been successfuly done.', 'success');
105
        if (!$this->isAjax()) {
106
            $this->forward('Languages:cloning');
107
        }
108
    }
109
110
    /**
111
     *
112
     *
113
     * @param \WebCMS\Entity\Page $new [description]
114
     */
115 1 View Code Duplication
    private function setPagePath($new)
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
        $path = $this->em->getRepository('WebCMS\Entity\Page')->getPath($new);
118
        $final = array();
119
        foreach ($path as $p) {
120 1
            if ($p->getParent() != NULL) {
121
                $final[] = $p->getSlug();
122
            }
123
        }
124
125
        $new->setPath(implode('/', $final));
126
127
        return $new;
128
    }
129
130
    /**
131
     *
132
     *
133
     * @param  [type] $values [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
134
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
135
     */
136
    private function normalizeValues($values)
137
    {
138
        unset($values->languageFrom);
139
        unset($values->languageTo);
140
        unset($values->removeData);
141
142
        return $values;
143
    }
144
145
    /**
146
     *
147
     *
148
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
149
     */
150
    private function pageCreateBoxes($page, $transformTable)
151
    {
152
        $boxes = $this->em->getRepository('WebCMS\Entity\Box')->findBy(array(
153
            'pageTo' => $page,
154
        ));
155
156
        foreach ($boxes as $box) {
157
            $newBox = $this->createNewBox($box, $transformTable);
158
            $this->em->persist($newBox);
159
        }
160
    }
161
162
    /**
163
     *
164
     *
165
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
166
     */
167
    private function cloneData($values, $languageFrom, $languageTo, $transformTable)
168
    {
169
        foreach ($values as $key => $value) {
170
            if ($value) {
171
                $module = $this->createObject(str_replace('_', '-', $key));
172
                if ($module->isCloneable()) {
173
                    $module->cloneData($this->em, $languageFrom, $languageTo, $transformTable);
174
                }
175
            }
176
        }
177
    }
178
179
    /**
180
     *
181
     *
182
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
183
     */
184
    private function removeData($languageTo)
185
    {
186
        $pages = $this->em->getRepository('WebCMS\Entity\Page')->findBy(array(
187
            'language' => $languageTo,
188
            'parent' => NULL,
189
        ));
190
191
        foreach ($pages as $page) {
192
            $this->em->remove($page);
193
        }
194
    }
195
196
    /**
197
     *
198
     *
199
     * @param  [type] $languageTo [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
200
     * @param  [type] $page       [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
201
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
202
     */
203
    private function createNewPage($languageTo, $page)
204
    {
205
        $new = new \WebCMS\Entity\Page();
206
        $new->setLanguage($languageTo);
207
        $new->setTitle($page->getTitle());
208
        $new->setPresenter($page->getPresenter());
209
        $new->setPath('tmp');
210
        $new->setVisible($page->getVisible());
211
        $new->setDefault($page->getDefault());
212
        $new->setClass($page->getClass());
213
        $new->setModule($page->getModule());
214
        $new->setModuleName($page->getModuleName());
215
        $new->setLayout($page->getLayout());
216
217
        return $new;
218
    }
219
220
    /**
221
     *
222
     *
223
     * @param  [type] $box [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
224
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
225
     */
226 1
    private function createNewBox($box, $transformTable)
227
    {
228
        $newBox = new \WebCMS\Entity\Box();
229
        $newBox->setBox($box->getBox());
230
        $newBox->setFunction($box->getFunction());
231
        $newBox->setModuleName($box->getModuleName());
232
        $newBox->setPresenter($box->getPresenter());
233
        $newBox->setPageFrom($transformTable[$box->getPageFrom()->getId()]);
234
        $newBox->setPageTo($transformTable[$box->getPageTo()->getId()]);
235
236
        return $newBox;
237 1
    }
238
}
239