Completed
Push — development ( a387f8...6053d7 )
by Claudio
02:31
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\ShopInventory;
8
use Illuminate\Http\JsonResponse;
9
use Illuminate\Http\RedirectResponse;
10
use Illuminate\Http\Request;
11
use Illuminate\Http\Response;
12
use Illuminate\Support\Facades\DB;
13
use Laravel\Lumen\Http\Redirector;
14
use Laravel\Lumen\Http\ResponseFactory;
15
use Laravel\Lumen\Routing\Controller as BaseController;
16
17
/**
18
 * Class ShopController
19
 * @package App\Http\Controllers
20
 */
21
class ShopController extends BaseController
22
{
23
    /**
24
     * List all Shop Countries
25
     *
26
     * @return JsonResponse
27
     */
28
    public function listCountries(): JsonResponse
29
    {
30
        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...
31
    }
32
33
    /**
34
     * Get the Shop Inventory of a Country
35
     *
36
     * @param string $countryCode
37
     * @return JsonResponse
38
     */
39
    public function getInventory(string $countryCode): JsonResponse
40
    {
41
        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...
42
            200, array(), JSON_UNESCAPED_SLASHES);
43
    }
44
45
    /**
46
     * Get User Purse
47
     *
48
     * @param Request $request
49
     * @return JsonResponse
50
     */
51
    public function getPurse(Request $request): JsonResponse
52
    {
53
        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...
54
    }
55
56
    /**
57
     * Proceed Payment Checkout
58
     *
59
     * @param string $paymentCategory
60
     * @param string $countryCode
61
     * @param int $shopItem
62
     * @param string $paymentMethod
63
     * @return RedirectResponse|Response|Redirector|ResponseFactory
64
     */
65
    public function proceed(string $paymentCategory, string $countryCode, int $shopItem, string $paymentMethod)
66
    {
67
        $paymentCheckout = DB::table('chocolatey_shop_payment_checkout')
68
            ->where('category', $paymentCategory)->where('country', $countryCode)
69
            ->where('item', $shopItem)->where('method', $paymentMethod)->first();
70
71
        return $paymentCheckout != null ? redirect($paymentCheckout->redirect)
72
            : response(view('failed-payment'), 400);
73
    }
74
75
    /**
76
     * Get User Purchase History
77
     *
78
     * @TODO: User Purchase History will be coded on the Future
79
     * @TODO: All Purchases of the CMS are Manually, so will be difficult track.
80
     * @TODO: Probably Administrators will Manually Insert History Through HK
81
     *
82
     * @param Request $request
83
     * @return JsonResponse
84
     */
85
    public function getHistory(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...
86
    {
87
        return response()->json([]);
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...
88
    }
89
90
    /**
91
     * Redeem Voucher
92
     *
93
     * @TODO: Need to Test if really works
94
     *
95
     * @param Request $request
96
     * @return JsonResponse
97
     */
98
    public function redeem(Request $request): JsonResponse
99
    {
100
        $voucher = DB::table('vouchers')->where('code', $request->json()->get('voucherCode'))->first();
101
102
        if ($voucher == null)
103
            return response()->json('', 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...
104
105
        DB::table('users')->where('id', $request->user()->uniqueId)->increment('credits', $voucher->credits);
106
        DB::table('users')->where('id', $request->user()->uniqueId)->increment('pixels', $voucher->points);
107
108
        return response()->json();
109
    }
110
}
111