Passed
Pull Request — master (#9)
by
unknown
02:43
created

ShareThisSTE   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 264
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 106
dl 0
loc 264
rs 9.0399
c 0
b 0
f 0
wmc 42

9 Methods

Rating   Name   Duplication   Size   Complexity  
A IncludeShareAll() 0 4 1
A getShareIcons() 0 4 1
B makeShareIcons() 0 53 10
A ShareAllExpandedList() 0 6 1
A makeBookmarks() 0 24 6
C applyToOwnerClass() 0 32 13
A getShareAll() 0 4 2
A getShowShareIcons() 0 8 3
A updateCMSFields() 0 30 5

How to fix   Complexity   

Complex Class

Complex classes like ShareThisSTE often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ShareThisSTE, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace SunnysideUp\ShareThis;
4
5
use SilverStripe\Dev\Debug;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\HeaderField;
8
use SilverStripe\Forms\CheckboxField;
9
use SilverStripe\Forms\LiteralField;
10
use SunnysideUp\ShareThis\ShareThisOptions;
11
use SilverStripe\View\Requirements;
12
use SilverStripe\Core\Config\Config;
13
use SilverStripe\Core\Convert;
14
use SilverStripe\View\ArrayData;
15
use SilverStripe\ORM\ArrayList;
16
use SunnysideUp\ShareThis\ShareThisDataObject;
17
use SilverStripe\CMS\Model\SiteTreeExtension;
18
19
/**
20
 * Add a field to each SiteTree object and it's subclasses to enable Share icons.
21
 * @author nicolaas [at] sunnysideup.co.nz
22
 * @inspiration: Silverstripe Original Module - full credits to them.  We made our own to improve their module
23
 * @todo fix populateDefaults to make sure SiteConfig table is built first
24
 */
25
class ShareThisSTE extends SiteTreeExtension
26
{
27
28
    /**
29
     * Use the font-awesome icon collection?
30
     * @var Boolean
31
     */
32
    private static $use_font_awesome = true;
0 ignored issues
show
introduced by
The private property $use_font_awesome is not used, and could be removed.
Loading history...
33
34
    /**
35
     * list of sitetree extending classnames where
36
     * the ShareThis functionality should be included
37
     * @var Array
38
     */
39
    private static $always_include_in = [];
0 ignored issues
show
introduced by
The private property $always_include_in is not used, and could be removed.
Loading history...
40
41
    /**
42
     * list of sitetree extending classnames where
43
     * the ShareThis functionality should NEVER be included
44
     * @var Array
45
     */
46
    private static $never_include_in = [];
0 ignored issues
show
introduced by
The private property $never_include_in is not used, and could be removed.
Loading history...
47
48
    /**
49
    * use BW icons
50
    * @var boolean
51
    */
52
    private static $use_bw_effect = false;
0 ignored issues
show
introduced by
The private property $use_bw_effect is not used, and could be removed.
Loading history...
53
54
    /**
55
    * specify icons to be included, if left empty, this variable will be ignored
56
    * We have this variable so that you can setup a bunch of default icons
57
    * @var array
58
    */
59
    private static $included_icons = [];
0 ignored issues
show
introduced by
The private property $included_icons is not used, and could be removed.
Loading history...
60
61
    /**
62
    * specify icons to be excluded, if left empty, this variable will be ignored
63
    * We have this variable so that you can setup a bunch of default icons
64
    * @var array
65
    */
66
    private static $excluded_icons = [];
0 ignored issues
show
introduced by
The private property $excluded_icons is not used, and could be removed.
Loading history...
67
68
    /**
69
     * standard SS method
70
     * @var Array
71
     **/
72
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
73
        'ShareIcons' => 'Boolean'
74
    );
75
76
    /**
77
     * @param  FieldList $fields
78
     *
79
     * @return FieldList $fields
80
     */
81
    public function updateCMSFields(FieldList $fields)
82
    {
83
        if ($this->applyToOwnerClass()) {
84
            $config = $this->owner->getSiteConfig();
85
86
            if (! $config->AlwaysIncludeShareThisLinks) {
87
                $fields->addFieldToTab('Root.SocialMedia', HeaderField::create('ShareThisHeader', 'Allow users to share this page'));
88
89
                $fields->addFieldToTab('Root.SocialMedia', CheckboxField::create('ShareIcons', 'Show Share Icons on this page', $config->IncludeByDefault));
90
91
                $fields->addFieldToTab('Root.SocialMedia', LiteralField::create('LinkToSiteConfigSocialMedia', "<p>Note: make sure to review the social media settings in the <a href=\"{$config->CMSEditLink()}\">Site Config</a>.</p>"));
92
            }
93
94
            $list = ShareThisOptions::get_all_options($this->owner->Title, $this->owner->Link(), $this->owner->MetaDescription);
95
96
            $fields->addFieldToTab('Root.SocialMedia', HeaderField::create('ShareThisNow', 'Share this page on your favourite social media sites...'));
97
98
            $html = "<div><p>Click on any of the icons below to share the '<i>{$this->owner->Title}</i>' page. Any click will open a new tab/window where you will need to enter your login details.</p>";
99
100
            foreach ($list as $key => $innerArray) {
101
                if (! isset($innerArray['click'])) {
102
                    $html .= "<span><a href=\"{$innerArray['url']}\" target=\"_blank\" style=\"whitespace: nowrap; display: inline-block;\"><img src=\"" . SS_SHARETHIS_DIR . "/images/icons/$key.png\" alt=\"$key\"/>{$innerArray['title']}</a></span>&nbsp;&nbsp;";
103
                }
104
            }
105
106
            $html .= '</div>';
107
            $fields->addFieldToTab('Root.SocialMedia', LiteralField::create('ShareNow', $html));
108
        }
109
110
        return $fields;
111
    }
112
113
    /**
114
     * Show the sharing icons
115
     */
116
    public function getShowShareIcons()
117
    {
118
        if ($this->applyToOwnerClass()) {
119
            $config = $this->owner->getSiteConfig();
120
            if ($config->AlwaysIncludeShareThisLinks) {
121
                return true;
122
            }
123
            return $this->owner->ShareIcons;
124
        }
125
    }
126
127
    /**
128
     * Get the sharing icons
129
     */
130
    public function getShareIcons()
131
    {
132
        $bookmarks = $this->makeBookmarks('IncludeThisIcon');
133
        return $this->makeShareIcons($bookmarks);
134
    }
135
136
    /**
137
     * Grabbing front end dependencies for the expanded sharing list with some extra
138
     * functionality
139
     */
140
    public function ShareAllExpandedList()
141
    {
142
        Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.min.js');
143
        Requirements::javascript('sunnysideup/sharethis: javascript/ShareAllExpandedList.js');
144
        $bookmarks = $this->makeBookmarks('IncludeThisIconInExtendedList');
145
        return $this->makeShareIcons($bookmarks);
146
    }
147
148
    /**
149
     * Include share all
150
     */
151
    public function IncludeShareAll()
152
    {
153
        $config = $this->owner->getSiteConfig();
154
        return $config->ShareThisAllInOne;
155
    }
156
157
    /**
158
     * @return boolean
159
     */
160
    public function getShareAll()
161
    {
162
        if ($this->IncludeShareAll()) {
163
            return ShareThisOptions::get_share_all();
164
        }
165
    }
166
167
    /**
168
     * @return array
169
     */
170
    protected function makeShareIcons($bookmarks)
171
    {
172
        $icons = [];
173
        if ($bookmarks) {
174
            $useFontAwesome = Config::inst()->get(ShareThisSTE::class, "use_font_awesome");
175
            Requirements::themedCSS('SocialNetworking', "sharethis"); // ALSO  added in template
176
177
            if ($useFontAwesome) {
178
                Requirements::css("//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css");
179
            }
180
181
            Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.min.js');
182
            Requirements::javascript('sunnysideup/sharethis: javascript/shareThis.js');
183
184
            if (Config::inst()->get(ShareThisSTE::class, "use_bw_effect")) {
185
                Requirements::customScript('sharethis.set_use_BW(true);', 'ShareThisBWEffect');
186
            }
187
188
            foreach ($bookmarks as $key => $bookmark) {
189
                if (isset($bookmark['title']) && isset($bookmark['url'])) {
190
                    $icon = array(
191
                        'Title' => Convert::raw2att($bookmark['title']),
192
                        'URL' => $bookmark['url'],
193
                        'Key' => $key,
194
                        'ImageSource' => "sharethis/images/icons/$key.png",
195
                        'FAIcon' => $bookmark["faicon"],
196
                        'UseStandardImage' => true
197
                    );
198
199
                    if (isset($bookmark['click'])) {
200
                        $icon['OnClick'] = $bookmark['click'];
201
                    }
202
203
                    if ($useFontAwesome) {
204
                        $icon['ImageSource'] = null;
205
                        $icon['UseStandardImage'] = false;
206
                        $icon['FAIcon'] = $bookmark["faicon"];
207
                    }
208
209
                    if (isset($bookmark['icon'])) {
210
                        $icon['ImageSource'] = $bookmark['icon'];
211
                        $icon['UseStandardImage'] = false;
212
                        $icon['FAIcon'] = null;
213
                    }
214
215
                    $icons[] = new ArrayData($icon);
216
                } else {
217
                    Debug::show("Title of url not defined for $key");
218
                }
219
            }
220
        }
221
222
        return new ArrayList($icons);
223
    }
224
225
    /**
226
     * Creating the bookmarks
227
     */
228
    protected function makeBookmarks($field)
229
    {
230
        $finalBookmarks = [];
231
232
        $bookmarks = ShareThisOptions::get_page_specific_data($this->owner->Title, $this->owner->Link(), $this->owner->MetaDescription);
233
234
        $objects = ShareThisDataObject::get()
235
            ->filter($field, 1)
236
            ->sort(array('Sort' => 'ASC', 'Title' => 'ASC'));
237
        if ($objects->count()) {
238
            foreach ($objects as $obj) {
239
                if (isset($bookmarks[$obj->Title])) {
240
                    $finalBookmarks[$obj->Title] = $bookmarks[$obj->Title];
241
242
                    if ($obj->AlternativeIconID && $obj->AlternativeIcon()->exists()) {
243
                        $finalBookmarks[$obj->Title]['icon'] = $obj->AlternativeIcon()->Link();
244
                    }
245
                }
246
            }
247
        } else {
248
            $finalBookmarks = $bookmarks;
249
        }
250
251
        return $finalBookmarks;
252
    }
253
254
    /**
255
     * @return boolean
256
     */
257
    private function applyToOwnerClass()
258
    {
259
        $always = Config::inst()->get(ShareThisSTE::class, "always_include_in");
260
        $never = Config::inst()->get(ShareThisSTE::class, "never_include_in");
261
        if (count($always) == 0 && count($never) == 0) {
262
            return true;
263
        } elseif (count($never) && count($always) == 0) {
264
            if (in_array($this->owner->ClassName, $never)) {
265
                return false;
266
            }
267
268
            return true;
269
        } elseif (count($always) && count($never) == 0) {
270
            if (in_array($this->owner->ClassName, $always)) {
271
                return true;
272
            }
273
274
            return false;
275
        } elseif (count($never) && count($always)) {
276
            if (in_array($this->owner->ClassName, $never)) {
277
                return false;
278
            }
279
280
            if (in_array($this->owner->ClassName, $always)) {
281
                return true;
282
            }
283
284
            //exception... if dev sets both always and never
285
            //then the ones not set will be included by default.
286
            return true;
287
        } else {
288
            user_error("Strange condition!", E_USER_NOTICE);
289
        }
290
    }
291
}
292