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