Completed
Push — development ( b7987d...8c0e57 )
by Claudio
05:33
created

ShopController::getWall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Facades\Mail;
6
use App\Facades\User;
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
 * @package App\Http\Controllers
25
 */
26
class ShopController extends BaseController
27
{
28
    /**
29
     * List all Shop Countries
30
     *
31
     * @return JsonResponse
32
     */
33
    public function listCountries(): JsonResponse
34
    {
35
        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...
36
    }
37
38
    /**
39
     * Get the Shop Inventory of a Country
40
     *
41
     * @param string $countryCode
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, array(), JSON_UNESCAPED_SLASHES);
48
    }
49
50
    /**
51
     * Get User Purse
52
     *
53
     * @param Request $request
54
     * @return JsonResponse
55
     */
56
    public function getPurse(Request $request): JsonResponse
57
    {
58
        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...
59
    }
60
61
    /**
62
     * Proceed Payment Checkout
63
     *
64
     * @param string $paymentCategory
65
     * @param int $countryCode
66
     * @param int $shopItem
67
     * @param int $paymentMethod
68
     * @return RedirectResponse|Response|Redirector|ResponseFactory
69
     */
70
    public function proceed(string $paymentCategory, int $countryCode, int $shopItem, int $paymentMethod)
71
    {
72
        $paymentCheckout = PaymentCheckout::where('category', $paymentCategory)->where('country', $countryCode)
73
            ->where('item', $shopItem)->where('method', $paymentMethod)->first();
74
75
        if ((strtotime($paymentCheckout->generated_at) + 172800) < time())
76
            return response(view('habbo-web-payments.canceled-payment'), 400);
77
78
        return $paymentCheckout != null ? response(view('habbo-web-payments.proceed', ['payment' => $paymentCheckout]))
79
            : response(view('habbo-web-payments.failed-payment'), 400);
80
    }
81
82
    /**
83
     * Success Payment Checkout
84
     *
85
     * @TODO: Code Business Logic
86
     *
87
     * @param Request $request
88
     * @param string $paymentCategory
89
     * @param int $countryCode
90
     * @param int $shopItem
91
     * @param int $paymentMethod
92
     * @return RedirectResponse|Response|Redirector|ResponseFactory
93
     */
94
    public function success(Request $request, string $paymentCategory, int $countryCode, int $shopItem, int $paymentMethod)
95
    {
96
        $paymentCheckout = PaymentCheckout::where('category', $paymentCategory)->where('country', $countryCode)
97
            ->where('item', $shopItem)->where('method', $paymentMethod)->first();
98
99
        if ($paymentCheckout == null)
100
            return response(view('habbo-web-payments.canceled-payment'), 500);
101
102
        $purchaseItem = (new ShopHistory)->store($paymentMethod, $request->user()->uniqueId, $shopItem);
103
104
        Mail::send(['email' => $request->user()->email, 'purchaseId' => $purchaseItem->transactionId,
105
            'product' => ShopItem::find($shopItem), 'subject' => 'Purchase completed'
106
        ], 'habbo-web-mail.purchase-confirmation');
107
108
        $paymentCheckout->delete();
109
110
        return response(view('habbo-web-payments.success-payment', ['checkoutId' => $purchaseItem->transactionId]), 200);
111
    }
112
113
    /**
114
     * Get User Purchase History
115
     *
116
     * @TODO: User Purchase History will be coded on the Future
117
     * @TODO: All Purchases of the CMS are Manually, so will be difficult track.
118
     * @TODO: Probably Administrators will Manually Insert History Through HK
119
     *
120
     * @param Request $request
121
     * @return JsonResponse
122
     */
123
    public function getHistory(Request $request): JsonResponse
124
    {
125
        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...
126
    }
127
128
    /**
129
     * Redeem Voucher
130
     *
131
     * @TODO: Need to Test if really works
132
     *
133
     * @param Request $request
134
     * @return JsonResponse
135
     */
136
    public function redeem(Request $request): JsonResponse
137
    {
138
        if (($voucher = Voucher::where('code', $request->json()->get('voucherCode'))->first()) == null)
139
            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...
140
141
        User::getUser()->increment('credits', $voucher->credits);
142
        User::getUser()->increment('pixels', $voucher->points);
143
144
        $voucher->delete();
145
146
        return response()->json(null, 204);
147
    }
148
}
149