ProductControllerExtension::reserveproduct()   A
last analyzed

Complexity

Conditions 6
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 14
rs 9.2222
cc 6
nc 2
nop 1
1
<?php
2
3
namespace Dynamic\Foxy\Inventory\Extension;
4
5
use Dynamic\Foxy\Inventory\Model\CartReservation;
6
use Dynamic\Foxy\Model\FoxyHelper;
7
use SilverStripe\CMS\Model\SiteTree;
8
use SilverStripe\Control\Controller;
9
use SilverStripe\Control\HTTPRequest;
10
use SilverStripe\Core\Extension;
11
use SilverStripe\View\Requirements;
12
13
class ProductControllerExtension extends Extension
14
{
15
    /**
16
     *
17
     */
18
    public function onAfterInit()
19
    {
20
        Requirements::javascript('dynamic/silverstripe-foxy-inventory: client/dist/javascript/inventory.js');
21
    }
22
23
    /**
24
     * @var array
25
     */
26
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
27
        'reserveproduct' => '->validReservation',
28
    ];
29
30
    /**
31
     * @param HTTPRequest|null $request
32
     * @return bool
33
     */
34
    public function validReservation(HTTPRequest $request = null)
35
    {
36
        if (!$request instanceof HTTPRequest) {
37
            $request = Controller::curr()->getRequest();
0 ignored issues
show
Unused Code introduced by
The assignment to $request is dead and can be removed.
Loading history...
38
        }
39
40
        return true;
41
    }
42
43
    /**
44
     * @param HTTPRequest $request
45
     * @return bool|void
46
     * @throws \SilverStripe\ORM\ValidationException
47
     */
48
    public function reserveproduct(HTTPRequest $request)
49
    {
50
        $code = $request->getVar('code');
51
        $id = $request->getVar('id');
52
        $expires = $request->getVar('expires');
53
        $quantity = $request->getVar('quantity');
54
        $cart = $request->getVar('cart');
55
56
        if (!$code || !$id || !$expires || !$quantity || !$cart) {
57
            return false;
58
        }
59
60
        //if (!$this->isProductReserved($code, $id, $expires)) {
61
            $this->addProductReservation($code, $id, $expires, $quantity, $cart);
62
        //}
63
    }
64
65
    /**
66
     * @param $code
67
     * @param $id
68
     * @param $expires
69
     * @param $quantity
70
     * @param $cart
71
     * @throws \SilverStripe\ORM\ValidationException
72
     */
73
    protected function addProductReservation($code, $id, $expires, $quantity, $cart)
74
    {
75
        $codeFilter = function (\Page $page) use ($code) {
76
            return $page->Code == $code;
77
        };
78
79
        if ($product = FoxyHelper::singleton()->getProducts()->filterByCallback($codeFilter)->first()) {
80
            $existing = CartReservation::get()
81
                ->filter([
82
                    'Cart' => $cart,
83
                    'Code' => $code,
84
                    'CartProductID' => $id,
85
                    'ProductID' => $product->ID
86
                ]);
87
88
            $remaining = $quantity - $existing->count();
89
90
            if ($remaining > 0) {
91
                for ($i = 0; $i < $remaining; $i++) {
92
                    $reservation = CartReservation::create();
93
                    //$reservation->ReservationCode = $this->getReservationHash($code, $id, $expires, $cart);
94
                    $reservation->CartProductID = $id;
95
                    $reservation->Code = $code;
96
                    $reservation->Expires = date('Y-m-d H:i:s', $expires);
97
                    $reservation->ProductID = $product->ID;
98
                    $reservation->Cart = $cart;
99
100
                    $reservation->write();
101
                }
102
            }
103
        }
104
    }
105
106
    /**
107
     * @param $code
108
     * @param $id
109
     * @param $expires
110
     * @return \SilverStripe\ORM\DataObject
111
     */
112
    protected function isProductReserved($code, $id, $expires)
113
    {
114
        return CartReservation::get()
115
            ->filter('ReservationCode', $this->getReservationHash($code, $id, $expires))
0 ignored issues
show
Bug introduced by
The call to Dynamic\Foxy\Inventory\E...n::getReservationHash() has too few arguments starting with cart. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
            ->filter('ReservationCode', $this->/** @scrutinizer ignore-call */ getReservationHash($code, $id, $expires))

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
116
            ->first();
117
    }
118
119
    /**
120
     * @param $code
121
     * @param $id
122
     * @param $expires
123
     * @return string
124
     */
125
    protected function getReservationHash($code, $id, $expires, $cart)
126
    {
127
        return md5($code . $id . $expires . $cart . rand());
128
    }
129
}
130