Completed
Push — master ( fb1da0...84ba9e )
by Nic
10s
created

ProductWishList::getUpdateLink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * Class ProductWishList
5
 *
6
 * @property string $Title
7
 * @property bool $Private
8
 * @property int $MemberID
9
 * @method Member $Member
10
 */
11
class ProductWishList extends DataObject implements PermissionProvider, Dynamic\ViewableDataObject\VDOInterfaces\ViewableDataObjectInterface, ManageableDataObjectInterface
12
{
13
14
    /**
15
     * @var array
16
     */
17
    private static $db = [
18
        'Title' => 'Varchar(100)',
19
        'Private' => 'Boolean',
20
    ];
21
22
    /**
23
     * @var array
24
     */
25
    private static $has_one = [
26
        'Member' => 'Member',
27
    ];
28
29
    /**
30
     *
31
     */
32 10
    public function onBeforeWrite()
33
    {
34 10
        parent::onBeforeWrite();
35
36 10
        if (!$this->MemberID > 0) {
37 10
            $this->MemberID = Member::currentUserID();
38 10
        }
39 10
    }
40
41
    /**
42
     * @param null $params
43
     *
44
     * @return FieldList
45
     */
46 1
    public function getFrontEndFields($params = null)
47
    {
48 1
        $fields = parent::getFrontEndFields($params);
49
50 1
        $fields->removeByName([
51 1
            'MemberID',
52 1
            'MenuTitle',
53 1
            'URLSegment',
54 1
            'MetaTitle',
55 1
            'MetaDescription',
56 1
        ]);
57
58 1
        return $fields;
59
    }
60
61
    /**
62
     * @param bool $showCancel
63
     *
64
     * @return FieldList
65
     */
66 1
    public function getFrontEndActions($showCancel = false)
67
    {
68 1
        $processTitle = ($this->getIsEditing()) ? "Update {$this->i18n_singular_name()}" : "Create {$this->i18n_singular_name()}";
69 1
        $actions = FieldList::create(
70 1
            FormAction::create('doSaveObject')
71 1
                      ->setTitle($processTitle)
72 1
        );
73
74 1
        if ($showCancel === true) {
75 1
            $actions->insertBefore('action_doSaveObject', CancelFormAction::create('Cancel'));
76 1
        }
77
78 1
        $this->extend('updateFrontEndActions', $actions);
79
80 1
        return $actions;
81
    }
82
83
    /**
84
     * @return RequiredFields
85
     */
86 1
    public function getFrontEndRequiredFields()
87
    {
88 1
        $fields = RequiredFields::create([
89 1
            'Title',
90 1
        ]);
91
92 1
        $this->extend('updateFrontEndRequiredFields', $fields);
93
94 1
        return $fields;
95
    }
96
97
    /**
98
     * @return bool
99
     */
100 1
    public function getIsEditing()
101
    {
102 1
        $params = Controller::curr()->getRequest()->latestParams();
103
104 1
        return isset($params['Action']) && $params['Action'] == 'edit' && isset($params['ID']);
105
    }
106
107
    /**
108
     * set ParentPage for ViewableDataobject
109
     *
110
     * @return string
111
     */
112
    public function getParentPage()
113
    {
114
        $class = $this->config()->get('listing_page_class');
115
        return $class::get()->first();
116
    }
117
118
    /**
119
     * @return String|bool
120
     */
121
    public function getUpdateLink()
122
    {
123
        return ($this->ID > 0) ? Controller::join_links($this->getParentPage()->Link(), 'edit', $this->ID) : false;
0 ignored issues
show
Bug introduced by
The method Link cannot be called on $this->getParentPage() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
124
    }
125
126
    /**
127
     * set ViewAction for ViewableDataobject
128
     *
129
     * @return string
130
     */
131 1
    public function getViewAction()
132
    {
133 1
        return 'view';
134
    }
135
136
    /**
137
     * @return array
138
     */
139 1
    public function providePermissions()
140
    {
141
        return [
142
            'WishList_EDIT' => [
143 1
                'name' => 'Edit a Wish List',
144 1
                'category' => 'Wish List Permissions',
145 1
            ],
146
            'WishList_DELETE' => [
147 1
                'name' => 'Delete a Wish List',
148 1
                'category' => 'Wish List Permissions',
149 1
            ],
150
            'WishList_CREATE' => [
151 1
                'name' => 'Create a Wish List',
152 1
                'category' => 'Wish List Permissions',
153 1
            ],
154
            'WishList_VIEW' => [
155 1
                'name' => 'View a Wish List',
156 1
                'category' => 'Wish List Permissions',
157 1
            ],
158 1
        ];
159
    }
160
161
    /**
162
     * @param Member|null $member
163
     *
164
     * @return bool|int
165
     */
166 1 View Code Duplication
    public function canEdit($member = null)
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...
167
    {
168 1
        $member = ($member === null) ? Member::currentUser() : $member;
169
170 1
        return ((Permission::check('WishList_EDIT', 'any',
171 1
                        $member) && $member->ID == $this->MemberID) || Permission::check('ADMIN'));
0 ignored issues
show
Documentation introduced by
$member is of type object<DataObject>, but the function expects a integer|object<Member>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
172
    }
173
174
    /**
175
     * @param Member|null $member
176
     *
177
     * @return bool|int
178
     */
179 1 View Code Duplication
    public function canDelete($member = null)
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...
180
    {
181 1
        $member = ($member === null) ? Member::currentUser() : $member;
182
183 1
        return ((Permission::check('WishList_DELETE', 'any',
184 1
                        $member) && $member->ID == $this->MemberID) || Permission::check('ADMIN'));
0 ignored issues
show
Documentation introduced by
$member is of type object<DataObject>, but the function expects a integer|object<Member>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
185
    }
186
187
    /**
188
     * @param Member|null $member
189
     *
190
     * @return bool|int
191
     */
192 1
    public function canCreate($member = null)
193
    {
194 1
        return Member::currentUser() && Permission::check('WishList_CREATE', 'any', $member);
195
    }
196
197
    /**
198
     * @param Member|null $member
199
     *
200
     * @return bool
201
     */
202 2 View Code Duplication
    public function canView($member = null)
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...
203
    {
204 2
        $member = ($member === null) ? Member::currentUser() : $member;
205
206 2
        return (!$this->Private) || ((Permission::check('WishList_VIEW', 'any',
207 2
                        $member) && $member->ID == $this->MemberID) || Permission::check('ADMIN'));
0 ignored issues
show
Documentation introduced by
$member is of type object<DataObject>, but the function expects a integer|object<Member>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
208
    }
209
210
}