Completed
Push — master ( 306fc8...866e53 )
by Mr
03:35
created

Customers::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 24
ccs 0
cts 0
cp 0
crap 2
rs 10
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 1
     * @return \Resova\Interfaces\QueryInterface
24
     */
25 1
    public function create(CustomerCreate $customer): QueryInterface
26 1
    {
27
        $customer->setRequired([
28
            'first_name',
29
            'last_name',
30
            'email',
31
        ]);
32 1
33 1
        // Set HTTP params
34 1
        $this->type     = 'post';
35 1
        $this->endpoint = '/customers';
36
        $this->params   = $customer;
37 1
        $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 1
     * @return $this
49
     */
50 1
    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 1
54 1
        // Set HTTP params
55 1
        $this->type     = 'get';
56
        $this->endpoint = '/customers/' . $customer_id;
57 1
        $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 1
     * @return \Resova\Interfaces\QueryInterface
69
     */
70
    public function update(Customer $customer): QueryInterface
71 1
    {
72 1
        // Set HTTP params
73 1
        $this->type     = 'put';
74 1
        $this->endpoint = '/customers/' . $this->customer_id;
75
        $this->params   = $customer;
76 1
        $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