Subscription::createSubscription()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Zoho\Subscription\Api;
5
6
use Zoho\Subscription\Client\Client;
7
8
/**
9
 * Subscription.
10
 *
11
 * @author Elodie Nazaret <[email protected]>
12
 *
13
 * @link https://www.zoho.com/subscriptions/api/v1/#subscriptions
14
 */
15
class Subscription extends Client
16
{
17
    const STATUS_UNPAID = 'unpaid';
18
19
    /**
20
     * @param array $data
21
     *
22
     * @throws \Exception
23
     *
24
     * @return string
25
     */
26
    public function createSubscription(array $data)
27
    {
28
        $response = $this->sendRequest('POST', 'subscriptions', ['content-type' => 'application/json'], json_encode($data));
29
30
        return $this->processResponse($response);
31
    }
32
33
    /**
34
     * @param string $subscriptionId The subscription's id
35
     * @param array  $data
36
     *
37
     * @throws \Exception
38
     *
39
     * @return string
40
     */
41
    public function buyOneTimeAddonForASubscription(string $subscriptionId, array $data)
42
    {
43
        $response = $this->sendRequest('POST', sprintf('subscriptions/%s/buyonetimeaddon', $subscriptionId), [], json_encode($data));
44
45
        return $this->processResponse($response);
46
    }
47
48
    /**
49
     * @param string $subscriptionId The subscription's id
50
     * @param string $couponCode     The coupon's code
51
     *
52
     * @throws \Exception
53
     *
54
     * @return array
55
     */
56
    public function associateCouponToASubscription(string $subscriptionId, string $couponCode)
57
    {
58
        $response = $this->sendRequest('POST', sprintf('subscriptions/%s/coupons/%s', $subscriptionId, $couponCode));
59
60
        return $this->processResponse($response);
61
    }
62
63
    /**
64
     * @param string $subscriptionId The subscription's id
65
     *
66
     * @throws \Exception
67
     *
68
     * @return string
69
     */
70
    public function reactivateSubscription(string $subscriptionId)
71
    {
72
        $response = $this->sendRequest('POST', sprintf('subscriptions/%s/reactivate', $subscriptionId));
73
74
        return $this->processResponse($response);
75
    }
76
77
    /**
78
     * @param string $subscriptionId The subscription's id
79
     *
80
     * @throws \Exception
81
     *
82
     * @return array
83
     */
84 View Code Duplication
    public function getSubscription(string $subscriptionId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
85
    {
86
        $cacheKey = sprintf('zoho_subscription_%s', $subscriptionId);
87
        $hit = $this->getFromCache($cacheKey);
88
89
        if (false === $hit) {
90
            $response = $this->sendRequest('GET', sprintf('subscriptions/%s', $subscriptionId));
91
92
            $result = $this->processResponse($response);
93
94
            $subscription = $result['subscription'];
95
96
            $this->saveToCache($cacheKey, $subscription);
97
98
            return $subscription;
99
        }
100
101
        return $hit;
102
    }
103
104
    /**
105
     * @param string $customerId The customer's id
106
     *
107
     * @throws \Exception
108
     *
109
     * @return array
110
     */
111 View Code Duplication
    public function listSubscriptionsByCustomer(string $customerId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
112
    {
113
        $cacheKey = sprintf('zoho_subscriptions_%s', $customerId);
114
        $hit = $this->getFromCache($cacheKey);
115
116
        if (false === $hit) {
117
            $response = $this->sendRequest('GET', sprintf('subscriptions?customer_id=%s', $customerId));
118
119
            $result = $this->processResponse($response);
120
121
            $invoices = $result['subscriptions'];
122
123
            $this->saveToCache($cacheKey, $invoices);
124
125
            return $invoices;
126
        }
127
128
        return $hit;
129
    }
130
}
131