DataObjectOneRecordUpdateController   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 108
Duplicated Lines 22.22 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 11
dl 24
loc 108
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A popup_link_only() 0 5 1
A popup_link() 8 8 2
A init() 16 16 2
A onerecordform() 0 33 5
A save() 0 15 2
A show() 0 11 2

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
 *@author nicolaas [at] sunnysideup.co.nz
4
 *@package: dataobjectsorter
5
 *@description: allows you to edit one record
6
 *
7
 **/
8
9
class DataObjectOneRecordUpdateController extends DataObjectSortBaseClass
10
{
11
    private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
12
        "onerecordform" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION',
13
        "show" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION',
14
        "save" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION'
15
    );
16
17
    /**
18
     *
19
     * make sure to also change in routes if you change this link
20
     * @var string
21
     */
22
    private static $url_segment = 'dataobjectonerecordupdate';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
23
24
    public static function popup_link_only($className, $recordID)
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...
25
    {
26
        DataObjectSorterRequirements::popup_link_requirements();
27
        return Injector::inst()->get('DataObjectOneRecordUpdateController')->Link('show/'.$className."/".$recordID);
28
    }
29 View Code Duplication
    public static function popup_link($className, $recordID, $linkText = 'click here to edit')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
30
    {
31
        $link = DataObjectOneRecordUpdateController::popup_link_only($className, $recordID);
32
        if ($link) {
33
            return '
34
                <a href="'.$link.'" class="modalPopUp modal-popup" data-width="800" data-height="600" data-rel="window.open(\''.$link.'\', \'sortlistFor'.$className.$recordID.'\',\'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=600,left = 440,top = 200\'); return false;">'.$linkText.'</a>';
35
        }
36
    }
37
38 View Code Duplication
    public function init()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
39
    {
40
        //must set this first.
41
        Config::inst()->update('SSViewer', 'theme_enabled', Config::inst()->get('DataObjectSorterRequirements', 'run_through_theme'));
42
        parent::init();
43
        if (! Director::is_ajax()) {
44
            DataObjectSorterRequirements::popup_requirements('onerecord');
45
            $url = Director::absoluteURL(
46
                 Injector::inst()->get('DataObjectOneRecordUpdateController')->Link('onerecordform')
47
            );
48
            Requirements::customScript(
49
                "var DataObjectOneRecordUpdateURL = '".$url."'",
50
                'DataObjectOneRecordUpdateURL'
51
            );
52
        }
53
    }
54
55
    public function onerecordform()
56
    {
57
        Versioned::set_reading_mode('Stage.Stage');
58
        $table = $this->SecureTableToBeUpdated();
59
        $record = $this->SecureRecordToBeUpdated();
60
        $obj = $table::get()->byID($record);
61
        if (!$obj) {
62
            user_error("record could not be found!", E_USER_ERROR);
63
        }
64
        if (! $obj->canEdit()) {
65
            $this->permissionFailureStandard();
66
        }
67
        $formFields = $this->getFormFields($obj);
0 ignored issues
show
Documentation Bug introduced by
The method getFormFields does not exist on object<DataObjectOneRecordUpdateController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
68
        if (!$formFields) {
69
            user_error("Form Fields could not be Found", E_USER_ERROR);
70
        }
71
        $fields = new FieldList(
72
            new HiddenField("Table", "Table", $table),
73
            new HiddenField("Record", "Record", $record)
74
        );
75
        foreach ($formFields as $f) {
76
            $fields->push($f);
77
        }
78
79
        $form = new Form(
80
            $controller = $this,
81
            $name = "OneRecordForm",
82
            $fields,
83
            $actions = new FieldList(new FormAction("save", "save and close"))
84
        );
85
        $form->loadDataFrom($obj);
86
        return $form;
87
    }
88
89
    public function save($data, $form)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

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

Loading history...
90
    {
91
        $table = $this->SecureTableToBeUpdated();
92
        $record = $this->SecureRecordToBeUpdated();
93
        $obj = $table::get()->byID($record);
94
        if ($obj->canEdit()) {
95
            $form->saveInto($obj);
96
            $obj->write();
97
            return '
98
                <p>Your changes have been saved, please <a href="#" onclick="self.close(); return false;">close window</a>.</p>
99
                <script type="text/javascript">self.close();</script>';
100
        } else {
101
            return $this->permissionFailureStandard();
102
        }
103
    }
104
105
    public function show()
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...
106
    {
107
        $table = $this->SecureTableToBeUpdated();
108
        $record = $this->SecureRecordToBeUpdated();
109
        $obj = $table::get()->byID($record);
110
        if ($obj->canEdit()) {
111
            //..
112
        } else {
113
            return $this->permissionFailure();
0 ignored issues
show
Bug introduced by
The method permissionFailure() does not exist on DataObjectOneRecordUpdateController. Did you maybe mean permissionFailureStandard()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
114
        }
115
    }
116
}
117