Completed
Branch master (e8947e)
by Andreas
15:09
created

net_nehmer_static_handler_admin   B

Complexity

Total Complexity 16

Size/Duplication

Total Lines 169
Duplicated Lines 57.4 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

Changes 0
Metric Value
dl 97
loc 169
rs 7.8571
c 0
b 0
f 0
wmc 16
lcom 1
cbo 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A _on_initialize() 0 4 1
A _load_schemadb() 12 12 4
A _load_controller() 13 13 2
B _handler_edit() 27 27 3
A save_callback() 11 11 2
A _handler_deletelink() 23 23 2
A _handler_delete() 11 11 2

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
 * @package net.nehmer.static
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * n.n.static admin page handler
11
 *
12
 * @package net.nehmer.static
13
 */
14
class net_nehmer_static_handler_admin extends midcom_baseclasses_components_handler
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
15
{
16
    /**
17
     * The content topic to use
18
     *
19
     * @var midcom_db_topic
20
     */
21
    private $_content_topic = null;
22
23
    /**
24
     * The article to operate on
25
     *
26
     * @var midcom_db_article
27
     */
28
    private $_article = null;
29
30
    /**
31
     * The schema database in use, available only while a datamanager is loaded.
32
     *
33
     * @var Array
34
     */
35
    private $_schemadb = null;
36
37
    /**
38
     * Maps the content topic from the request data to local member variables.
39
     */
40
    public function _on_initialize()
41
    {
42
        $this->_content_topic = $this->_request_data['content_topic'];
43
    }
44
45
    /**
46
     * Loads and prepares the schema database.
47
     *
48
     * Special treatment is done for the name field, which is set readonly for non-admins
49
     * if the simple_name_handling config option is set. (using an auto-generated urlname based
50
     * on the title, if it is missing.)
51
     *
52
     * The operations are done on all available schemas within the DB.
53
     */
54 View Code Duplication
    private function _load_schemadb()
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...
55
    {
56
        $this->_schemadb =& $this->_request_data['schemadb'];
57
        if (   $this->_config->get('simple_name_handling')
58
            && ! midcom::get()->auth->admin)
59
        {
60
            foreach (array_keys($this->_schemadb) as $name)
61
            {
62
                $this->_schemadb[$name]->fields['name']['readonly'] = true;
63
            }
64
        }
65
    }
66
67
    /**
68
     * Internal helper, loads the controller for the current article. Any error triggers a 500.
69
     *
70
     * @return midcom_helper_datamanager2_controller_simple
71
     */
72 View Code Duplication
    private function _load_controller()
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...
73
    {
74
        $this->_load_schemadb();
75
        $controller = midcom_helper_datamanager2_controller::create('simple');
76
        $controller->schemadb =& $this->_schemadb;
77
        $controller->set_storage($this->_article);
78
79
        if (! $controller->initialize())
80
        {
81
            throw new midcom_error("Failed to initialize a DM2 controller instance for article {$this->_article->id}.");
82
        }
83
        return $controller;
84
    }
85
86
    /**
87
     * Displays an article edit view.
88
     *
89
     * @param mixed $handler_id The ID of the handler.
90
     * @param array $args The argument list.
91
     * @param array &$data The local request data.
92
     */
93 View Code Duplication
    public function _handler_edit($handler_id, array $args, array &$data)
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...
94
    {
95
        $this->_article = new midcom_db_article($args[0]);
96
97
        // Relocate for the correct content topic, let the true content topic take care of the ACL
98
        if ($this->_article->topic !== $this->_content_topic->id)
99
        {
100
            $nap = new midcom_helper_nav();
101
            $node = $nap->get_node($this->_article->topic);
102
103
            if (!empty($node[MIDCOM_NAV_ABSOLUTEURL]))
104
            {
105
                return new midcom_response_relocate($node[MIDCOM_NAV_ABSOLUTEURL] . "edit/{$args[0]}/");
106
            }
107
            throw new midcom_error_notfound("The article with GUID {$args[0]} was not found.");
108
        }
109
110
        $this->_article->require_do('midgard:update');
111
        midcom::get()->head->set_pagetitle(sprintf($this->_l10n_midcom->get('edit %s'), $this->_article->title));
112
113
        $workflow = $this->get_workflow('datamanager2', array
114
        (
115
            'controller' => $this->_load_controller(),
116
            'save_callback' => array($this, 'save_callback')
117
        ));
118
        return $workflow->run();
119
    }
120
121 View Code Duplication
    public function save_callback(midcom_helper_datamanager2_controller $controller)
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...
122
    {
123
        // Reindex the article
124
        $indexer = midcom::get()->indexer;
125
        net_nehmer_static_viewer::index($controller->datamanager, $indexer, $this->_content_topic);
126
        if ($this->_article->name == 'index')
127
        {
128
            return '';
129
        }
130
        return $this->_article->name . '/';
131
    }
132
133
    /**
134
     * Displays article link delete confirmation
135
     *
136
     * @param mixed $handler_id The ID of the handler.
137
     * @param array $args The argument list.
138
     * @param array &$data The local request data.
139
     */
140 View Code Duplication
    public function _handler_deletelink($handler_id, array $args, array &$data)
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...
141
    {
142
        $this->_article = new midcom_db_article($args[0]);
143
144
        $qb = net_nehmer_static_link_dba::new_query_builder();
145
        $qb->add_constraint('topic', '=', $this->_content_topic->id);
146
        $qb->add_constraint('article', '=', $this->_article->id);
147
148
        if ($qb->count() === 0)
149
        {
150
            throw new midcom_error_notfound('No links were found');
151
        }
152
153
        // Get the link
154
        $results = $qb->execute_unchecked();
155
        $this->_link = $results[0];
156
        $workflow = $this->get_workflow('delete', array
157
        (
158
            'object' => $this->_link,
159
            'label' => $this->_l10n->get('article link')
160
        ));
161
        return $workflow->run();
162
    }
163
164
    /**
165
     * Displays an article delete confirmation view.
166
     *
167
     * @param mixed $handler_id The ID of the handler.
168
     * @param array $args The argument list.
169
     * @param array &$data The local request data.
170
     */
171 View Code Duplication
    public function _handler_delete($handler_id, array $args, array &$data)
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...
172
    {
173
        $this->_article = new midcom_db_article($args[0]);
174
        // Relocate to delete the link instead of the article itself
175
        if ($this->_article->topic !== $this->_content_topic->id)
176
        {
177
            return new midcom_response_relocate("delete/link/{$args[0]}/");
178
        }
179
        $workflow = $this->get_workflow('delete', array('object' => $this->_article));
180
        return $workflow->run();
181
    }
182
}
183