CustomerClient::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Dynamic\Foxy\SingleSignOn\Client;
4
5
use Dynamic\Foxy\API\Client\APIClient;
6
use Foxy\FoxyClient\FoxyClient;
7
use SilverStripe\Core\Config\Configurable;
8
use SilverStripe\Core\Extensible;
9
use SilverStripe\Core\Injector\Injectable;
10
use SilverStripe\Security\Member;
11
12
/**
13
 * Class CustomerClient
14
 * @package Dynamic\FoxyStripe\API\Client
15
 */
16
class CustomerClient extends APIClient
17
{
18
    use Configurable;
19
    use Extensible;
20
    use Injectable;
21
22
    /**
23
     * @var bool
24
     */
25
    private static $foxy_sso_enabled = true;
26
27
    /**
28
     * @var array
29
     */
30
    private static $customer_map = [
31
        'Customer_ID' => 'id',
32
        'FirstName' => 'first_name',
33
        'Surname' => 'last_name',
34
        'Email' => 'email',
35
        'Salt' => 'password_salt',
36
        'Password' => 'password_hash',
37
    ];
38
39
    /**
40
     * @var string
41
     */
42
    //private static $foxy_password_hash_type = 'bcrypt';
43
    private static $foxy_password_hash_type = 'sha1';
44
45
    /**
46
     * @var Member
47
     */
48
    private $customer;
49
50
    /**
51
     * CustomerClient constructor.
52
     * @param Member $customer
53
     */
54
    public function __construct(Member $customer)
55
    {
56
        parent::__construct();
57
58
        $this->setCustomer($customer);
59
    }
60
61
    /**
62
     * @param $customer
63
     * @return $this
64
     */
65
    public function setCustomer($customer)
66
    {
67
        $this->customer = $customer;
68
69
        return $this;
70
    }
71
72
    /**
73
     * @return Member
74
     */
75
    public function getCustomer()
76
    {
77
        return $this->customer;
78
    }
79
80
    /**
81
     * @param bool $single
82
     * @return string
83
     */
84
    private function getAPIURI($single = false)
85
    {
86
        $parts = [FoxyClient::PRODUCTION_API_HOME, 'customers'];
87
88
        if ($single) {
89
            $parts[] = $this->getCustomer()->Customer_ID;
90
        }
91
92
        return implode('/', $parts);
93
    }
94
95
    private function getNewCustomerAPIURI()
96
    {
97
        return implode('/', [$this->getCurrentStore(), 'customers']);
98
    }
99
100
    /**
101
     * @return mixed
102
     */
103
    public function putCustomer()
104
    {
105
        $client = $this->getClient();
106
107
        if (!$this->getCustomer()->Customer_ID) {
108
            $response = $client->post($this->getNewCustomerAPIURI(), $this->getSendData());
109
        } else {
110
            $response = $client->patch($this->getAPIURI(true), $this->getSendData());
111
        }
112
113
        return $response;
114
    }
115
116
    /**
117
     * @return mixed
118
     */
119
    public function fetchCustomer()
120
    {
121
        $client = $this->getClient();
122
123
        $result = $client->get($this->getAPIURI(true));
124
125
        return $result;
126
    }
127
128
    /**
129
     * @return mixed
130
     */
131
    public function fetchCustomers()
132
    {
133
        $client = $this->getClient();
134
135
        $result = $client->get($this->getAPIURI(true));
136
137
        return $result;
138
    }
139
140
    /**
141
     *
142
     */
143
    public function deleteCustomer()
144
    {
145
    }
146
147
    /**
148
     * @return array
149
     */
150
    public function getSendData()
151
    {
152
        $data = [];
153
154
        if ($customer = $this->getCustomer()) {
155
            foreach ($this->config()->get('customer_map') as $localField => $remoteField) {
156
                if ($customer->{$localField}) {
157
                    $data[$remoteField] = $customer->{$localField};
158
                }
159
            }
160
        }
161
162
        return $data;
163
    }
164
}
165