ShopController::redeem()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Facades\Mail;
6
use App\Facades\User as UserFacade;
7
use App\Models\Country;
8
use App\Models\PaymentCheckout;
9
use App\Models\Purse;
10
use App\Models\ShopHistory;
11
use App\Models\ShopInventory;
12
use App\Models\ShopItem;
13
use App\Models\Voucher;
14
use Illuminate\Http\JsonResponse;
15
use Illuminate\Http\RedirectResponse;
16
use Illuminate\Http\Request;
17
use Illuminate\Http\Response;
18
use Laravel\Lumen\Http\Redirector;
19
use Laravel\Lumen\Http\ResponseFactory;
20
use Laravel\Lumen\Routing\Controller as BaseController;
21
22
/**
23
 * Class ShopController.
24
 */
25
class ShopController extends BaseController
26
{
27
    /**
28
     * List all Shop Countries.
29
     *
30
     * @return JsonResponse
31
     */
32
    public function listCountries(): JsonResponse
33
    {
34
        return response()->json(Country::all());
0 ignored issues
show
Bug introduced by
It seems like \App\Models\Country::all() targeting Illuminate\Database\Eloquent\Model::all() can also be of type object<Illuminate\Database\Eloquent\Collection>; however, Laravel\Lumen\Http\ResponseFactory::json() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
35
    }
36
37
    /**
38
     * Get the Shop Inventory of a Country.
39
     *
40
     * @param string $countryCode
41
     *
42
     * @return JsonResponse
43
     */
44
    public function getInventory(string $countryCode): JsonResponse
45
    {
46
        return response()->json(new ShopInventory(Country::where('countryCode', $countryCode)->first()),
0 ignored issues
show
Documentation introduced by
new \App\Models\ShopInve...$countryCode)->first()) is of type object<App\Models\ShopInventory>, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
47
            200, [], JSON_UNESCAPED_SLASHES);
48
    }
49
50
    /**
51
     * Get User Purse.
52
     *
53
     * @param Request $request
54
     *
55
     * @return JsonResponse
56
     */
57
    public function getPurse(Request $request): JsonResponse
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
    {
59
        return response()->json(new Purse(UserFacade::getUser()->uniqueId));
0 ignored issues
show
Documentation introduced by
new \App\Models\Purse(\A...r::getUser()->uniqueId) is of type object<App\Models\Purse>, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
60
    }
61
62
    /**
63
     * Proceed Payment Checkout.
64
     *
65
     * @param string $paymentCategory
66
     * @param int    $countryCode
67
     * @param int    $shopItem
68
     * @param int    $paymentMethod
69
     *
70
     * @return RedirectResponse|Response|Redirector|ResponseFactory
71
     */
72
    public function proceed(string $paymentCategory, int $countryCode, int $shopItem, int $paymentMethod)
73
    {
74
        $paymentCheckout = PaymentCheckout::where('category', $paymentCategory)->where('country', $countryCode)
75
            ->where('item', $shopItem)->where('method', $paymentMethod)->first();
76
77
        if ($paymentCheckout == null) {
78
            return response(view('habbo-web-payments.failed-payment'), 400);
79
        }
80
81
        if ((strtotime($paymentCheckout->generated_at) + 172800) < time()) {
82
            return response(view('habbo-web-payments.canceled-payment'), 400);
83
        }
84
85
        return response(view('habbo-web-payments.proceed', ['payment' => $paymentCheckout]));
86
    }
87
88
    /**
89
     * Success Payment Checkout.
90
     *
91
     * @TODO: Code Business Logic
92
     *
93
     * @param Request $request
94
     * @param string  $paymentCategory
95
     * @param int     $countryCode
96
     * @param int     $shopItem
97
     * @param int     $paymentMethod
98
     *
99
     * @return RedirectResponse|Response|Redirector|ResponseFactory
100
     */
101
    public function success(Request $request, string $paymentCategory, int $countryCode, int $shopItem, int $paymentMethod)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
    {
103
        $paymentCheckout = PaymentCheckout::where('category', $paymentCategory)->where('country', $countryCode)
104
            ->where('item', $shopItem)->where('method', $paymentMethod)->first();
105
106
        if ($paymentCheckout == null) {
107
            return response(view('habbo-web-payments.canceled-payment'), 500);
108
        }
109
110
        $purchaseItem = (new ShopHistory())->store($paymentMethod, UserFacade::getUser()->uniqueId, $shopItem);
111
112
        Mail::send(['email' => UserFacade::getUser()->email, 'purchaseId' => $purchaseItem->transactionId,
113
            'product'       => ShopItem::find($shopItem), 'subject' => 'Purchase completed',
114
        ], 'habbo-web-mail.purchase-confirmation');
115
116
        $paymentCheckout->delete();
117
118
        return response(view('habbo-web-payments.success-payment', ['checkoutId' => $purchaseItem->transactionId]), 200);
119
    }
120
121
    /**
122
     * Get User Purchase History.
123
     *
124
     * @TODO: User Purchase History will be coded on the Future
125
     * @TODO: All Purchases of the CMS are Manually, so will be difficult track.
126
     * @TODO: Probably Administrators will Manually Insert History Through HK
127
     *
128
     * @return JsonResponse
129
     */
130
    public function getHistory(): JsonResponse
131
    {
132
        return response()->json(ShopHistory::where('user_id', UserFacade::getUser()->uniqueId)->get());
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
133
    }
134
135
    /**
136
     * Redeem Voucher.
137
     *
138
     * @TODO: Need to Test if really works
139
     *
140
     * @param Request $request
141
     *
142
     * @return JsonResponse
143
     */
144
    public function redeem(Request $request): JsonResponse
145
    {
146
        if (($voucher = Voucher::where('code', $request->json()->get('voucherCode'))->first()) == null) {
147
            return response()->json(null, 404);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
148
        }
149
150
        UserFacade::getUser()->increment('credits', $voucher->credits);
151
        UserFacade::getUser()->increment('pixels', $voucher->points);
152
153
        $voucher->delete();
154
155
        return response()->json(null, 204);
156
    }
157
}
158