Completed
Pull Request — master (#1)
by Nic
09:01
created

ProductWishListForm()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0909

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 11
cts 13
cp 0.8462
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 12
nc 12
nop 1
crap 5.0909
1
<?php
2
3
/**
4
 * Class ProductWishListControllerExtension
5
 */
6
class ProductWishListControllerExtension extends Extension
7
{
8
9
    /**
10
     * @var array
11
     */
12
    private static $allowed_actions = [
13
        'ProductWishListForm',
14
        'update',
15
    ];
16
17
    /**
18
     * @param null $wishList
19
     *
20
     * @return Form
21
     */
22 1
    public function ProductWishListForm($wishList = null)
23
    {
24 1
        $wishList = ($wishList !== null && $wishList instanceof ProductWishList && $wishList->exists()) ? $wishList : Injector::inst()->get('ProductWishList');
25
26 1
        $fields = $wishList->getFrontEndFields();
27
28 1
        if ($wishList->getIsEditing()) {
29
            $fields->push(HiddenField::create('ID')->setValue($wishList->ID));
30
        }
31
32 1
        $actions = $wishList->getFrontEndActions();
33
34 1
        $required = $wishList->getFrontEndRequiredFields();
35
36 1
        $controller = $this->getController();
37
38 1
        $form = Form::create($controller, 'ProductWishListForm', $fields, $actions, $required);
39
40 1
        $form->loadDataFrom($wishList);
41
42 1
        $this->owner->extend('updateProductWishListForm', $form);
43
44 1
        return $form;
45
    }
46
47
    /**
48
     * @return Object
49
     */
50 1
    protected function getController(){
51 1
        return ($this->owner->hasMethod('getCurrentPage'))
52 1
            ? $this->owner->getCurrentPage()
53 1
            : $this->owner;
54
    }
55
56
    /**
57
     * @param $data
58
     * @param Form $form
59
     */
60
    public function doProcessWishList($data, Form $form)
61
    {
62
        $wishList = (isset($data['ID'])) ? ProductWishList::get()->byID($data['ID']) : ProductWishList::create();
63
        $form->saveInto($wishList);
64
        if($wishList->write()){
65
            return $this->getController()->redirect($this->getController()->Link($wishList->getUpdateLink()));
0 ignored issues
show
Bug introduced by
The method getUpdateLink() does not exist on DataObject. Did you maybe mean update()?

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...
66
        }
67
        return $this->getController()->redirect($this->getController()->Link('error'));
68
    }
69
70
    public function update(SS_HTTPRequest $request = null)
71
    {
72
        if ($request === null) {
73
            $request = Controller::curr()->getRequest();
74
        }
75
76
        $wishList = ProductWishList::get()->byID($request->param('ID'));
77
78
        return $this->owner->customise([
79
            'ProductWishListForm' => $this->ProductWishListForm($wishList),
0 ignored issues
show
Bug introduced by
It seems like $wishList defined by \ProductWishList::get()-...($request->param('ID')) on line 76 can also be of type object<DataObject>; however, ProductWishListControlle...::ProductWishListForm() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
80
        ]);
81
    }
82
83
}