Passed
Push — main ( cee2a9...63dbdd )
by Richard
04:42
created

RedeemController::redeem()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 45
rs 8.5226
c 0
b 0
f 0
cc 7
nc 8
nop 1
1
<?php
2
3
namespace Furic\RedeemCodes\Http\Controllers;
4
5
use Furic\RedeemCodes\Models\RedeemCode;
6
use Furic\RedeemCodes\Models\RedeemCodeHistory;
7
use App\Http\Controllers\Controller;
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Illuminate\Http\Request;
9
use Validator;
10
11
// The controller for redeem code api calls.
12
class RedeemController extends Controller
13
{
14
15
    public function redeem($code)
16
    {
17
        $validator = Validator::make(['code' => $code], ['code' => 'exists:redeem_codes,code']);
18
        if ($validator->fails()) {
19
            return response([
20
                'error' => 'No redeem code found.'
21
            ], 400);
22
        }
23
24
        $redeemCode = RedeemCode::findByCode($code);
25
26
        if ($redeemCode->redeemed != false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
27
            return response([
28
                'error' => 'Redeem code has already redeemed.'
29
            ], 400);
30
        }
31
32
        // Check event valid date
33
        $event = $redeemCode->event;
34
        if ($event != null) {
35
            $validator = Validator::make($event->toArray(), ['start_at' => 'before:tomorrow']);
36
            if ($validator->fails()) {
37
                return response([
38
                    'error' => 'Redeem code cannot be used yet.'
39
                ], 400);
40
            }
41
            $validator = Validator::make($event->toArray(), ['end_at' => 'after:yesterday']);
42
            if ($validator->fails()) {
43
                return response([
44
                    'error' => 'Redeem code has expired.'
45
                ], 400);
46
            }
47
        }
48
49
        if ($redeemCode->reusable == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
50
            $redeemCode->setRedeemed();
51
        }
52
53
        // Add a redeem code history
54
        $data['redeem_code_id'] = $redeemCode->id;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
55
        $data['ip'] = filter_input(INPUT_SERVER, "REMOTE_ADDR");
56
        $data['agent'] = filter_input(INPUT_SERVER, "HTTP_USER_AGENT");
57
        RedeemCodeHistory::create($data);
58
59
        return $redeemCode;
60
    }
61
62
}
63