Customers   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 28
c 2
b 0
f 1
dl 0
loc 106
ccs 0
cts 27
cp 0
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A search() 0 24 1
A create() 0 15 1
A update() 0 9 1
A __invoke() 0 10 1
1
<?php
2
3
namespace Resova\Endpoints;
4
5
use Resova\Client;
6
use Resova\Interfaces\QueryInterface;
7
use Resova\Models\Customer;
8
use Resova\Models\CustomerCreate;
9
10
class Customers extends Client
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $namespace = __CLASS__;
16
17
    /**
18
     * Create a customer
19
     * Creates a new customer object.
20
     *
21
     * @param CustomerCreate $customer
22
     *
23
     * @return \Resova\Interfaces\QueryInterface
24
     */
25
    public function create(CustomerCreate $customer): QueryInterface
26
    {
27
        $customer->setRequired([
28
            'first_name',
29
            'last_name',
30
            'email',
31
        ]);
32
33
        // Set HTTP params
34
        $this->type     = 'post';
35
        $this->endpoint = '/customers';
36
        $this->params   = $customer;
37
        $this->response = Customer::class;
0 ignored issues
show
Bug Best Practice introduced by
The property response does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
39
        return $this;
40
    }
41
42
    /**
43
     * Retrieve a customer
44
     * Retrieves the details of a customer. Provide the unique id for the customer.
45
     *
46
     * @param int $customer_id The customer id
47
     *
48
     * @return $this
49
     */
50
    public function __invoke(int $customer_id): self
51
    {
52
        $this->customer_id = $customer_id;
0 ignored issues
show
Bug Best Practice introduced by
The property customer_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
53
54
        // Set HTTP params
55
        $this->type     = 'get';
56
        $this->endpoint = '/customers/' . $customer_id;
57
        $this->response = Customer::class;
0 ignored issues
show
Bug Best Practice introduced by
The property response does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58
59
        return $this;
60
    }
61
62
    /**
63
     * Create a customer
64
     * Creates a new customer object.
65
     *
66
     * @param Customer $customer
67
     *
68
     * @return \Resova\Interfaces\QueryInterface
69
     */
70
    public function update(Customer $customer): QueryInterface
71
    {
72
        // Set HTTP params
73
        $this->type     = 'put';
74
        $this->endpoint = '/customers/' . $this->customer_id;
75
        $this->params   = $customer;
76
        $this->response = Customer::class;
0 ignored issues
show
Bug Best Practice introduced by
The property response does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
77
78
        return $this;
79
    }
80
81
    /**
82
     * Try to find customer by string
83
     *
84
     * INFO: It work only in frontend mode
85
     *
86
     * @param string $string
87
     * @param int    $page
88
     * @param int    $limit
89
     *
90
     * @return \Resova\Interfaces\QueryInterface
91
     */
92
    public function search(string $string, int $page = 1, int $limit = 500): QueryInterface
93
    {
94
        $params = [
95
            'page'   => $page,
96
            'limit'  => $limit,
97
            'search' => $string,
98
        ];
99
100
        // Set HTTP params
101
        $this->type     = 'get';
102
        $this->endpoint = '/customers' . '?' . http_build_query($params);
103
104
        // TODO: array of customers like below
105
        // current_page: 1
106
        // data: [{id: 1, reference: "some-hash", ip: "123.123.123.123", first_name: "Test",…}] // TODO: here array of Customer objects
107
        // last_page: 1
108
        // overall_total: 1
109
        // per_page: 500
110
        // total: 1
111
        // visible: 1
112
113
        // $this->response = Customer::class;
114
115
        return $this;
116
    }
117
}
118