Completed
Pull Request — master (#2)
by Matthew
13:57
created

ProductExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 18
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRemoveLink() 0 15 3
A canRemoveFromWishList() 0 16 3
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
        if ($this->canRemoveFromWishList()) {
26
            /** @var \SilverStripe\CMS\Model\SiteTree $page */
27
            if ($page = $wishListPage::get()->first()) {
28
                return Controller::join_links(
29
                    $page->Link('remove'),
30
                    Controller::curr()->getRequest()->param('ID'),
31
                    $this->owner->ID
32
                );
33
            }
34
        }
35
36
        return false;
37
    }
38
39
    /**
40
     * @return bool
41
     */
42
    protected function canRemoveFromWishList()
43
    {
44
        $request = Controller::curr()->getRequest();
45
        if ($request->param('Action') != 'view') {
46
            return false;
47
        }
48
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
            return false;
55
        }
56
57
        return $list->canEdit(Security::getCurrentUser());
58
    }
59
}
60