Passed
Push — master ( 213ed7...ff9bff )
by Nicolaas
01:32
created

InternalExternalLinkExtension::getMyLink()   C

Complexity

Conditions 13
Paths 11

Size

Total Lines 40
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 13
eloc 28
c 6
b 0
f 0
nc 11
nop 1
dl 0
loc 40
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Sunnysideup\InternalExternalLink\Extensions;
4
5
use Page;
0 ignored issues
show
Bug introduced by
The type Page 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...
6
7
use Sunnysideup\EmailAddressDatabaseField\Model\Fieldtypes\EmailAddress;
0 ignored issues
show
Bug introduced by
The type Sunnysideup\EmailAddress...Fieldtypes\EmailAddress 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...
8
9
use Sunnysideup\PhoneField\Model\Fieldtypes\PhoneField;
0 ignored issues
show
Bug introduced by
The type Sunnysideup\PhoneField\Model\Fieldtypes\PhoneField 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...
10
11
use Sunnysideup\InternalExternalLink\Extensions\InternalExternalLinkExtension;
12
use SilverStripe\AssetAdmin\Forms\UploadField;
0 ignored issues
show
Bug introduced by
The type SilverStripe\AssetAdmin\Forms\UploadField 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...
13
use SilverStripe\Assets\File;
14
use SilverStripe\Forms\FieldList;
15
use SilverStripe\Forms\HeaderField;
16
use SilverStripe\Forms\OptionsetField;
17
use SilverStripe\Forms\FormScaffolder;
18
use SilverStripe\Forms\Tab;
19
use SilverStripe\Forms\TextField;
20
use SilverStripe\Forms\TreeDropdownField;
21
use SilverStripe\ORM\DataExtension;
22
use SilverStripe\ORM\FieldType\DBField;
23
24
use SilverStripe\ORM\DataObject;
25
use SilverStripe\View\Requirements;
26
27
use SilverStripe\Core\Config\Config;
28
29
class InternalExternalLinkExtension extends DataExtension
30
{
31
32
    public static $casting = [
33
        'MyLink' => 'Varchar',
34
    ];
35
    private static $db = [
36
        'LinkType' => "Enum('Internal,External,DownloadFile,Email,Phone', 'Internal')",
37
        'ExternalLink' => 'Varchar(255)',
38
    ];
39
40
    private static $has_one = [
41
        'InternalLink' => Page::class,
42
        'DownloadFile' => File::class,
43
    ];
44
45
    private static $owns = [
46
        'DownloadFile',
47
    ];
48
49
    /**
50
     * use the $fieldNameAppendix if you have multiple fields.
51
     *
52
     * @param string $fieldNameAppendix - optional
53
     */
54
    public function MyLink($fieldNameAppendix = ''): ?string
55
    {
56
        return $this->owner->getMyLink($fieldNameAppendix);
57
    }
58
59
    /**
60
     * use the $fieldNameAppendix if you have multiple fields.
61
     *
62
     * @param string $fieldNameAppendix - optional
63
     */
64
    public function getMyLink(?string $fieldNameAppendix = ''): ?string
65
    {
66
        $linkTypeFieldName = 'LinkType' . $fieldNameAppendix;
67
68
        $InternalLinkMethodName = 'InternalLink' . $fieldNameAppendix;
69
        $internalLinkFieldName = $InternalLinkMethodName . 'ID';
70
71
        $downloadLinkMethodName = 'DownloadFile' . $fieldNameAppendix;
72
        $downloadLinkFieldName = $downloadLinkMethodName . 'ID';
73
74
        $externalLinkFieldName = 'ExternalLink' . $fieldNameAppendix;
75
        if ('Internal' === $this->owner->{$linkTypeFieldName} && $this->owner->{$internalLinkFieldName}) {
76
            $obj = $this->owner->{$InternalLinkMethodName}();
77
            if ($obj) {
78
                return $obj->Link();
79
            }
80
        } elseif ('DownloadFile' === $this->owner->{$linkTypeFieldName} && $this->owner->{$downloadLinkFieldName}) {
81
            $obj = $this->owner->{$downloadLinkMethodName}();
82
            if ($obj) {
83
                return $obj->Link();
84
            }
85
        } elseif ($this->owner->{$externalLinkFieldName}) {
86
            if('External' === $this->owner->{$linkTypeFieldName}) {
87
                return DBField::create_field('Varchar', $this->owner->{$externalLinkFieldName})->url();
88
            } elseif ('Email' === $this->owner->{$linkTypeFieldName} ) {
89
                $val = $this->owner->{$externalLinkFieldName};
90
                if(class_exists('Sunnysideup\\EmailAddressDatabaseField\\Model\\Fieldtypes\\EmailAddress')) {
91
                    $val = DBField::create_field('EmailAddress', $val)->HiddenEmailAddress()->RAW();
92
                }
93
                return 'mailto:'.$val;
94
            } elseif ( 'Phone' === $this->owner->{$linkTypeFieldName}) {
95
                $val = $this->owner->{$externalLinkFieldName};
96
                if(class_exists('Sunnysideup\\PhoneField\\Model\\Fieldtypes\\PhoneField')) {
97
                    $val = DBField::create_field('PhoneField', $this->owner->{$externalLinkFieldName})->IntlFormat()->Raw();
98
                }
99
                return 'callto:'.$val;
100
            }
101
        }
102
103
        return null;
104
    }
105
106
    public function updateCMSFields(FieldList $fields)
107
    {
108
        $fieldNameAppendici = $this->getFieldNameAppendici();
109
        foreach ($fieldNameAppendici as $appendix) {
110
            $linkTypeClass = 'LinkType' . $appendix . '_'.rand(0,999999);
111
            $internalClass = 'InternalLink' . $appendix . 'ID_'.rand(0,999999);
112
            $externalClass = 'ExternalLink' . $appendix . '_'.rand(0,999999);
113
            $downloadFileClass = 'DownloadFile' . $appendix . '_'.rand(0,999999);
114
115
            $js = <<<js
116
                var el = this;
117
                const val = jQuery('.{$linkTypeClass}').find('.form-check-input:checked').val();
118
                const internaLinkHolder = jQuery('.{$internalClass}');
119
                const externaLinkHolder = jQuery('.{$externalClass}');
120
                const downloadLinkHolder = jQuery('.{$downloadFileClass}');
121
                if (val === 'Internal') {
122
                    internaLinkHolder.show();
123
                    externaLinkHolder.hide();
124
                    downloadLinkHolder.hide();
125
                } else if(val === 'External' || val === 'Phone' || val === 'Email') {
126
                    internaLinkHolder.hide();
127
                    externaLinkHolder.show();
128
                    downloadLinkHolder.hide();
129
                } else if(val === 'DownloadFile') {
130
                    internaLinkHolder.hide();
131
                    externaLinkHolder.hide();
132
                    downloadLinkHolder.show();
133
                } else {
134
                    internaLinkHolder.show();
135
                    externaLinkHolder.show();
136
                    downloadLinkHolder.show();
137
                }
138
139
js;
140
            Requirements::customScript('
141
                const '.$linkTypeClass.'fx = function() {
142
                    '.$js.'
143
                }
144
                jQuery(".'.$linkTypeClass.' input").on("change click", '.$linkTypeClass.'fx());
145
                window.setInterval(
146
                    function() {
147
                        '.$linkTypeClass.'fx();
148
                    },
149
                    700
150
                )',
151
                $linkTypeClass
152
            );
153
            $fields->removeByName([
154
                'LinkType' . $appendix,
155
                'InternalLink' . $appendix . 'ID',
156
                'DownloadFile' . $appendix,
157
                'ExternalLink' . $appendix,
158
            ]);
159
            // $fields->insertBefore(new Tab('Links', 'Links'), 'Settings');
160
            $fields->addFieldsToTab(
161
                'Root.Links',
162
                [
163
                    HeaderField::create(
164
                        'Link-Details-Heading' . $appendix,
165
                        'Link'
166
                    ),
167
168
                    OptionsetField::create(
169
                        'LinkType' . $appendix,
170
                        'Link Type ' . $appendix,
171
                        $this->owner->dbObject('LinkType')->enumValues()
172
                    )
173
                        ->setAttribute('onchange', $js)
174
                        ->setAttribute('onclick', $js)
175
                        ->addExtraClass($linkTypeClass),
176
177
                    TreeDropdownField::create(
178
                        'InternalLink' . $appendix . 'ID',
179
                        'Internal Link ' . $appendix,
180
                        Page::class
181
                    )
182
                        ->addExtraClass($internalClass),
183
184
                    TextField::create(
185
                        'ExternalLink' . $appendix,
186
                        'External Link / Email / Phone'
187
                    )
188
                        ->addExtraClass($externalClass),
189
190
                    UploadField::create(
191
                        'DownloadFile' . $appendix,
192
                        'Download File ' . $appendix
193
                    )
194
                        ->addExtraClass($downloadFileClass),
195
                ]
196
            );
197
            Requirements::customScript(
198
                'window.setTimeout(
199
                    function() {
200
                        jQuery(\'input[name="LinkType' . $appendix . '"]\').change();
201
                    }
202
                    , 500
203
                )',
204
                'InternalExternalLinkKickStart' . $appendix
205
            );
206
        }
207
    }
208
209
210
    protected function getFieldNameAppendici(): array
211
    {
212
        if ($this->owner->hasMethod('getFieldNameAppendiciMore')) {
213
            return $this->owner->getFieldNameAppendiciMore();
214
        }
215
216
        // we need an empty string here ...
217
        return [
218
            '',
219
        ];
220
    }
221
}
222