SiteConfigLeftAndMain::getEditForm()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 59
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 29
nc 8
nop 2
dl 0
loc 59
rs 9.1448
c 0
b 0
f 0

How to fix   Long Method   

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 LeKoala\Admini;
4
5
use SilverStripe\Forms\Form;
6
use LeKoala\Admini\LeftAndMain;
7
use LeKoala\Admini\MaterialIcons;
8
use SilverStripe\ORM\ArrayList;
9
use SilverStripe\ORM\DataObject;
10
use SilverStripe\View\ArrayData;
11
use SilverStripe\Control\Director;
12
use SilverStripe\Forms\FormAction;
13
use SilverStripe\ORM\ValidationResult;
14
use SilverStripe\SiteConfig\SiteConfig;
15
use SilverStripe\Versioned\RecursivePublishable;
16
17
class SiteConfigLeftAndMain extends LeftAndMain
18
{
19
    /**
20
     * @var string
21
     */
22
    private static $url_segment = 'settings';
23
24
    /**
25
     * @var string
26
     */
27
    private static $url_rule = '/$Action/$ID/$OtherID';
28
29
    /**
30
     * @var int
31
     */
32
    private static $menu_priority = -1;
0 ignored issues
show
introduced by
The private property $menu_priority is not used, and could be removed.
Loading history...
33
34
    /**
35
     * @var string
36
     */
37
    private static $menu_title = 'Settings';
38
39
    /**
40
     * @var string
41
     */
42
    private static $menu_icon = MaterialIcons::SETTINGS;
0 ignored issues
show
introduced by
The private property $menu_icon is not used, and could be removed.
Loading history...
43
44
    /**
45
     * @var string
46
     */
47
    private static $tree_class = SiteConfig::class;
48
49
    /**
50
     * @var array
51
     */
52
    private static $required_permission_codes = array('EDIT_SITECONFIG');
53
54
    /**
55
     * Initialises the {@link SiteConfig} controller.
56
     */
57
    public function init()
58
    {
59
        parent::init();
60
    }
61
62
    /**
63
     * @param null $id Not used.
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $id is correct as it would always require null to be passed?
Loading history...
64
     * @param null $fields Not used.
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
65
     *
66
     * @return Form
67
     */
68
    public function getEditForm($id = null, $fields = null)
69
    {
70
        $siteConfig = SiteConfig::current_site_config();
71
        $fields = $siteConfig->getCMSFields();
72
73
        // Retrieve validator, if one has been setup (e.g. via data extensions).
74
        if ($siteConfig->hasMethod("getCMSValidator")) {
75
            $validator = $siteConfig->getCMSValidator();
76
        } else {
77
            $validator = null;
78
        }
79
80
        $actions = $siteConfig->getCMSActions();
81
        $saveAction = $actions->fieldByName("action_save_siteconfig");
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $saveAction is correct as $actions->fieldByName('action_save_siteconfig') targeting SilverStripe\Forms\FieldList::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
82
        if ($saveAction) {
0 ignored issues
show
introduced by
$saveAction is of type null, thus it always evaluated to false.
Loading history...
83
            $saveAction->removeExtraClass("btn-primary");
84
            $saveAction->addExtraClass("btn-outline-success");
85
            $saveAction->setIcon(MaterialIcons::DONE);
86
        }
87
        // $negotiator = $this->getResponseNegotiator();
88
        /** @var Form $form */
89
        $form = Form::create(
90
            $this,
91
            'EditForm',
92
            $fields,
93
            $actions,
94
            $validator
95
        )->setHTMLID('Form_EditForm');
96
97
        /*
98
        $form->setValidationResponseCallback(function (ValidationResult $errors) use ($negotiator, $form) {
99
            $request = $this->getRequest();
100
            if ($request->isAjax() && $negotiator) {
101
                $result = $form->forTemplate();
102
                return $negotiator->respond($request, array(
103
                    'CurrentForm' => function () use ($result) {
104
                        return $result;
105
                    }
106
                ));
107
            }
108
        });*/
109
110
        $this->setCMSTabset($form);
111
112
        $form->setHTMLID('Form_EditForm');
113
        $form->loadDataFrom($siteConfig);
114
        $form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
115
116
        $actions = $actions->dataFields();
117
        if ($actions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $actions of type SilverStripe\Forms\FormField[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
118
            /** @var FormAction $action */
119
            foreach ($actions as $action) {
120
                $action->setUseButtonTag(true);
121
            }
122
        }
123
124
        $this->extend('updateEditForm', $form);
125
126
        return $form;
127
    }
128
129
    /**
130
     * Save the current sites {@link SiteConfig} into the database.
131
     * The method is defined in SiteConfig::getCMSActions
132
     *
133
     * @param array $data
134
     * @param Form $form
135
     * @return String
136
     */
137
    public function save_siteconfig($data, $form)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

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

137
    public function save_siteconfig(/** @scrutinizer ignore-unused */ $data, $form)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
138
    {
139
        $data = $form->getData();
140
        $siteConfig = DataObject::get_by_id(SiteConfig::class, $data['ID']);
141
        $form->saveInto($siteConfig);
0 ignored issues
show
Bug introduced by
It seems like $siteConfig can also be of type null; however, parameter $dataObject of SilverStripe\Forms\Form::saveInto() does only seem to accept SilverStripe\ORM\DataObjectInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

141
        $form->saveInto(/** @scrutinizer ignore-type */ $siteConfig);
Loading history...
142
        $siteConfig->write();
143
144
        if ($siteConfig->hasExtension(RecursivePublishable::class)) {
145
            $siteConfig->publishRecursive();
146
        }
147
148
        $message = _t('SilverStripe\\Admin\\LeftAndMain.SAVEDUP', 'Saved.');
149
150
        // TODO: implement our own logic
151
        // if (Director::is_ajax()) {
152
        //     $this->response->addHeader(
153
        //         'X-Status',
154
        //         rawurlencode($message)
155
        //     );
156
        //     return $form->forTemplate();
157
        // }
158
        $this->successMessage($message);
159
        return $this->redirect($this->LinkHash());
160
    }
161
162
    public function Breadcrumbs($unlinked = false)
163
    {
164
        return new ArrayList(array(
165
            new ArrayData(array(
166
                'Title' => static::menu_title(),
167
                'Link' => $this->Link()
168
            ))
169
        ));
170
    }
171
}
172