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

net_nehmer_blog_handler_link   B

Complexity

Total Complexity 12

Size/Duplication

Total Lines 135
Duplicated Lines 51.85 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
dl 70
loc 135
rs 8.4614
c 0
b 0
f 0
wmc 12
lcom 1
cbo 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A _on_initialize() 0 4 1
A load_schemadb() 0 4 1
A get_schema_name() 0 4 1
A save_callback() 0 8 1
A get_schema_defaults() 13 13 2
A dm2_create_callback() 13 13 2
A _handler_create() 19 19 2
B _handler_delete() 25 25 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.blog
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.blog link handler
11
 *
12
 * @package net.nehmer.blog
13
 */
14
class net_nehmer_blog_handler_link 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
implements midcom_helper_datamanager2_interfaces_create
1 ignored issue
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
16
{
17
    /**
18
     * The content topic to use
19
     *
20
     * @var midcom_db_topic
21
     */
22
    private $_content_topic = null;
23
24
    /**
25
     * The article link which has been created
26
     *
27
     * @var net_nehmer_blog_link_dba
28
     */
29
    private $_link = null;
30
31
    /**
32
     * Maps the content topic from the request data to local member variables.
33
     */
34
    public function _on_initialize()
35
    {
36
        $this->_content_topic = $this->_request_data['content_topic'];
37
    }
38
39
    public function load_schemadb()
40
    {
41
        return midcom_helper_datamanager2_schema::load_database($this->_config->get('schemadb_link'));
42
    }
43
44
    public function get_schema_name()
45
    {
46
        return 'link';
47
    }
48
49 View Code Duplication
    public function get_schema_defaults()
0 ignored issues
show
Coding Style introduced by
get_schema_defaults uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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...
50
    {
51
        $defaults = array();
52
        if (isset($_GET['article']))
53
        {
54
            $defaults['article'] = $_GET['article'];
55
        }
56
        else
57
        {
58
            $defaults['topic'] = $this->_topic->id;
59
        }
60
        return $defaults;
61
    }
62
63
    /**
64
     * DM2 creation callback, binds to the current content topic.
65
     */
66 View Code Duplication
    public function &dm2_create_callback (&$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...
67
    {
68
        $this->_link = new net_nehmer_blog_link_dba();
69
        $this->_link->topic = $this->_topic->id;
70
71
        if (!$this->_link->create())
72
        {
73
            debug_print_r('We operated on this object:', $this->_link);
74
            throw new midcom_error('Failed to create a new article. Last Midgard error was: '. midcom_connection::get_error_string());
75
        }
76
77
        return $this->_link;
78
    }
79
80
    /**
81
     * Displays an article edit view.
82
     *
83
     * @param mixed $handler_id The ID of the handler.
84
     * @param array $args The argument list.
85
     * @param array &$data The local request data.
86
     */
87 View Code Duplication
    public function _handler_create($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...
88
    {
89
        $this->_content_topic->require_do('midgard:create');
90
91
        if (!$this->_config->get('enable_article_links'))
92
        {
93
            throw new midcom_error_notfound('Article linking disabled');
94
        }
95
96
        $workflow = $this->get_workflow('datamanager2', array
97
        (
98
            'controller' => $this->get_controller('create'),
99
            'save_callback' => array($this, 'save_callback')
100
        ));
101
102
        midcom::get()->head->set_pagetitle(sprintf($this->_l10n_midcom->get('create %s'), $this->_l10n->get('article link')));
103
104
        return $workflow->run();
105
    }
106
107
    public function save_callback(midcom_helper_datamanager2_controller $controller)
108
    {
109
        // Reindex the article
110
        $indexer = midcom::get()->indexer;
111
        net_nehmer_blog_viewer::index($controller->datamanager, $indexer, $this->_content_topic);
112
        $article = new midcom_db_article($this->_link->article);
113
        return $article->name . '/';
114
    }
115
116
    /**
117
     * Displays article link delete confirmation
118
     *
119
     * @param mixed $handler_id The ID of the handler.
120
     * @param array $args The argument list.
121
     * @param array &$data The local request data.
122
     */
123 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...
124
    {
125
        $article = new midcom_db_article($args[0]);
126
127
        $qb = net_nehmer_blog_link_dba::new_query_builder();
128
        $qb->add_constraint('topic', '=', $this->_content_topic->id);
129
        $qb->add_constraint('article', '=', $article->id);
130
131
        if ($qb->count() === 0)
132
        {
133
            throw new midcom_error_notfound('No links were found');
134
        }
135
136
        // Get the link
137
        $results = $qb->execute_unchecked();
138
        $this->_link = $results[0];
139
        $this->_link->require_do('midgard:delete');
140
141
        $workflow = $this->get_workflow('delete', array
142
        (
143
            'object' => $this->_link,
144
            'label' => $this->_l10n->get('article link')
145
        ));
146
        return $workflow->run();
147
    }
148
}
149