SubscriptionActivated::handle()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 6.002

Importance

Changes 0
Metric Value
eloc 25
c 0
b 0
f 0
dl 0
loc 44
ccs 25
cts 26
cp 0.9615
rs 8.8977
cc 6
nc 12
nop 1
crap 6.002
1
<?php
2
/**
3
 * This file implements Subscription Activated.
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
use TwentyTwoDigital\CashierFastspring\SubscriptionPeriod;
17
18
/**
19
 * This class describes a subscription activated.
20
 *
21
 * It is planned to listen following fastspring events:
22
 *  - subscription.canceled
23
 *  - subscription.deactivated
24
 *  - subscription.payment.overdue
25
 * It updates related subscription event.
26
 *
27
 * IMPORTANT: This class handles expansion enabled webhooks.
28
 *
29
 * {@inheritdoc}
30
 */
31
class SubscriptionActivated extends Base
32
{
33
    /**
34
     * Create the event listener.
35
     *
36
     * @return null
37
     */
38 1
    public function __construct()
39
    {
40
        //
41 1
    }
42
43
    /**
44
     * Handle the event.
45
     *
46
     * @param \TwentyTwoDigital\CashierFastspring\Events\SubscriptionActivated $event
47
     *
48
     * @return void
49
     */
50 1
    public function handle(Events\SubscriptionActivated $event)
51
    {
52 1
        $data = $event->data;
53
54
        // first look for is there any subscription
55 1
        $user = $this->getUserByFastspringId($data['account']['id']);
56 1
        $subscriptionName = isset($data['tags']['name']) ? $data['tags']['name'] : 'default';
57
58 1
        $subscription = $user->subscription();
59
60 1
        if (!$subscription) {
61 1
            $subscription = new Subscription();
62 1
            $subscription->user_id = $user->id;
63 1
            $subscription->name = $subscriptionName;
64
        }
65
66
        // fill
67 1
        $subscription->fastspring_id = $data['id'];
68 1
        $subscription->plan = $data['product']['product'];
69 1
        $subscription->state = $data['state'];
70 1
        $subscription->currency = $data['currency'];
71 1
        $subscription->quantity = $data['quantity'];
72 1
        $subscription->interval_unit = $data['intervalUnit'];
73 1
        $subscription->interval_length = $data['intervalLength'];
74
75
        // save
76 1
        $subscription->save();
77
78
        // save instructions as periods
79
        // since this is the first time subscription is created we dont need to
80
        // check if it is already existed
81 1
        $instructions = $data['instructions'];
82
83 1
        foreach ($instructions as $instruction) {
84
            // if end or start date is null don't insert
85 1
            if (is_null($instruction['periodStartDateInSeconds']) || is_null($instruction['periodEndDateInSeconds'])) {
86
                continue;
87
            }
88
89 1
            $subscriptionPeriod = SubscriptionPeriod::firstOrCreate([
0 ignored issues
show
Unused Code introduced by
The assignment to $subscriptionPeriod is dead and can be removed.
Loading history...
90 1
                'subscription_id' => $subscription->id,
91 1
                'type'            => 'fastspring',
92 1
                'start_date'      => date('Y-m-d', $instruction['periodStartDateInSeconds']),
93 1
                'end_date'        => date('Y-m-d', $instruction['periodEndDateInSeconds']),
94
            ]);
95
        }
96 1
    }
97
}
98