ViewableCartExtension::getContinueLink()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
c 0
b 0
f 0
nc 5
nop 0
dl 0
loc 20
rs 9.9
1
<?php
2
3
namespace SilverShop\Extension;
4
5
use SilverShop\Cart\ShoppingCart;
6
use SilverShop\Page\CartPage;
7
use SilverShop\Page\CheckoutPage;
8
use SilverShop\Page\ProductCategory;
9
use SilverStripe\Control\Director;
10
use SilverStripe\Core\Extension;
11
12
/**
13
 * ViewableData extension that provides access to the cart from anywhere.
14
 * Also handles last-minute recalculation, if required.
15
 * All order updates: quantities, modifiers etc should be done before
16
 * this function is called.
17
 *
18
 * @package shop
19
 */
20
class ViewableCartExtension extends Extension
21
{
22
    /**
23
     * Get the cart, and do last minute calculation if necessary.
24
     */
25
    public function Cart()
26
    {
27
        $order = ShoppingCart::curr();
28
        if (!$order || !$order->Items() || !$order->Items()->exists()) {
0 ignored issues
show
introduced by
$order is of type SilverShop\Model\Order, thus it always evaluated to true.
Loading history...
29
            return false;
30
        }
31
32
        return $order;
33
    }
34
35
    public function getContinueLink()
36
    {
37
        if ($cartPage = CartPage::get()->first()) {
38
            if ($cartPage->ContinuePageID) {
39
                return $cartPage->ContinuePage()->Link();
40
            }
41
        }
42
43
        $maincategory = ProductCategory::get()
44
            ->sort(
45
                [
46
                    'ParentID' => 'ASC',
47
                    'ID' => 'ASC',
48
                ]
49
            )->first();
50
        if ($maincategory) {
51
            return $maincategory->Link();
52
        }
53
54
        return Director::baseURL();
55
    }
56
57
    public function getCartLink()
58
    {
59
        return CartPage::find_link();
60
    }
61
62
    public function getCheckoutLink()
63
    {
64
        return CheckoutPage::find_link();
65
    }
66
}
67