SubscriptionStateChanged::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file implements Subscription State Changed.
4
 *
5
 * @author    Bilal Gultekin <[email protected]>
6
 * @author    Justin Hartman <[email protected]>
7
 * @copyright 2019 22 Digital
8
 * @license   MIT
9
 * @since     v0.1
10
 */
11
12
namespace TwentyTwoDigital\CashierFastspring\Listeners;
13
14
use TwentyTwoDigital\CashierFastspring\Events;
15
use TwentyTwoDigital\CashierFastspring\Subscription;
16
17
/**
18
 * This class is a listener for subscription state change events.
19
 * It is planned to listen following fastspring events:
20
 *  - subscription.canceled
21
 *  - subscription.payment.overdue
22
 * It updates related subscription event.
23
 *
24
 * IMPORTANT: This class handles expansion enabled webhooks.
25
 *
26
 * {@inheritdoc}
27
 */
28
class SubscriptionStateChanged extends Base
29
{
30
    /**
31
     * Create the event listener.
32
     *
33
     * @return null
34
     */
35 1
    public function __construct()
36
    {
37
        //
38 1
    }
39
40
    /**
41
     * Handle the event.
42
     *
43
     * @param \TwentyTwoDigital\CashierFastspring\Events\Base $event
44
     *
45
     * @return void
46
     */
47 1
    public function handle(Events\Base $event)
48
    {
49 1
        $data = $event->data;
50
51
        // create
52 1
        $subscription = Subscription::where('fastspring_id', $data['id'])->firstOrFail();
53
54
        // fill
55 1
        $subscription->user_id = $this->getUserByFastspringId($data['account']['id'])->id;
56 1
        $subscription->plan = $data['product']['product'];
57 1
        $subscription->state = $data['state'];
58 1
        $subscription->currency = $data['currency'];
59 1
        $subscription->quantity = $data['quantity'];
60
61
        // save
62 1
        $subscription->save();
63 1
    }
64
}
65