Completed
Pull Request — master (#12)
by
unknown
03:33 queued 10s
created

ExpoController::subscribe()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 35

Duplication

Lines 8
Ratio 22.86 %

Code Coverage

Tests 19
CRAP Score 3

Importance

Changes 0
Metric Value
dl 8
loc 35
ccs 19
cts 19
cp 1
rs 9.36
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace NotificationChannels\ExpoPushNotifications\Http;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Routing\Controller;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Validator;
10
use NotificationChannels\ExpoPushNotifications\ExpoChannel;
11
12
class ExpoController extends Controller
13
{
14
    /**
15
     * @var ExpoChannel
16
     */
17
    private $expoChannel;
18
19
    /**
20
     * ExpoController constructor.
21
     *
22
     * @param ExpoChannel $expoChannel
23
     */
24 12
    public function __construct(ExpoChannel $expoChannel)
25
    {
26 12
        $this->expoChannel = $expoChannel;
27 12
    }
28
29
    /**
30
     * Handles subscription endpoint for an expo token.
31
     *
32
     * @param Request $request
33
     *
34
     * @return \Illuminate\Http\JsonResponse
35
     */
36 6
    public function subscribe(Request $request)
37
    {
38 6
        $validator = Validator::make($request->all(), [
39 6
            'expo_token'    =>  'required|string',
40
        ]);
41
42 6 View Code Duplication
        if ($validator->fails()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43 2
            return JsonResponse::create([
44 2
                'status' => 'failed',
45
                'error' => [
46
                    'message' => 'Expo Token is required',
47
                ],
48 2
            ], 422);
49
        }
50
51 4
        $token = $request->get('expo_token');
52
53 4
        $interest = $this->expoChannel->interestName(Auth::user());
54
55
        try {
56 4
            $this->expoChannel->expo->subscribe($interest, $token);
57 2
        } catch (\Exception $e) {
58 2
            return JsonResponse::create([
59 2
                'status'    => 'failed',
60
                'error'     =>  [
61 2
                    'message' => $e->getMessage(),
62
                ],
63 2
            ], 500);
64
        }
65
66 2
        return JsonResponse::create([
67 2
            'status'    =>  'succeeded',
68 2
            'expo_token' => $token,
69 2
        ], 200);
70
    }
71
72
    /**
73
     * Handles removing subscription endpoint for the authenticated interest.
74
     *
75
     * @return \Illuminate\Http\JsonResponse
76
     */
77 6
    public function unsubscribe(Request $request)
78
    {
79 6
        $interest = $this->expoChannel->interestName(Auth::user());
80
81 6
        $validator = Validator::make($request->all(), [
82 6
            'expo_token'    =>  'sometimes|string',
83
        ]);
84
85 6 View Code Duplication
        if ($validator->fails()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
            return JsonResponse::create([
87
                'status' => 'failed',
88
                'error' => [
89
                    'message' => 'Expo Token is invalid',
90
                ],
91
            ], 422);
92
        }
93
94 6
        $token = $request->get('expo_token') ?: null;
95
96
        try {
97 6
            $deleted = $this->expoChannel->expo->unsubscribe($interest, $token);
0 ignored issues
show
Unused Code introduced by
The call to Expo::unsubscribe() has too many arguments starting with $token.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
98 2
        } catch (\Exception $e) {
99 2
            return JsonResponse::create([
100 2
                'status'    => 'failed',
101
                'error'     =>  [
102 2
                    'message' => $e->getMessage(),
103
                ],
104 2
            ], 500);
105
        }
106
107 4
        return JsonResponse::create(['deleted' => $deleted]);
108
    }
109
}
110