Share   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 206
Duplicated Lines 19.9 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 41
loc 206
rs 9.92
c 0
b 0
f 0
wmc 31
lcom 1
cbo 4

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 1
A index() 0 9 1
A _install() 0 5 2
A _deinstall() 0 5 2
F _make_share_form() 0 58 15
A _make_like_buttons_fb() 0 17 2
A _make_like_buttons_vk() 0 26 3
A _make_like_buttons_google() 18 18 2
A _make_like_buttons_twitter() 13 13 2
A _make_like_buttons() 0 10 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
use CMSFactory\assetManager;
4
use core\src\CoreFactory;
5
6
if (!defined('BASEPATH')) {
7
    exit('No direct script access allowed');
8
}
9
10
/**
11
 * Image CMS
12
 *
13
 * Comments component
14
 * @link http://api.yandex.ru/share/
15
 */
16
class Share extends MY_Controller
17
{
18
19
    /**
20
     * @var array|mixed
21
     */
22
    public $settings = [];
23
24
    public static $i;
25
26
    /**
27
     * Share constructor.
28
     */
29 View Code Duplication
    public function __construct() {
30
31
        parent::__construct();
32
        $lang = new MY_Lang();
33
        $lang->load('share');
34
        $this->load->module('core');
35
36
        $this->db->select('settings');
37
        $this->settings = unserialize(implode(',', $this->db->get_where('components', ['name' => 'share'])->row_array()));
38
    }
39
40
    /**
41
     * Default function to access module by URL
42
     */
43
    public function index() {
44
45
        $this->template->add_array(
46
            'ss',
47
            [
48
             'html' => $this->_make_share_form(),
49
            ]
50
        );
51
    }
52
53
    public function _install() {
54
55
        $this->load->dbforge();
56
        ($this->dx_auth->is_admin()) OR exit;
57
    }
58
59
    public function _deinstall() {
60
61
        $this->load->dbforge();
62
        ($this->dx_auth->is_admin()) OR exit;
63
    }
64
65
    /**
66
     * @param string $url
67
     * @return string
68
     */
69
    public function _make_share_form($url = '') {
70
71
        $url = $url ?: site_url(CoreFactory::getUrlParser()->getFullUrl(false));
72
73
        $settings = $this->settings;
74
        $services = '';
75
        if ($settings['yaru'] == 1) {
76
            $services .= 'yaru,';
77
        }
78
        if ($settings['vkcom'] == 1) {
79
            $services .= 'vkontakte,';
80
        }
81
        if ($settings['facebook'] == 1) {
82
            $services .= 'facebook,';
83
        }
84
        if ($settings['twitter'] == 1) {
85
            $services .= 'twitter,';
86
        }
87
        if ($settings['odnoclass'] == 1) {
88
            $services .= 'odnoklassniki,';
89
        }
90
        if ($settings['myworld'] == 1) {
91
            $services .= 'moimir,';
92
        }
93
        if ($settings['lj'] == 1) {
94
            $services .= 'lj,';
95
        }
96
        if ($settings['ff'] == 1) {
97
            $services .= 'friendfeed,';
98
        }
99
        if ($settings['mc'] == 1) {
100
            $services .= 'moikrug,';
101
        }
102
        if ($settings['gg'] == 1) {
103
            $services .= 'gplus,';
104
        }
105
106
        if ($settings['type'] == 'counter') {
107
            $type = ' data-counter=""';
108
        } elseif ($settings['type'] == 'button') {
109
            $type = '';
110
        } elseif ($settings['type'] == 'icon') {
111
            $type = ' data-limit="3"';
112
        }
113
114
        \CI_Controller::get_instance()
115
            ->template
116
            ->registerJsScript('<script async="async" type="text/javascript" src="//yastatic.net/es5-shims/0.0.2/es5-shims.min.js" charset="utf-8"></script>', 'before');
117
        \CI_Controller::get_instance()
118
            ->template
119
            ->registerJsScript('<script type="text/javascript" src="//yastatic.net/share2/share.js" charset="utf-8"></script>', 'before');
120
121
        $html = '<div class="ya-share2" data-lang="ru" data-url="' . $url . '"'
122
            . $type
123
            . ' data-services="' . $services . '"></div>';
124
125
        return $html;
126
    }
127
128
    /**
129
     * @param string $url
130
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
131
     */
132
    public function _make_like_buttons_fb($url = '') {
133
134
        $settings = $this->settings;
135
136
        if ($settings['facebook_like'] == 1) {
137
            $fbsrc = '(function(d, s, id) {
138
                        var js, fjs = d.getElementsByTagName(s)[0];
139
                        if (d.getElementById(id)) return;
140
                        js = d.createElement(s); js.id = id;
141
                        js.src = "//connect.facebook.net/en_EN/all.js#xfbml=1";
142
                        fjs.parentNode.insertBefore(js, fjs);
143
                        }(document, "script", "facebook-jssdk"));';
144
            assetManager::create()->registerJsScript($fbsrc, FALSE, 'before');
145
            return '<div id="fb-root"></div>
146
                <div class="fb-like" data-send="false" data-layout="button_count" data-width="60" data-show-faces="true" data-href="' . $url . '"></div>';
147
        }
148
    }
149
150
    public function _make_like_buttons_vk($url = '') {
151
152
        self::$i++;
153
        $settings = $this->settings;
154
155
        if ($settings['vk_like'] == 1 && $settings['vk_apiid']) {
156
            \CI_Controller::get_instance()->template->registerJsScript(
157
                "<script type='text/javascript' src='//vk.com/js/api/openapi.js?101'></script>
158
                        <script type='text/javascript'>
159
                          VK.init({apiId: '{$settings['vk_apiid']}', onlyWidgets: true});
160
                        </script>",
161
                'before'
162
            );
163
164
            return "<!-- Put this div tag to the place, where the Like block will be -->
165
                        <div id='vk_like" . self::$i . "'></div>
166
                        <script type='text/javascript'>
167
                        VK.Widgets.Like('vk_like" . self::$i . "', {
168
                                                    type: 'mini', 
169
                                                    height: 18,
170
                                                    pageUrl: '$url'
171
                                                    }
172
                                                );
173
                        </script>";
174
        }
175
    }
176
177 View Code Duplication
    public function _make_like_buttons_google($url = '') {
178
179
        $settings = $this->settings;
180
181
        if ($settings['gg_like'] == 1) {
182
            \CI_Controller::get_instance()->template->registerJsScript(
183
                "<script type='text/javascript' src='https://apis.google.com/js/plusone.js'>
184
                          {lang: 'ru', parsetags: 'explicit' }
185
                        </script>",
186
                'before'
187
            );
188
            return "<!-- Place this tag where you want the +1 button to render. -->
189
                        <div class='g-plusone' data-size='medium' data-href='$url'></div>
190
191
                        <!-- Place this render call where appropriate. -->
192
                        <script type='text/javascript'>gapi.plusone.go();</script>";
193
        }
194
    }
195
196 View Code Duplication
    public function _make_like_buttons_twitter($url = '') {
197
198
        $settings = $this->settings;
199
200
        if ($settings['twitter_like'] == 1) {
201
            \CI_Controller::get_instance()->template->registerJsScript(
202
                '<script async="async" defer="defer">!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id))
203
                    {js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script></li>',
204
                'before'
205
            );
206
            return '<a href="https://twitter.com/share" class="twitter-share-button" data-url="' . $url . '">Tweet</a>';
207
        }
208
    }
209
210
    public function _make_like_buttons($url = '') {
211
212
        $html = '<ul class="items items-social">'
213
            . '<li>' . $this->_make_like_buttons_fb($url) . '</li>'
214
            . '<li>' . $this->_make_like_buttons_vk($url) . '</li>'
215
            . '<li>' . $this->_make_like_buttons_google($url) . '</li>'
216
            . '<li>' . $this->_make_like_buttons_twitter($url) . '</li>' .
217
            '</ul>';
218
        return $html;
219
    }
220
221
}