ShareThisDataObject   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 275
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 96
dl 0
loc 275
rs 10
c 0
b 0
f 0
wmc 27

15 Methods

Rating   Name   Duplication   Size   Complexity  
A IncludeThisIconNice() 0 3 1
A providePermissions() 0 3 1
A getIncludeThisIconInExtendedListNice() 0 3 2
A onAfterWrite() 0 4 1
A canEdit() 0 3 1
A canCreate() 0 3 1
A getIcon() 0 10 2
A canView() 0 3 1
A getIncludeThisIconNice() 0 3 2
B requireDefaultRecords() 0 51 8
A IncludeThisIconInExtendedListNice() 0 3 1
A validate() 0 15 2
A canDelete() 0 3 1
A getCMSFields() 0 8 2
A Icon() 0 3 1
1
<?php
2
3
namespace SunnysideUp\ShareThis;
4
5
use SilverStripe\Assets\Image;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Forms\LiteralField;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\ORM\DB;
10
use SilverStripe\ORM\FieldType\DBField;
11
use SilverStripe\ORM\ValidationResult;
12
use SilverStripe\Security\Permission;
13
use SilverStripe\Security\PermissionProvider;
14
use SunnysideUp\ShareThis\ShareThisOptions;
15
use SunnysideUp\ShareThis\ShareThisSTE;
16
17
/**
18
 * @author nicolaas[at]sunnysideup.co.nz
19
 * @description: list of Share This Options that can be shown
20
 * @todo finish onAfterWrite and delete objects
21
 */
22
// class ShareThisDataObject extends DataObject implements PermissionProvider
23
class ShareThisDataObject extends DataObject
24
{
25
    /**
26
     * @var string
27
     */
28
    private static $table_name = 'ShareThisDataObject';
0 ignored issues
show
introduced by MasseyIsaako
The private property $table_name is not used, and could be removed.
Loading history...
29
30
    /**
31
     * @var array
32
     */
33
    private static $permission_framework = [
0 ignored issues
show
introduced by MasseyIsaako
The private property $permission_framework is not used, and could be removed.
Loading history...
34
        "SOCIAL_MEDIA" => [
35
            'name' => "Social Media Management",
36
            'category' => "Social Media",
37
            'help' => 'Edit relationships, links and data of various social media platforms.',
38
            'sort' => 0
39
        ]
40
    ];
41
42
    /**
43
     * @var array
44
     */
45
    private static $db = [
0 ignored issues
show
introduced by MasseyIsaako
The private property $db is not used, and could be removed.
Loading history...
46
        'Title' => 'Varchar(20)',
47
        'IncludeThisIcon' => 'Boolean',
48
        'IncludeThisIconInExtendedList' => 'Boolean',
49
        'Sort' => 'Int'
50
    ];
51
52
    /**
53
     * @var array
54
     */
55
    private static $has_one = [
0 ignored issues
show
introduced by MasseyIsaako
The private property $has_one is not used, and could be removed.
Loading history...
56
        'AlternativeIcon' => Image::class
57
    ];
58
59
    /**
60
     * @var array
61
     */
62
    private static $casting = [
0 ignored issues
show
introduced by MasseyIsaako
The private property $casting is not used, and could be removed.
Loading history...
63
        'Icon' => 'HTMLText',
64
        'IncludeThisIconNice' => 'Varchar',
65
        'IncludeThisIconInExtendedListNice' => 'IncludeThisIconInExtendedList'
66
    ];
67
68
    /**
69
     * @var array
70
     */
71
    private static $field_labels = [
0 ignored issues
show
introduced by MasseyIsaako
The private property $field_labels is not used, and could be removed.
Loading history...
72
        'Title' => 'Name',
73
        'IncludeThisIcon' => 'Include in main list',
74
        'IncludeThisIconNice' => 'Include in primary list',
75
        'IncludeThisIconInExtendedList' => 'Include in secondary list',
76
        'IncludeThisIconInExtendedListNice' => 'Include in secondary list',
77
        'Sort' => 'Sort Index (lower numbers shown first)',
78
        'AlternativeIcon' => 'Optional Alternative Icon (can be any size, a 32px by 32px square is recommended)'
79
    ];
80
81
    /**
82
     * @var array
83
     */
84
    private static $summary_fields = [
0 ignored issues
show
introduced by MasseyIsaako
The private property $summary_fields is not used, and could be removed.
Loading history...
85
        'Title' => 'Name',
86
    ];
87
88
    /**
89
     * @var string
90
     */
91
    private static $singular_name = 'Icon to share this page';
0 ignored issues
show
introduced by sunnysideup
The private property $singular_name is not used, and could be removed.
Loading history...
92
93
    /**
94
     * @var string
95
     */
96
    private static $plural_name = 'Icons to share this page';
0 ignored issues
show
introduced by sunnysideup
The private property $plural_name is not used, and could be removed.
Loading history...
97
98
    /**
99
     * @var string
100
     */
101
    private static $default_sort = 'IncludeThisIcon DESC, IncludeThisIconInExtendedList ASC, Sort ASC, Title ASC';
0 ignored issues
show
introduced by sunnysideup
The private property $default_sort is not used, and could be removed.
Loading history...
102
103
    /**
104
     * @return string
105
     */
106
    public function providePermissions()
107
    {
108
        return Config::inst()->get(ShareThisDataObject::class, "permission_framework");
109
    }
110
111
    /**
112
     * @return boolean
113
     */
114
    public function canView($member = null)
115
    {
116
        return Permission::checkMember($member, 'SOCIAL_MEDIA');
117
    }
118
119
    /**
120
     * @return boolean
121
     */
122
    public function canCreate($member = null, $context = [])
123
    {
124
        return Permission::checkMember($member, 'SOCIAL_MEDIA');
125
    }
126
127
    /**
128
     * @return boolean
129
     */
130
    public function canEdit($member = null)
131
    {
132
        return Permission::checkMember($member, 'SOCIAL_MEDIA');
133
    }
134
135
    /**
136
     * @return boolean
137
     */
138
    public function canDelete($member = null)
139
    {
140
        return Permission::checkMember($member, 'SOCIAL_MEDIA');
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    public function IncludeThisIconNice()
147
    {
148
        return $this->getIncludeThisIconNice();
149
    }
150
151
    /**
152
     * @return string
153
     */
154
    public function getIncludeThisIconNice()
155
    {
156
        return $this->IncludeThisIcon ? "Yes" : "No" ;
157
    }
158
159
    /**
160
     * @return string
161
     */
162
    public function IncludeThisIconInExtendedListNice()
163
    {
164
        return $this->getIncludeThisIconInExtendedListNice();
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    public function getIncludeThisIconInExtendedListNice()
171
    {
172
        return $this->IncludeThisIconInExtendedList ? "Yes" : "No" ;
0 ignored issues
show
Bug Best Practice introduced by MasseyIsaako
The property IncludeThisIconInExtendedList does not exist on SunnysideUp\ShareThis\ShareThisDataObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
173
    }
174
175
    /**
176
     * Icon
177
     */
178
    public function Icon()
179
    {
180
        return $this->getIcon();
181
    }
182
183
    /**
184
     * Get the icon
185
     *
186
     * @return  DBField [<description>]
187
     */
188
    public function getIcon()
189
    {
190
        $icon = $this->AlternativeIcon();
0 ignored issues
show
Bug introduced by sunnysideup
The method AlternativeIcon() does not exist on SunnysideUp\ShareThis\ShareThisDataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

190
        /** @scrutinizer ignore-call */ 
191
        $icon = $this->AlternativeIcon();
Loading history...
191
        if ($icon->exists()) {
192
            return $icon->ScaleHeight(16);
193
        }
194
195
        $html = '<img src="' . SS_SHARETHIS_DIR . '/images/icons/' . strtolower($this->Title) . ".png\" alt=\"{$this->Title}\"/>";
196
197
        return DBField::create_field("HTMLText", $html);
198
    }
199
200
    /**
201
     * @return FieldList $fields
0 ignored issues
show
Bug introduced by MasseyIsaako
The type SunnysideUp\ShareThis\FieldList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
202
     */
203
    public function getCMSFields()
204
    {
205
        $fields = parent::getCMSFields();
206
        if (class_exists("DataObjectSorterDOD")) {
207
            $fields->addFieldToTab("Root.Sort", LiteralField::create("SortShortList", $this->dataObjectSorterPopupLink("IncludeThisIcon", 1, "<h3>Sort Main Icons</h3>")));
0 ignored issues
show
Bug introduced by MasseyIsaako
The method dataObjectSorterPopupLink() does not exist on SunnysideUp\ShareThis\ShareThisDataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

207
            $fields->addFieldToTab("Root.Sort", LiteralField::create("SortShortList", $this->/** @scrutinizer ignore-call */ dataObjectSorterPopupLink("IncludeThisIcon", 1, "<h3>Sort Main Icons</h3>")));
Loading history...
208
        }
209
210
        return $fields;
211
    }
212
213
    /**
214
     * @return void
215
     */
216
    public function onAfterWrite()
217
    {
218
        parent::onAfterWrite();
219
        $objects = ShareThisDataObject::get()->filter('Title', $this->Title)->exclude('ID', $this->ID);
0 ignored issues
show
Unused Code introduced by sunnysideup
The assignment to $objects is dead and can be removed.
Loading history...
220
    }
221
222
    /**
223
     * @return ValidationResult $result
224
     */
225
    public function validate()
226
    {
227
        $result = parent::validate();
228
        $bookmarks = ShareThisOptions::get_page_specific_data("", "", "");
229
        if (!isset($bookmarks[$this->Title])) {
230
            $result->addError(sprintf(
231
                _t(
232
                    'ShareThisDataObject.NON_EXISTING_TITLE',
233
                    'This social plaform "%s" does not exist.  Please change / delete the this entry.'
234
                ),
235
                $this->Title
236
            ));
237
        }
238
239
        return $result;
240
    }
241
242
    /**
243
     * Setting default records
244
     *
245
     * @return void
246
     */
247
    public function requireDefaultRecords()
248
    {
249
        parent::requireDefaultRecords();
250
        $actualArray = ShareThisOptions::get_general_data();
251
        Config::inst()->update(ShareThisSTE::class, "included_icons", []);
0 ignored issues
show
Bug introduced by MasseyIsaako
The method update() does not exist on SilverStripe\Config\Coll...nfigCollectionInterface. It seems like you code against a sub-type of SilverStripe\Config\Coll...nfigCollectionInterface such as SilverStripe\Config\Coll...\MemoryConfigCollection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

251
        Config::inst()->/** @scrutinizer ignore-call */ update(ShareThisSTE::class, "included_icons", []);
Loading history...
252
        Config::inst()->update(ShareThisSTE::class, "excluded_icons", []);
253
        ShareThisOptions::set_general_data(null);
0 ignored issues
show
Unused Code introduced by sunnysideup
The call to SunnysideUp\ShareThis\Sh...ons::set_general_data() has too many arguments starting with null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

253
        ShareThisOptions::/** @scrutinizer ignore-call */ 
254
                          set_general_data(null);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
254
        $fullArray = ShareThisOptions::get_general_data();
255
256
        foreach ($fullArray as $key) {
257
            $object = ShareThisDataObject::get()->filter('Title', $key);
258
259
            if (!$object->exists()) {
260
                $object = new ShareThisDataObject();
261
                $object->Title = $key;
262
                $style = 'excluded';
263
                $object->IncludeThisIcon = false;
0 ignored issues
show
Bug Best Practice introduced by sunnysideup
The property IncludeThisIcon does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
264
265
                if (in_array($key, $actualArray)) {
0 ignored issues
show
Bug introduced by sunnysideup
$actualArray of type SunnysideUp\ShareThis\general_data is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

265
                if (in_array($key, /** @scrutinizer ignore-type */ $actualArray)) {
Loading history...
266
                    $object->IncludeThisIcon = true;
267
                    $style = 'included';
268
                }
269
270
                $object->write();
271
                DB::alteration_message("Added Bookmark Icon for $key ($style)", 'created');
272
            }
273
        }
274
275
        $inc = Config::inst()->get(ShareThisSTE::class, "included_icons");
276
277
        foreach ($inc as $key) {
278
            $object = ShareThisDataObject::get()->filter(['Title' => $key, 'IncludeThisIcon' => 0]);
279
280
            if ($object->exists()) {
281
                $object = $object->first();
282
                $object->IncludeThisIcon = true;
283
                $object->write();
284
                DB::alteration_message("Updated inclusion for $key", 'created');
285
            }
286
        }
287
288
        $exc = Config::inst()->get(ShareThisSTE::class, "excluded_icons");
289
290
        foreach ($exc as $key) {
291
            $object = ShareThisDataObject::get()->filter(['Title' => $key, 'IncludeThisIcon' => 1]);
292
293
            if ($object->exists()) {
294
                $object = $object->first();
295
                $object->IncludeThisIcon = false;
296
                $object->write();
297
                DB::alteration_message("Updated inclusion for $key", 'created');
298
            }
299
        }
300
    }
301
}
302