Passed
Push — master ( 5540e2...e2d432 )
by Jason
02:58
created

OptionType::canEdit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Foxy\Model;
4
5
use Dynamic\Products\Page\Product;
0 ignored issues
show
Bug introduced by
The type Dynamic\Products\Page\Product 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
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\GridField\GridField;
8
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
9
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
10
use SilverStripe\ORM\DataObject;
11
use SilverStripe\Security\Permission;
12
use SilverStripe\Security\Security;
13
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
14
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
15
16
class OptionType extends DataObject
17
{
18
    /**
19
     * @var array
20
     */
21
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
22
        'Title' => 'Varchar(255)',
23
        'SortOrder' => 'Int',
24
    ];
25
26
    /**
27
     * @var array
28
     */
29
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
30
        'Options' => ProductOption::class,
31
    ];
32
33
    /**
34
     * @var array
35
     */
36
    private static $many_many_extraFields = [
0 ignored issues
show
introduced by
The private property $many_many_extraFields is not used, and could be removed.
Loading history...
37
        'Options' => [
38
            'WeightModifier' => 'Decimal',
39
            'CodeModifier' => 'Text',
40
            'PriceModifier' => 'Currency',
41
            'WeightModifierAction' => "Enum('Add,Subtract,Set', null)",
42
            'CodeModifierAction' => "Enum('Add,Subtract,Set', null)",
43
            'PriceModifierAction' => "Enum('Add,Subtract,Set', null)",
44
            'Available' => 'Boolean',
45
            'SortOrder' => 'Int',
46
        ],
47
    ];
48
49
    /**
50
     * @var string
51
     */
52
    private static $table_name = 'OptionType';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
53
54
    public function getCMSFields()
55
    {
56
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
57
            $fields->removeByName([
58
                'SortOrder',
59
                'ProductID',
60
                'Options',
61
            ]);
62
63
            if ($this->ID) {
64
                $config = GridFieldConfig_RelationEditor::create();
65
                $config
66
                    ->addComponents([
67
                        new GridFieldAddExistingSearchButton(),
68
                        new GridFieldOrderableRows('SortOrder'),
69
                    ])
70
                    ->removeComponentsByType([
71
                        GridFieldAddExistingAutocompleter::class,
72
                    ]);
73
                $options = GridField::create(
74
                    'Options',
75
                    'Options',
76
                    $this->owner->Options()->sort('SortOrder'),
0 ignored issues
show
Bug introduced by
The method Options() does not exist on null. ( Ignorable by Annotation )

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

76
                    $this->owner->/** @scrutinizer ignore-call */ 
77
                                  Options()->sort('SortOrder'),

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...
Bug Best Practice introduced by
The property owner does not exist on Dynamic\Foxy\Model\OptionType. Since you implemented __get, consider adding a @property annotation.
Loading history...
77
                    $config
78
                );
79
                $fields->addFieldToTab('Root.Main', $options);
80
            }
81
        });
82
83
        return parent::getCMSFields();
84
    }
85
86
    /**
87
     * @param $member
88
     * @return bool|int|void
89
     */
90
    public function canCreate($member = null, $context = [])
91
    {
92
        if (!$member) {
93
            $member = Security::getCurrentUser();
94
        }
95
96
        return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS');
97
    }
98
99
    /**
100
     * @param $member
101
     * @return bool|int|void|null
102
     */
103
    public function canEdit($member = null, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context 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

103
    public function canEdit($member = null, /** @scrutinizer ignore-unused */ $context = [])

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...
104
    {
105
        if (!$member) {
106
            $member = Security::getCurrentUser();
107
        }
108
109
        return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS');
110
    }
111
112
    /**
113
     * @param $member
114
     * @return bool|int|void
115
     */
116
    public function canDelete($member = null, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context 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

116
    public function canDelete($member = null, /** @scrutinizer ignore-unused */ $context = [])

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...
117
    {
118
        if (!$member) {
119
            $member = Security::getCurrentUser();
120
        }
121
122
        return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS');
123
    }
124
}
125