Passed
Pull Request — master (#9)
by Jason
03:45
created

ProductControllerExtension   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 25
c 1
b 0
f 0
dl 0
loc 92
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isProductReserved() 0 3 1
A addProductReservation() 0 15 2
A reserveproduct() 0 12 5
A getReservationHash() 0 3 1
A onAfterInit() 0 3 1
A validReservation() 0 7 2
1
<?php
2
3
namespace Dynamic\Foxy\Inventory\Extension;
4
5
use Dynamic\Foxy\Inventory\Model\CartReservation;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\Core\Extension;
10
use SilverStripe\View\Requirements;
11
12
class ProductControllerExtension extends Extension
13
{
14
    /**
15
     *
16
     */
17
    public function onAfterInit()
18
    {
19
        Requirements::javascript('dynamic/silverstripe-foxy-inventory: client/dist/javascript/scripts.min.js');
20
    }
21
22
    /**
23
     * @var array
24
     */
25
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
26
        'reserveproduct' => '->validReservation',
27
    ];
28
29
    /**
30
     * @param HTTPRequest|null $request
31
     * @return bool
32
     */
33
    public function validReservation(HTTPRequest $request = null)
34
    {
35
        if (!$request instanceof HTTPRequest) {
36
            $request = Controller::curr()->getRequest();
0 ignored issues
show
Unused Code introduced by
The assignment to $request is dead and can be removed.
Loading history...
37
        }
38
39
        return true;
40
    }
41
42
    /**
43
     * @param HTTPRequest $request
44
     */
45
    public function reserveproduct(HTTPRequest $request)
46
    {
47
        $code = $request->getVar('code');
48
        $id = $request->getVar('id');
49
        $expires = $request->getVar('expires');
50
51
        if (!$code || !$id || !$expires) {
52
            return false;
53
        }
54
55
        if (!$this->isProductReserved($code, $id, $expires)) {
56
            $this->addProductReservation($code, $id, $expires);
57
        }
58
    }
59
60
    /**
61
     * @param $code
62
     * @param $id
63
     * @param $expires
64
     * @return bool
65
     * @throws \SilverStripe\ORM\ValidationException
66
     */
67
    protected function addProductReservation($code, $id, $expires)
68
    {
69
        $product = SiteTree::get()->filter('Code', $code)->first();
70
        // todo: determine way to query products now that any site tree can be a product
71
72
        $reservation = CartReservation::create();
73
        $reservation->ReservationCode = $this->getReservationHash($code, $id, $expires);
74
        $reservation->CartProductID = $id;
75
        $reservation->Code = $code;
76
        $reservation->Expires = date('Y-m-d H:i:s', $expires);
77
        if ($product !== null) {
78
            $reservation->ProductID = $product->ID;
79
        }
80
81
        return $reservation->write() > 0;
82
    }
83
84
    /**
85
     * @param $code
86
     * @param $id
87
     * @param $expires
88
     * @return \SilverStripe\ORM\DataObject
89
     */
90
    protected function isProductReserved($code, $id, $expires)
91
    {
92
        return CartReservation::get()->filter('ReservationCode', $this->getReservationHash($code, $id, $expires))->first();
93
    }
94
95
    /**
96
     * @param $code
97
     * @param $id
98
     * @param $expires
99
     * @return string
100
     */
101
    protected function getReservationHash($code, $id, $expires)
102
    {
103
        return md5($code.$id.$expires);
104
    }
105
}
106