ProductExtension::getRemoveLink()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 18
ccs 0
cts 11
cp 0
rs 9.9332
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
namespace Dynamic\Wishlist\Extensions;
4
5
6
use Dynamic\Wishlist\Model\ProductWishList;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Core\Extension;
10
use SilverStripe\Security\Security;
11
12
/**
13
 * Class ProductExtension
14
 * @package Dynamic\Wishlist\Extensions
15
 */
16
class ProductExtension extends Extension
17
{
18
19
    /**
20
     * @return bool|String
21
     */
22
    public function getRemoveLink()
23
    {
24
        $wishListPage = Config::inst()->get(ProductWishList::class, 'listing_page_class');
25
        /** @var \SilverStripe\CMS\Model\SiteTree $page */
26
        $page = $page = $wishListPage::get()->first();
0 ignored issues
show
Unused Code introduced by
The assignment to $page is dead and can be removed.
Loading history...
27
28
        if (!$page) {
0 ignored issues
show
introduced by
$page is of type SilverStripe\CMS\Model\SiteTree, thus it always evaluated to true.
Loading history...
29
            return false;
30
        }
31
32
        if (!$this->canRemoveFromWishList()) {
33
            return false;
34
        }
35
36
        return Controller::join_links(
37
            $page->Link('remove'),
38
            Controller::curr()->getRequest()->param('ID'),
39
            $this->owner->ID
40
        );
41
    }
42
43
    /**
44
     * @return bool
45
     */
46
    protected function canRemoveFromWishList()
47
    {
48
        $request = Controller::curr()->getRequest();
49
        $list = ProductWishList::get()->filter([
50
            'URLSegment' => $request->param('ID'),
51
        ])->first();
52
53
        if (!$list) {
0 ignored issues
show
introduced by
$list is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
54
            $list = ProductWishList::get()->filter([
55
                'MemberID' => Security::getCurrentUser()->ID,
56
            ])->first();
57
        }
58
59
        if (!$list) {
0 ignored issues
show
introduced by
$list is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
60
            return false;
61
        }
62
63
        return $list->canEdit(Security::getCurrentUser());
64
    }
65
}
66