Completed
Push — master ( bad662...5b32ef )
by Aly
11:55
created

ExpoController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 10
    public function __construct(ExpoChannel $expoChannel)
25
    {
26 10
        $this->expoChannel = $expoChannel;
27 10
    }
28
29
    /**
30
     * Handles subscription endpoint for an expo token.
31
     *
32
     * @param Request $request
33
     *
34
     * @return \Illuminate\Http\JsonResponse
35
     */
36 5
    public function subscribe(Request $request)
37
    {
38 5
        $validator = Validator::make($request->all(), [
39 5
            'expo_token'    =>  'required|string',
40
        ]);
41
42 5 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 3
        $token = $request->get('expo_token');
52
53 3
        $interest = $this->expoChannel->interestName(Auth::user());
54
55
        try {
56 3
            $this->expoChannel->expo->subscribe($interest, $token);
57 1
        } catch (\Exception $e) {
58 1
            return JsonResponse::create([
59 1
                'status'    => 'failed',
60
                'error'     =>  [
61 1
                    'message' => $e->getMessage(),
62
                ],
63 1
            ], 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
     * @param Request $request
76
     *
77
     * @return JsonResponse
78
     */
79 5
    public function unsubscribe(Request $request)
80
    {
81 5
        $interest = $this->expoChannel->interestName(Auth::user());
82
83 5
        $validator = Validator::make($request->all(), [
84 5
            'expo_token'    =>  'sometimes|string',
85
        ]);
86
87 5 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...
88
            return JsonResponse::create([
89
                'status' => 'failed',
90
                'error' => [
91
                    'message' => 'Expo Token is invalid',
92
                ],
93
            ], 422);
94
        }
95
96 5
        $token = $request->get('expo_token') ?: null;
97
98
        try {
99 5
            $deleted = $this->expoChannel->expo->unsubscribe($interest, $token);
100 1
        } catch (\Exception $e) {
101 1
            return JsonResponse::create([
102 1
                'status'    => 'failed',
103
                'error'     =>  [
104 1
                    'message' => $e->getMessage(),
105
                ],
106 1
            ], 500);
107
        }
108
109 4
        return JsonResponse::create(['deleted' => $deleted]);
110
    }
111
}
112