Passed
Push — master ( 057475...fb016c )
by Aly
03:10
created

ExpoController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 83
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A unsubscribe() 0 17 2
B subscribe() 0 35 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 5
    public function __construct(ExpoChannel $expoChannel)
25
    {
26 5
        $this->expoChannel = $expoChannel;
27 5
    }
28
29
    /**
30
     * Handles subscription endpoint for an expo token.
31
     *
32
     * @param Request $request
33
     *
34
     * @return \Illuminate\Http\JsonResponse
35
     */
36 3
    public function subscribe(Request $request)
37
    {
38 3
        $validator = Validator::make($request->all(), [
39 3
            'expo_token'    =>  'required|string',
40
        ]);
41
42 3
        if ($validator->fails()) {
43 1
            return JsonResponse::create([
44 1
                'status' => 'failed',
45
                'error' => [
46
                    'message' => 'Expo Token is required',
47
                ],
48 1
            ], 422);
49
        }
50
51 2
        $token = $request->get('expo_token');
52
53 2
        $interest = $this->expoChannel->interestName(Auth::user());
54
55
        try {
56 2
            $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 1
        return JsonResponse::create([
67 1
            'status'    =>  'succeeded',
68 1
            'expo_token' => $token,
69 1
        ], 200);
70
    }
71
72
    /**
73
     * Handles removing subscription endpoint for the authenticated interest.
74
     *
75
     * @return \Illuminate\Http\JsonResponse
76
     */
77 2
    public function unsubscribe()
78
    {
79 2
        $interest = $this->expoChannel->interestName(Auth::user());
80
81
        try {
82 2
            $deleted = $this->expoChannel->expo->unsubscribe($interest);
83 1
        } catch (\Exception $e) {
84 1
            return JsonResponse::create([
85 1
                'status'    => 'failed',
86
                'error'     =>  [
87 1
                    'message' => $e->getMessage(),
88
                ],
89 1
            ], 500);
90
        }
91
92 1
        return JsonResponse::create(['deleted' => $deleted]);
93
    }
94
}
95