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

net_nehmer_static_viewer   B

Complexity

Total Complexity 17

Size/Duplication

Total Lines 174
Duplicated Lines 32.18 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
dl 56
loc 174
rs 8.4614
c 0
b 0
f 0
wmc 17
lcom 1
cbo 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A _on_initialize() 0 23 2
A _on_handle() 0 6 1
A get_topic_qb() 0 20 2
A _determine_content_topic() 19 19 3
A index() 20 20 2
C _populate_node_toolbar() 17 44 7

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 site interface class
11
 *
12
 * @package net.nehmer.static
13
 */
14
class net_nehmer_static_viewer extends midcom_baseclasses_components_request
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 topic in which to look for articles. This defaults to the current content topic
18
     * unless overridden by the symlink topic feature.
19
     *
20
     * @var midcom_db_topic
21
     */
22
    private $_content_topic = null;
23
24
    /**
25
     * Initialize the request switch and the content topic.
26
     */
27
    public function _on_initialize()
28
    {
29
        $this->_determine_content_topic();
30
        $this->_request_data['content_topic'] = $this->_content_topic;
31
32
        // View mode handler, set index viewer according to autoindex setting.
33
        // These, especially the general view handler, must come last, otherwise we'll hide other
34
        // handlers
35
        if ($this->_config->get('autoindex'))
36
        {
37
            $this->_request_switch['autoindex'] = array
38
            (
39
                'handler' => array('net_nehmer_static_handler_autoindex', 'autoindex'),
40
            );
41
        }
42
        else
43
        {
44
            $this->_request_switch['index'] = array
45
            (
46
                'handler' => array('net_nehmer_static_handler_view', 'view'),
47
            );
48
        }
49
    }
50
51
    /**
52
     * Set the content topic to use. This will check against the configuration setting 'symlink_topic'.
53
     */
54 View Code Duplication
    private function _determine_content_topic()
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
        $guid = $this->_config->get('symlink_topic');
57
        if (is_null($guid))
58
        {
59
            // No symlink topic
60
            // Workaround, we should talk to a DBA object automatically here in fact.
61
            $this->_content_topic = midcom_db_topic::get_cached($this->_topic->id);
62
            return;
63
        }
64
65
        $this->_content_topic = midcom_db_topic::get_cached($guid);
66
        // Validate topic.
67
        if ($this->_content_topic->component != 'net.nehmer.static')
68
        {
69
            debug_print_r('Retrieved topic was:', $this->_content_topic);
70
            throw new midcom_error('Symlink content topic is invalid, see the debug level log for details.');
71
        }
72
    }
73
74
    /**
75
     * Indexes an article.
76
     *
77
     * @param midcom_helper_datamanager2_datamanager $dm The Datamanager encapsulating the event.
78
     * @param midcom_services_indexer $indexer The indexer instance to use.
79
     * @param midcom_db_topic $topic The topic which we are bound to. If this is not an object, the code
80
     *     tries to load a new topic instance from the database identified by this parameter.
81
     */
82 View Code Duplication
    public static function index($dm, $indexer, $topic)
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
        if (!is_object($topic))
85
        {
86
            $topic = new midcom_db_topic($topic);
87
        }
88
89
        // Don't index directly, that would lose a reference due to limitations
90
        // of the index() method. Needs fixes there.
91
92
        $nav = new midcom_helper_nav();
93
        $node = $nav->get_node($topic->id);
94
95
        $document = $indexer->new_document($dm);
96
        $document->topic_guid = $topic->guid;
97
        $document->topic_url = $node[MIDCOM_NAV_FULLURL];
98
        $document->read_metadata_from_object($dm->storage->object);
99
        $document->component = $topic->component;
100
        $indexer->index($document);
101
    }
102
103
    /**
104
     * Populates the node toolbar depending on the user's rights.
105
     */
106
    private function _populate_node_toolbar()
107
    {
108
        $buttons = array();
109
        $workflow = $this->get_workflow('datamanager2');
110 View Code Duplication
        if ($this->_content_topic->can_do('midgard:create'))
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...
111
        {
112
            foreach (array_keys($this->_request_data['schemadb']) as $name)
113
            {
114
                $buttons[] = $workflow->get_button("create/{$name}/", array
115
                (
116
                    MIDCOM_TOOLBAR_LABEL => sprintf
117
                    (
118
                        $this->_l10n_midcom->get('create %s'),
119
                        $this->_l10n->get($this->_request_data['schemadb'][$name]->description)
120
                    ),
121
                    MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/attach.png',
122
                    MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/new-text.png',
123
                    MIDCOM_TOOLBAR_ACCESSKEY => 'n',
124
                ));
125
            }
126
        }
127
128
        if (   $this->_config->get('enable_article_links')
129
            && $this->_content_topic->can_do('midgard:create'))
130
        {
131
            $buttons[] = $workflow->get_button("create/link/", array
132
            (
133
                MIDCOM_TOOLBAR_LABEL => sprintf($this->_l10n_midcom->get('create %s'), $this->_l10n->get('article link')),
134
                MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/attach.png',
135
            ));
136
        }
137
138
        if (   $this->_topic->can_do('midgard:update')
139
            && $this->_topic->can_do('midcom:component_config'))
140
        {
141
            $buttons[] = $workflow->get_button('config/', array
142
            (
143
                MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('component configuration'),
144
                MIDCOM_TOOLBAR_HELPTEXT => $this->_l10n_midcom->get('component configuration helptext'),
145
                MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/stock_folder-properties.png',
146
            ));
147
        }
148
        $this->_node_toolbar->add_items($buttons);
149
    }
150
151
    /**
152
     * The handle callback populates the toolbars.
153
     */
154
    public function _on_handle($handler, array $args)
155
    {
156
        $this->_request_data['schemadb'] = midcom_helper_datamanager2_schema::load_database($this->_config->get('schemadb'));
157
158
        $this->_populate_node_toolbar();
159
    }
160
161
    /**
162
     *
163
     * @param midcom_helper_configuration $config
164
     * @param integer $id The topic ID
165
     * @return midcom_core_querybuilder The querybuilder instance
166
     */
167
    public static function get_topic_qb(midcom_helper_configuration $config, $id)
168
    {
169
        $qb = midcom_db_article::new_query_builder();
170
171
        // Include the article links to the indexes if enabled
172
        if ($config->get('enable_article_links'))
173
        {
174
            $mc = net_nehmer_static_link_dba::new_collector('topic', $id);
175
176
            $qb->begin_group('OR');
177
                $qb->add_constraint('id', 'IN', $mc->get_values('article'));
178
                $qb->add_constraint('topic', '=', $id);
179
            $qb->end_group();
180
        }
181
        else
182
        {
183
            $qb->add_constraint('topic', '=', $id);
184
        }
185
        return $qb;
186
    }
187
}
188