Completed
Pull Request — master (#2)
by Matthew
05:26 queued 03:06
created

ProductExtension::canRemoveFromWishList()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
ccs 0
cts 10
cp 0
rs 9.9666
c 0
b 0
f 0
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
        if ($request->param('Action') != 'view') {
50
            return false;
51
        }
52
53
        $list = ProductWishList::get()->filter([
54
            'URLSegment' => $request->param('ID'),
55
        ])->first();
56
57
        if (!$list) {
0 ignored issues
show
introduced by
$list is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
58
            return false;
59
        }
60
61
        return $list->canEdit(Security::getCurrentUser());
62
    }
63
}
64