Completed
Push — development ( a928ed...ed05a0 )
by Claudio
02:29
created

ShopController::redeem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Country;
6
use App\Models\Purse;
7
use App\Models\ShopHistory;
8
use App\Models\ShopInventory;
9
use Illuminate\Http\JsonResponse;
10
use Illuminate\Http\RedirectResponse;
11
use Illuminate\Http\Request;
12
use Illuminate\Http\Response;
13
use Illuminate\Support\Facades\DB;
14
use Laravel\Lumen\Http\Redirector;
15
use Laravel\Lumen\Http\ResponseFactory;
16
use Laravel\Lumen\Routing\Controller as BaseController;
17
18
/**
19
 * Class ShopController
20
 * @package App\Http\Controllers
21
 */
22
class ShopController extends BaseController
23
{
24
    /**
25
     * List all Shop Countries
26
     *
27
     * @return JsonResponse
28
     */
29
    public function listCountries(): JsonResponse
30
    {
31
        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...
32
    }
33
34
    /**
35
     * Get the Shop Inventory of a Country
36
     *
37
     * @param string $countryCode
38
     * @return JsonResponse
39
     */
40
    public function getInventory(string $countryCode): JsonResponse
41
    {
42
        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...
43
            200, array(), JSON_UNESCAPED_SLASHES);
44
    }
45
46
    /**
47
     * Get User Purse
48
     *
49
     * @param Request $request
50
     * @return JsonResponse
51
     */
52
    public function getPurse(Request $request): JsonResponse
53
    {
54
        return response()->json(new Purse($request->user()->uniqueId));
0 ignored issues
show
Documentation introduced by
new \App\Models\Purse($request->user()->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...
55
    }
56
57
    /**
58
     * Proceed Payment Checkout
59
     *
60
     * @param string $paymentCategory
61
     * @param int $countryCode
62
     * @param int $shopItem
63
     * @param int $paymentMethod
64
     * @return RedirectResponse|Response|Redirector|ResponseFactory
65
     */
66
    public function proceed(string $paymentCategory, int $countryCode, int $shopItem, int $paymentMethod)
67
    {
68
        $paymentCheckout = DB::table('chocolatey_shop_payment_checkout')
69
            ->where('category', $paymentCategory)->where('country', $countryCode)
70
            ->where('item', $shopItem)->where('method', $paymentMethod)->first();
71
72
        return $paymentCheckout != null ? response(view('habbo-web-payments.proceed', ['payment' => $paymentCheckout]))
73
            : response(view('habbo-web-payments.failed-payment'), 400);
74
    }
75
76
    /**
77
     * Success Payment Checkout
78
     *
79
     * @TODO: Code Business Logic
80
     *
81
     * @param Request $request
82
     * @param string $paymentCategory
83
     * @param int $countryCode
84
     * @param int $shopItem
85
     * @param int $paymentMethod
86
     * @return RedirectResponse|Response|Redirector|ResponseFactory
87
     */
88
    public function success(Request $request, string $paymentCategory, int $countryCode, int $shopItem, int $paymentMethod)
89
    {
90
        $paymentCheckout = DB::table('chocolatey_shop_payment_checkout')
91
            ->where('category', $paymentCategory)->where('country', $countryCode)
92
            ->where('item', $shopItem)->where('method', $paymentMethod)->first();
93
94
        $purchaseItem = (new ShopHistory)->store($paymentMethod, $request->user()->uniqueId, $shopItem);
95
        $purchaseItem->save();
96
97
        (new MailController)->send([
98
            'mail' => $request->user()->email,
99
            'product' => DB::table('chocolatey_shop_items')->where('id', $shopItem)->first(),
100
            'purchaseId' => $purchaseItem->transactionId
101
        ], 'habbo-web-mail.purchase-confirmation');
102
103
        return $paymentCheckout != null ? response(view('habbo-web-payments.success-payment', [
104
            'checkoutId' => $purchaseItem->transactionId]), 200)
105
            : response(view('habbo-web-payments.canceled-payment'), 500);
106
    }
107
108
    /**
109
     * Get User Purchase History
110
     *
111
     * @TODO: User Purchase History will be coded on the Future
112
     * @TODO: All Purchases of the CMS are Manually, so will be difficult track.
113
     * @TODO: Probably Administrators will Manually Insert History Through HK
114
     *
115
     * @param Request $request
116
     * @return JsonResponse
117
     */
118
    public function getHistory(Request $request): JsonResponse
119
    {
120
        return response()->json(ShopHistory::where('user_id', $request->user()->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...
121
    }
122
123
    /**
124
     * Redeem Voucher
125
     *
126
     * @TODO: Need to Test if really works
127
     *
128
     * @param Request $request
129
     * @return JsonResponse
130
     */
131
    public function redeem(Request $request): JsonResponse
132
    {
133
        $voucher = DB::table('vouchers')->where('code', $request->json()->get('voucherCode'))->first();
134
135
        if ($voucher == null)
136
            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...
137
138
        DB::table('users')->where('id', $request->user()->uniqueId)->increment('credits', $voucher->credits);
139
        DB::table('users')->where('id', $request->user()->uniqueId)->increment('pixels', $voucher->points);
140
141
        return response()->json(null);
142
    }
143
144
    /**
145
     * Get Offer Wall
146
     *
147
     * @TODO: Need to Know how this really works!
148
     * @TODO: Ability of custom this shit
149
     *
150
     * @param Request $request
151
     * @return mixed
152
     */
153
    public function getWall(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...
154
    {
155
        return response()->json(['url' => "https://www.offertoro.com/ifr/show/2150/s-hhus-bf01d11c861e8785afe95065caa7f182/1308"]);
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...
156
    }
157
}
158