CustomerService   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 126
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 12
c 3
b 0
f 1
lcom 1
cbo 15
dl 18
loc 126
ccs 38
cts 38
cp 1
rs 9.1666

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getCustomers() 0 16 3
A getCustomer() 9 9 2
A getCustomerByExternalUid() 9 9 2
A updateCustomer() 0 6 1
A createCustomer() 0 6 1
A deleteCustomer() 0 7 1
A addCredits() 0 7 1
A createSecureLink() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 12
    public function getCustomers(Get\RequestData $requestData = null)
20
    {
21 12
        $request = new Get\Request($requestData);
22
23 12
        $apiResponse = $this->sendRequest($request, Get\ApiResponse::class);
24
25
        /** @var Get\Response $response */
26 12
        $response = $apiResponse->getResponse();
27 12
        foreach ($response->getCustomers() as $customer) {
28 12
            if ($customer->getBirthday() instanceof \DateTime) {
29 9
                $customer->getBirthday()->setTime(0, 0, 0);
30 9
            }
31 12
        }
32
33 12
        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 6 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 6
        $requestData = new Get\RequestData();
45 6
        $requestData->setCustomerId($customerId);
46
47 6
        $customers = $this->getCustomers($requestData)->getResponse()->getCustomers();
48
49 6
        return isset($customers[0]) ? $customers[0] : null;
50
    }
51
52
    /**
53
     * Get once customer by the external ID.
54
     *
55
     * @param string $customerExternalUid The external customer ID.
56
     * @return Customer|null
57
     */
58 3 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
    {
60 3
        $requestData = new Get\RequestData();
61 3
        $requestData->setCustomerExternalUid($customerExternalUid);
62
63 3
        $customers = $this->getCustomers($requestData)->getResponse()->getCustomers();
64
65 3
        return isset($customers[0]) ? $customers[0] : null;
66
    }
67
68
    /**
69
     * Update a customer.
70
     *
71
     * @param Update\RequestData $requestData The data to send.
72
     * @return Update\ApiResponse
73
     */
74 3
    public function updateCustomer(Update\RequestData $requestData)
75
    {
76 3
        $request = new Update\Request($requestData);
77
78 3
        return $this->sendRequest($request, Update\ApiResponse::class);
79
    }
80
81
    /**
82
     * Create a customer.
83
     *
84
     * @param Customer $customer The customer data to send.
85
     * @return Create\ApiResponse
86
     */
87 3
    public function createCustomer(Customer $customer)
88
    {
89 3
        $request = new Create\Request($customer);
90
91 3
        return $this->sendRequest($request, Create\ApiResponse::class);
92
    }
93
94
    /**
95
     * Delete a customer.
96
     *
97
     * @param integer $customerId The customer ID to delete.
98
     * @return Delete\ApiResponse
99
     */
100 3
    public function deleteCustomer($customerId)
101
    {
102 3
        $requestData = new Delete\RequestData($customerId);
103 3
        $request = new Delete\Request($requestData);
104
105 3
        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 3
    public function addCredits($customerId, $amount)
116
    {
117 3
        $requestData = new AddCredits\RequestData($customerId, $amount);
118 3
        $request = new AddCredits\Request($requestData);
119
120 3
        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 3
    public function createSecureLink($customerId)
130
    {
131 3
        $requestData = new CreateSecureLink\RequestData($customerId);
132 3
        $request = new CreateSecureLink\Request($requestData);
133
134 3
        return $this->sendRequest($request, CreateSecureLink\ApiResponse::class);
135
    }
136
}
137