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

ProductWishListControllerExtension   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 48.39%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 9
dl 0
loc 78
ccs 15
cts 31
cp 0.4839
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B ProductWishListForm() 0 24 5
A getController() 0 5 2
A doProcessWishList() 0 9 3
A update() 0 12 2
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
}