Passed
Push — main ( cf6476...352566 )
by Chris
15:09
created

ensureSubscriptionIsConfirmed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Meema\MediaConverter\Http\Controllers;
4
5
use Aws\Sns\Message;
6
use Illuminate\Routing\Controller;
7
use Illuminate\Support\Facades\Http;
8
use Meema\MediaConverter\Events\ConversionHasCompleted;
9
use Meema\MediaConverter\Events\ConversionHasError;
10
use Meema\MediaConverter\Events\ConversionHasInputInformation;
11
use Meema\MediaConverter\Events\ConversionHasNewWarning;
12
use Meema\MediaConverter\Events\ConversionHasStatusUpdate;
13
use Meema\MediaConverter\Events\ConversionIsProgressing;
14
use Meema\MediaConverter\Events\ConversionQueueHop;
15
16
class IncomingWebhookController extends Controller
17
{
18
    public function __construct()
19
    {
20
        $this->middleware('verify-signature');
21
    }
22
23
    /**
24
     * @throws \Exception
25
     */
26
    public function __invoke()
27
    {
28
        try {
29
            $message = $this->ensureSubscriptionIsConfirmed();
30
31
            $detail = $message['detail'];
32
            $status = $detail['status'];
33
34
            $this->fireEventFor($status, $message);
35
        } catch (\Exception $e) {
36
            throw new \Exception($e);
37
        }
38
    }
39
40
    /**
41
     * @param $status
42
     * @param $message
43
     * @throws \Exception
44
     */
45
    public function fireEventFor($status, $message)
46
    {
47
        switch ($status) {
48
            case 'PROGRESSING':
49
                event(new ConversionIsProgressing($message));
50
                break;
51
            case 'INPUT_INFORMATION':
52
                event(new ConversionHasInputInformation($message));
53
                break;
54
            case 'COMPLETE':
55
                event(new ConversionHasCompleted($message));
56
                break;
57
            case 'STATUS_UPDATE':
58
                event(new ConversionHasStatusUpdate($message));
59
                break;
60
            case 'NEW_WARNING':
61
                event(new ConversionHasNewWarning($message));
62
                break;
63
            case 'QUEUE_HOP':
64
                event(new ConversionQueueHop($message));
65
                break;
66
            case 'ERROR':
67
                event(new ConversionHasError($message));
68
                break;
69
            default:
70
                throw new \Exception();
71
        }
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    public function ensureSubscriptionIsConfirmed(): array
78
    {
79
        $message = Message::fromRawPostData()->toArray();
80
81
        if (array_key_exists('SubscribeURL', $message)) {
82
            Http::get($message['SubscribeURL']);
83
        }
84
85
        return $message;
86
    }
87
}
88