LockableExtension   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 17
c 1
b 0
f 0
dl 0
loc 52
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCMSFields() 0 3 1
A updateCMSActions() 0 6 3
A lockFields() 0 6 2
A LockRecord() 0 6 1
A canEdit() 0 6 2
1
<?php
2
3
namespace LeKoala\CommonExtensions;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\ORM\DataExtension;
8
use LeKoala\CmsActions\CustomAction;
9
10
/**
11
 * Makes record locked
12
 *
13
 * Unlocking require special action
14
 *
15
 * @property \LeKoala\CommonExtensions\LockableExtension|DataObject $owner
16
 * @property boolean $IsLocked
17
 */
18
class LockableExtension extends DataExtension
19
{
20
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
21
        "IsLocked" => "Boolean"
22
    ];
23
24
    public function updateCMSFields(FieldList $fields)
25
    {
26
        $fields->removeByName('IsLocked');
27
    }
28
29
    /**
30
     *  Use this in your model. It has to run last and cannot be done automatically
31
     *
32
     *  if($this->hasExtension(LockableExtension::class)) {
33
     *     $this->lockFields($fields);
34
     *  }
35
     *
36
     * @param FieldList $fields
37
     * @return void
38
     */
39
    public function lockFields(FieldList $fields)
40
    {
41
        if (!$this->owner->IsLocked) {
42
            return;
43
        }
44
        $fields->makeReadonly();
45
    }
46
47
    public function canEdit($member)
48
    {
49
        if ($this->owner->IsLocked) {
50
            return false;
51
        }
52
        return parent::canEdit($member);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::canEdit($member) targeting SilverStripe\ORM\DataExtension::canEdit() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
53
    }
54
55
    public function updateCMSActions(FieldList $actions)
56
    {
57
        if ($this->owner->ID && !$this->owner->IsLocked) {
0 ignored issues
show
Bug Best Practice introduced by
The property ID does not exist on LeKoala\CommonExtensions\LockableExtension. Did you maybe forget to declare it?
Loading history...
58
            $lockRecord = new CustomAction("LockRecord", "Lock");
59
            $lockRecord->setConfirmation("Are you sure to lock this record?");
60
            $actions->push($lockRecord);
61
        }
62
    }
63
64
    public function LockRecord($data, $form, $controller)
0 ignored issues
show
Unused Code introduced by
The parameter $controller 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

64
    public function LockRecord($data, $form, /** @scrutinizer ignore-unused */ $controller)

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...
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

64
    public function LockRecord(/** @scrutinizer ignore-unused */ $data, $form, $controller)

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...
Unused Code introduced by
The parameter $form 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

64
    public function LockRecord($data, /** @scrutinizer ignore-unused */ $form, $controller)

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...
65
    {
66
        $this->owner->IsLocked = true;
67
        $this->owner->write();
0 ignored issues
show
Bug introduced by
The method write() does not exist on LeKoala\CommonExtensions\LockableExtension. ( Ignorable by Annotation )

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

67
        $this->owner->/** @scrutinizer ignore-call */ 
68
                      write();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
69
        return 'Record locked';
70
    }
71
}
72