EcommerceTemplateTest::index()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
nop 0
1
<?php
2
3
/**
4
 * @description: used to display a random product in the Template Test.
5
 *
6
 * @authors: Nicolaas [at] Sunny Side Up .co.nz
7
 * @package: ecommerce
8
 * @sub-package: control
9
 * @inspiration: Silverstripe Ltd, Jeremy
10
 **/
11
class EcommerceTemplateTest extends Page_Controller
12
{
13
    public function index()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
14
    {
15
        return $this->renderWith(array("EcommerceTemplateTest", "Page"));
16
    }
17
    /**
18
     * Goes through all products and find one that
19
     * "canPurchase".
20
     *
21
     * @return Product
22
     */
23
    public function RandomProduct()
24
    {
25
        $offSet = 0;
26
        $product = true;
27
        $notForSale = true;
28
        while ($product && $notForSale) {
29
            $notForSale = false;
30
            $product = Product::get()
31
                ->where('"AllowPurchase" = 1  AND "Price" > 0')
32
                ->sort('RAND()')
33
                ->limit(1, $offSet)
34
                ->First();
35
            if ($product) {
36
                $notForSale = $product->canPurchase() ? false : true;
37
            }
38
            ++$offSet;
39
        }
40
41
        return $product;
42
    }
43
44
    public function SubmittedOrder()
45
    {
46
        $lastStatusOrder = OrderStep::last_order_step();
47
        if ($lastStatusOrder) {
48
            return
49
            DataObject::get_one(
50
                'Order',
51
                array('StatusID' => $lastStatusOrder->ID),
52
                $cacheDataObjectGetOne = true,
53
                'RAND()'
54
            );
55
        }
56
    }
57
58
    /**
59
     * This is used for template-ty stuff.
60
     *
61
     * @return bool
62
     */
63
    public function IsEcommercePage()
64
    {
65
        return true;
66
    }
67
}
68