Completed
Pull Request — master (#7)
by
unknown
13:44
created

ShareThisDataObject   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 186
Duplicated Lines 9.68 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 18
loc 186
rs 10
c 0
b 0
f 0
wmc 27
lcom 0
cbo 1

15 Methods

Rating   Name   Duplication   Size   Complexity  
A providePermissions() 0 4 1
A canView() 0 4 1
A canCreate() 0 4 1
A canEdit() 0 4 1
A canDelete() 0 4 1
A IncludeThisIconNice() 0 4 1
A getIncludeThisIconNice() 0 4 2
A IncludeThisIconInExtendedListNice() 0 4 1
A getIncludeThisIconInExtendedListNice() 0 4 2
A Icon() 0 4 1
A getIcon() 0 9 2
A getCMSFields() 0 9 2
A onAfterWrite() 0 6 1
A validate() 0 16 2
B requireDefaultRecords() 18 44 8

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
namespace SunnySideUp\ShareThis;
4
5
use SilverStripe\Assets\Image;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Security\Permission;
8
use SilverStripe\ORM\FieldType\DBField;
9
use SilverStripe\Forms\LiteralField;
10
use SunnySideUp\ShareThis\ShareThisOptions;
11
use SunnySideUp\ShareThis\ShareThisSTE;
12
use SilverStripe\ORM\DB;
13
use SilverStripe\ORM\DataObject;
14
use SilverStripe\Security\PermissionProvider;
15
16
/**
17
 * @author nicolaas[at]sunnysideup.co.nz
18
 * @description: list of Share This Options that can be shown
19
 * @todo finish onAfterWrite and delete objects
20
 */
21
class ShareThisDataObject extends DataObject implements PermissionProvider
22
{
23
    private static $permission_framework = array(
24
        "SOCIAL_MEDIA" => array(
25
            'name' => "Social Media Management",
26
            'category' => "Social Media",
27
            'help' => 'Edit relationships, links and data of various social media platforms.',
28
            'sort' => 0
29
        )
30
    );
31
32
    private static $db = array(
33
        'Title' => 'Varchar(20)',
34
        'IncludeThisIcon' => 'Boolean',
35
        'IncludeThisIconInExtendedList' => 'Boolean',
36
        'Sort' => 'Int'
37
    );
38
39
    private static $has_one = array(
40
        'AlternativeIcon' => Image::class
41
    );
42
43
    private static $casting = array(
44
        'Icon' => 'HTMLText',
45
        'IncludeThisIconNice' => 'Varchar',
46
        'IncludeThisIconInExtendedListNice' => 'IncludeThisIconInExtendedList'
47
    );
48
49
    private static $field_labels = array(
50
        'Title' => 'Name',
51
        'IncludeThisIcon' => 'Include in main list',
52
        'IncludeThisIconNice' => 'Include in primary list',
53
        'IncludeThisIconInExtendedList' => 'Include in secondary list',
54
        'IncludeThisIconInExtendedListNice' => 'Include in secondary list',
55
        'Sort' => 'Sort Index (lower numbers shown first)',
56
        'AlternativeIcon' => 'Optional Alternative Icon (can be any size, a 32px by 32px square is recommended)'
57
    );
58
59
    private static $summary_fields = array(
60
        'Icon' => 'Icon',
61
        'Title' => 'Name',
62
        'IncludeThisIconNice' => 'IncludeThisIcon'
63
        //'IncludeThisIconInExtendedListNice' => 'IncludeThisIconInExtendedList'
64
    );
65
66
    private static $singular_name = 'Icon to share this page';
67
68
    private static $plural_name = 'Icons to share this page';
69
70
    private static $default_sort = 'IncludeThisIcon DESC, IncludeThisIconInExtendedList ASC, Sort ASC, Title ASC';
71
72
    public function providePermissions()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
73
    {
74
        return Config::inst()->get(ShareThisDataObject::class, "permission_framework");
75
    }
76
77
    public function canView($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
78
    {
79
        return Permission::checkMember($member, 'SOCIAL_MEDIA');
80
    }
81
82
    public function canCreate($member = null, $context = [])
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
83
    {
84
        return Permission::checkMember($member, 'SOCIAL_MEDIA');
85
    }
86
87
    public function canEdit($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
88
    {
89
        return Permission::checkMember($member, 'SOCIAL_MEDIA');
90
    }
91
92
    public function canDelete($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
93
    {
94
        return Permission::checkMember($member, 'SOCIAL_MEDIA');
95
    }
96
    public function IncludeThisIconNice()
97
    {
98
        return $this->getIncludeThisIconNice();
99
    }
100
    public function getIncludeThisIconNice()
101
    {
102
        return $this->IncludeThisIcon ? "YES" : "NO" ;
103
    }
104
105
    public function IncludeThisIconInExtendedListNice()
106
    {
107
        return $this->getIncludeThisIconInExtendedListNice();
108
    }
109
    public function getIncludeThisIconInExtendedListNice()
110
    {
111
        return $this->IncludeThisIconInExtendedList ? "YES" : "NO" ;
112
    }
113
114
    public function Icon()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
115
    {
116
        return $this->getIcon();
117
    }
118
    public function getIcon()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
119
    {
120
        $icon = $this->AlternativeIcon();
121
        if ($icon->exists()) {
122
            return $icon->SetHeight(16);
123
        }
124
        $html = '<img src="' . SS_SHARETHIS_DIR . '/images/icons/' . strtolower($this->Title) . ".png\" alt=\"{$this->Title}\"/>";
125
        return DBField::create_field("HTMLText", $html);
126
    }
127
128
    public function getCMSFields()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
129
    {
130
        $fields = parent::getCMSFields();
131
        if (class_exists("DataObjectSorterDOD")) {
132
            $fields->addFieldToTab("Root.Sort", new LiteralField("SortShortList", $this->dataObjectSorterPopupLink("IncludeThisIcon", 1, "<h3>Sort Main Icons</h3>")));
133
        }
134
        //$fields->replaceField('Title', new LiteralField('Title', "<p>{$this->Icon}<span>{$this->Title}</span></p>"));
135
        return $fields;
136
    }
137
138
    public function onAfterWrite()
139
    {
140
        parent::onAfterWrite();
141
        $objects = ShareThisDataObject::get()->filter('Title', $this->Title)->exclude('ID', $this->ID);
0 ignored issues
show
Unused Code introduced by
$objects is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
142
        //$objects->delete();
143
    }
144
145
    public function validate()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
146
    {
147
        $result = parent::validate();
148
        $bookmarks = ShareThisOptions::get_page_specific_data("", "", "");
149
        if (!isset($bookmarks[$this->Title])) {
150
            $result->error(sprintf(
151
                _t(
152
                    'ShareThisDataObject.NON_EXISTING_TITLE',
153
                    'This social plaform "%s" does not exist.  Please change / delete the this entry.'
154
                ),
155
                $this->Title
156
            ));
157
        }
158
159
        return $result;
160
    }
161
162
    public function requireDefaultRecords()
163
    {
164
        parent::requireDefaultRecords();
165
        $actualArray = ShareThisOptions::get_general_data();
166
        Config::inst()->update(ShareThisSTE::class, "included_icons", array());
167
        Config::inst()->update(ShareThisSTE::class, "excluded_icons", array());
168
        ShareThisOptions::set_general_data(null);
169
        $fullArray = ShareThisOptions::get_general_data();
170
        foreach ($fullArray as $key) {
171
            $object = ShareThisDataObject::get()->filter('Title', $key);
172
            if (! $object->exists()) {
173
                $object = new ShareThisDataObject();
174
                $object->Title = $key;
175
                $style = 'excluded';
176
                $object->IncludeThisIcon = false;
177
                if (in_array($key, $actualArray)) {
178
                    $object->IncludeThisIcon = true;
179
                    $style = 'included';
180
                }
181
                $object->write();
182
                DB::alteration_message("Added Bookmark Icon for $key ($style)", 'created');
183
            }
184
        }
185
        $inc = Config::inst()->get(ShareThisSTE::class, "included_icons");
186 View Code Duplication
        foreach ($inc as $key) {
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...
187
            $object = ShareThisDataObject::get()->filter(array('Title' => $key, 'IncludeThisIcon' => 0));
188
            if ($object->exists()) {
189
                $object = $object->first();
190
                $object->IncludeThisIcon = true;
191
                $object->write();
192
                DB::alteration_message("Updated inclusion for $key", 'created');
193
            }
194
        }
195
        $exc = Config::inst()->get(ShareThisSTE::class, "excluded_icons");
196 View Code Duplication
        foreach ($exc as $key) {
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...
197
            $object = ShareThisDataObject::get()->filter(array('Title' => $key, 'IncludeThisIcon' => 1));
198
            if ($object->exists()) {
199
                $object = $object->first();
200
                $object->IncludeThisIcon = false;
201
                $object->write();
202
                DB::alteration_message("Updated inclusion for $key", 'created');
203
            }
204
        }
205
    }
206
}
207