Completed
Push — master ( 5bd201...5ab7f0 )
by Dragos
14:22
created

CustomerService::getCustomerByExternalUid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 9
loc 9
ccs 1
cts 1
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Speicher210\Fastbill\Api\Service\Customer;
4
5
use Speicher210\Fastbill\Api\AbstractService;
6
use Speicher210\Fastbill\Api\Model\Customer;
7
8
/**
9
 * Service for customers.
10
 */
11
class CustomerService extends AbstractService
12
{
13
    /**
14
     * Get the customers.
15
     *
16
     * @param Get\RequestData|null $requestData The request data.
17
     * @return Get\ApiResponse
18
     */
19 6
    public function getCustomers(Get\RequestData $requestData = null)
20
    {
21 6
        $request = new Get\Request($requestData);
22
23 6
        $apiResponse = $this->sendRequest($request, Get\ApiResponse::class);
24
25
        /** @var Get\Response $response */
26 6
        $response = $apiResponse->getResponse();
27 6
        foreach ($response->getCustomers() as $customer) {
28 6
            if ($customer->getBirthday() instanceof \DateTime) {
29 3
                $customer->getBirthday()->setTime(0, 0, 0);
30 3
            }
31 6
        }
32
33 6
        return $apiResponse;
34
    }
35
36
    /**
37
     * Get one customer by ID.
38
     *
39
     * @param integer $customerId The customer ID.
40
     * @return Customer|null
41
     */
42 3 View Code Duplication
    public function getCustomer($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...
43
    {
44 3
        $requestData = new Get\RequestData();
45
        $requestData->setCustomerId($customerId);
46 3
47
        $customers = $this->getCustomers($requestData)->getResponse()->getCustomers();
48
49
        return isset($customers[0]) ? $customers[0] : null;
50
    }
51
52
    /**
53
     * Get once customer by the external ID.
54
     *
55 3
     * @param string $customerExternalUid The external customer ID.
56
     * @return Customer|null
57 3
     */
58 View Code Duplication
    public function getCustomerByExternalUid($customerExternalUid)
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...
59 3
    {
60
        $requestData = new Get\RequestData();
61
        $requestData->setCustomerExternalUid($customerExternalUid);
62
63
        $customers = $this->getCustomers($requestData)->getResponse()->getCustomers();
64
65
        return isset($customers[0]) ? $customers[0] : null;
66
    }
67
68 3
    /**
69
     * Update a customer.
70 3
     *
71 3
     * @param Update\RequestData $requestData The data to send.
72
     * @return Update\ApiResponse
73 3
     */
74
    public function updateCustomer(Update\RequestData $requestData)
75
    {
76
        $request = new Update\Request($requestData);
77
78
        return $this->sendRequest($request, Update\ApiResponse::class);
79
    }
80
81
    /**
82
     * Create a customer.
83 3
     *
84
     * @param Customer $customer The customer data to send.
85 3
     * @return Create\ApiResponse
86 3
     */
87
    public function createCustomer(Customer $customer)
88 3
    {
89
        $request = new Create\Request($customer);
90
91
        return $this->sendRequest($request, Create\ApiResponse::class);
92
    }
93
94
    /**
95
     * Delete a customer.
96
     *
97 3
     * @param integer $customerId The customer ID to delete.
98
     * @return Delete\ApiResponse
99 3
     */
100 3
    public function deleteCustomer($customerId)
101
    {
102 3
        $requestData = new Delete\RequestData($customerId);
103
        $request = new Delete\Request($requestData);
104
105
        return $this->sendRequest($request, Delete\ApiResponse::class);
106
    }
107
108
    /**
109
     * Add credits to a customer.
110
     *
111
     * @param integer $customerId The customer ID.
112
     * @param float $amount The amount of credit to add.
113
     * @return AddCredits\ApiResponse
114
     */
115
    public function addCredits($customerId, $amount)
116
    {
117
        $requestData = new AddCredits\RequestData($customerId, $amount);
118
        $request = new AddCredits\Request($requestData);
119
120
        return $this->sendRequest($request, AddCredits\ApiResponse::class);
121
    }
122
123
    /**
124
     * Add credits to a customer.
125
     *
126
     * @param integer $customerId The customer ID.
127
     * @return CreateSecureLink\ApiResponse
128
     */
129
    public function createSecureLink($customerId)
130
    {
131
        $requestData = new CreateSecureLink\RequestData($customerId);
132
        $request = new CreateSecureLink\Request($requestData);
133
134
        return $this->sendRequest($request, CreateSecureLink\ApiResponse::class);
135
    }
136
}
137