Customer::getListCustomersByEmail()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 19
loc 19
rs 9.6333
cc 2
nc 2
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
 * @author Hang Pham <[email protected]>
10
 * @author Tristan Bessoussa <[email protected]>
11
 *
12
 * @link   https://www.zoho.com/subscriptions/api/v1/#customers
13
 */
14
class Customer extends Client
15
{
16
    /**
17
     * @param string $customerEmail
18
     *
19
     * @return array
20
     */
21 View Code Duplication
    public function getListCustomersByEmail(string $customerEmail): array
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...
22
    {
23
        $cacheKey = sprintf('zoho_customer_%s', md5($customerEmail));
24
        $hit = $this->getFromCache($cacheKey);
25
26
        if (false === $hit) {
27
            $response = $this->sendRequest('GET', sprintf('customers?email=%s', $customerEmail));
28
29
            $result = $this->processResponse($response);
30
31
            $customers = $result['customers'];
32
33
            $this->saveToCache($cacheKey, $customers);
34
35
            return $customers;
36
        }
37
38
        return $hit;
39
    }
40
41
    /**
42
     * @param string $customerEmail
43
     *
44
     * @return array
45
     */
46
    public function getCustomerByEmail(string $customerEmail): array
47
    {
48
        $customers = $this->getListCustomersByEmail($customerEmail);
49
50
        if (count($customers) === 0) {
51
            throw new \LogicException(sprintf('customer with email %s not found', $customerEmail));
52
        }
53
54
        return $this->getCustomerById($customers[0]['customer_id']);
55
    }
56
57
    /**
58
     * @param string $customerId The customer's id
59
     *
60
     * @throws \Exception
61
     *
62
     * @return array
63
     */
64 View Code Duplication
    public function getCustomerById(string $customerId): array
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...
65
    {
66
        $cacheKey = sprintf('zoho_customer_%s', $customerId);
67
        $hit = $this->getFromCache($cacheKey);
68
69
        if (false === $hit) {
70
            $response = $this->sendRequest('GET', sprintf('customers/%s', $customerId));
71
            $result = $this->processResponse($response);
72
73
            $customer = $result['customer'];
74
75
            $this->saveToCache($cacheKey, $customer);
76
77
            return $customer;
78
        }
79
80
        return $hit;
81
    }
82
83
    /**
84
     * @param string $customerId The customer's id
85
     * @param array  $data
86
     *
87
     * @throws \Exception
88
     *
89
     * @return array|bool
90
     */
91 View Code Duplication
    public function updateCustomer(string $customerId, array $data)
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...
92
    {
93
        $response = $this->sendRequest('PUT', sprintf('customers/%s', $customerId), ['content-type' => 'application/json'], json_encode($data));
94
95
        $result = $this->processResponse($response);
96
97
        if ($result['code'] == '0') {
98
            $customer = $result['customer'];
99
100
            $this->deleteCustomerCache($customer);
101
102
            return $customer;
103
        } else {
104
            return false;
105
        }
106
    }
107
108
    /**
109
     * @param array $customer
110
     */
111
    private function deleteCustomerCache(array $customer)
112
    {
113
        $cacheKey = sprintf('zoho_customer_%s', $customer['customer_id']);
114
        $this->deleteCacheByKey($cacheKey);
115
116
        $cacheKey = sprintf('zoho_customer_%s', md5($customer['email']));
117
        $this->deleteCacheByKey($cacheKey);
118
    }
119
120
    /**
121
     * @param array $data
122
     *
123
     * @throws \Exception
124
     *
125
     * @return array|bool
126
     */
127 View Code Duplication
    public function createCustomer(array $data)
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...
128
    {
129
        $response = $this->sendRequest('POST', 'customers', ['content-type' => 'application/json'], json_encode($data));
130
131
        $result = $this->processResponse($response);
132
133
        if ($result['code'] == '0') {
134
            $customer = $result['customer'];
135
136
            return $customer;
137
        } else {
138
            return false;
139
        }
140
    }
141
}
142