Completed
Pull Request — master (#11)
by Cesar
01:33
created

CustomerService::listCustomers()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 4
nc 3
nop 0
1
<?php
2
3
namespace App\Service\Braintree;
4
5
use Braintree\Result\Error;
6
use Braintree\Result\Successful;
7
use Exception;
8
9
/**
10
 * Class CustomerService
11
 * @package App\Service\Braintree
12
 */
13
class CustomerService extends AbstractBraintreeService
14
{
15
    /**
16
     * @return array
17
     */
18
    public function listCustomers()
19
    {
20
        $customers = [];
21
        $customersList = $this->gateway->customer()->search([
22
23
        ]);
24
        foreach ($customersList as $customer) {
25
            $customer = [
26
                'id' => $customer->id,
27
                'full_name' => $customer->firstName . ' ' . $customer->lastName,
28
                'email' => $customer->email,
29
                'available_payment_methods' => count($customer->paymentMethods)
30
            ];
31
32
            if ($customer['full_name'] && $customer['email']) {
33
                array_unshift($customers, $customer);
34
            }
35
        }
36
        return $customers;
37
    }
38
39
    /**
40
     * @param array $customerData
41
     * @return Error|Successful|Exception
42
     */
43
    public function createCustomer(array $customerData)
44
    {
45
        $requiredFieldKeys = ['firstName', 'lastName', 'email', 'company', 'website', 'phone'];
46
        foreach ($requiredFieldKeys as $key) {
47
            if (!array_key_exists($key, $customerData) || empty($customerData[$key])) {
48
                return new Exception('Unable to create customer, missing key: ' . $key);
49
            }
50
        }
51
        return $this->gateway->customer()->create($customerData);
52
    }
53
}
54