ProductControllerExtension::WishListForm()   A
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 22
ccs 0
cts 13
cp 0
rs 9.2222
cc 6
nc 7
nop 1
crap 42
1
<?php
2
3
namespace Dynamic\Wishlist\Extensions;
4
5
use Dynamic\Wishlist\Form\AddToWishListForm;
6
use Dynamic\Wishlist\Form\RemoveFromWishListForm;
7
use Dynamic\Wishlist\Model\ProductWishList;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\Core\Extension;
10
use SilverStripe\Security\Security;
11
12
/**
13
 * Class ProductExtension
14
 * @package Dynamic\Wishlist\Extensions
15
 *
16
 * @property-read \SilverStripe\Control\Controller $owner
17
 */
18
class ProductControllerExtension extends Extension
19
{
20
    /**
21
     * @var array
22
     */
23
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
24
        'WishListForm',
25
    ];
26
27
    /**
28
     * @param int|HTTPRequest $productID
29
     * @return \Dynamic\Wishlist\Form\AddToWishListForm|\Dynamic\Wishlist\Form\RemoveFromWishListForm|bool
30
     */
31
    public function WishListForm($productID = 0)
32
    {
33
        if (!Security::getCurrentUser()) {
34
            return false;
35
        }
36
37
        if (!$productID) {
38
            $productID = $this->owner->ID;
39
        } else if ($productID instanceof HTTPRequest) {
40
            $productID = $productID->postVar('ProductID');
41
        }
42
43
        /** @var ProductWishList $list */
44
        $list = ProductWishList::get()->filter([
45
            'MemberID' => Security::getCurrentUser()->ID,
46
        ])->first();
47
48
        if (!$list || !$list->Products()->byID($productID)) {
0 ignored issues
show
introduced by
$list is of type Dynamic\Wishlist\Model\ProductWishList, thus it always evaluated to true.
Loading history...
Bug introduced by
The method Products() does not exist on Dynamic\Wishlist\Model\ProductWishList. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

48
        if (!$list || !$list->/** @scrutinizer ignore-call */ Products()->byID($productID)) {
Loading history...
49
            return AddToWishListForm::create($this->owner, __FUNCTION__, $productID);
50
        }
51
52
        return RemoveFromWishListForm::create($this->owner, __FUNCTION__, $productID);
53
    }
54
}
55