WooCommerceTrait::previous()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Codexshaper\WooCommerce\Traits;
4
5
trait WooCommerceTrait
6
{
7
    /**
8
     * GET method.
9
     * Retrieve data.
10
     *
11
     * @param string $endpoint API endpoint.
12
     * @param array  $options
13
     *
14
     * @return array
15
     */
16 View Code Duplication
    public function all($endpoint = '', $options = [])
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...
17
    {
18
        try {
19
            self::__construct();
20
21
            return $this->client->get($endpoint, $options);
0 ignored issues
show
Bug introduced by
The property client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
        } catch (\Exception $e) {
23
            throw new \Exception($e->getMessage(), 1);
24
        }
25
    }
26
27
    /**
28
     * GET method.
29
     * Retrieve Single data.
30
     *
31
     * @param string $endpoint API endpoint.
32
     * @param array  $options
33
     *
34
     * @return array
35
     */
36 View Code Duplication
    public function find($endpoint = '', $options = [])
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...
37
    {
38
        try {
39
            self::__construct();
40
41
            return $this->client->get($endpoint, $options);
42
        } catch (\Exception $e) {
43
            throw new \Exception($e->getMessage(), 1);
44
        }
45
    }
46
47
    /**
48
     * POST method.
49
     * Insert data.
50
     *
51
     * @param string $endpoint API endpoint.
52
     * @param array  $data
53
     *
54
     * @return array
55
     */
56 View Code Duplication
    public function create($endpoint, $data)
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...
57
    {
58
        try {
59
            self::__construct();
60
61
            return $this->client->post($endpoint, $data);
62
        } catch (\Exception $e) {
63
            throw new \Exception($e->getMessage(), 1);
64
        }
65
    }
66
67
    /**
68
     * PUT method.
69
     * Update data.
70
     *
71
     * @param string $endpoint API endpoint.
72
     * @param array  $data
73
     *
74
     * @return array
75
     */
76 View Code Duplication
    public function update($endpoint, $data)
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...
77
    {
78
        try {
79
            self::__construct();
80
81
            return $this->client->put($endpoint, $data);
82
        } catch (\Exception $e) {
83
            throw new \Exception($e->getMessage(), 1);
84
        }
85
    }
86
87
    /**
88
     * DELETE method.
89
     * Remove data.
90
     *
91
     * @param string $endpoint API endpoint.
92
     * @param array  $options
93
     *
94
     * @return array
95
     */
96 View Code Duplication
    public function delete($endpoint, $options = [])
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...
97
    {
98
        try {
99
            self::__construct();
100
101
            return $this->client->delete($endpoint, $options);
102
        } catch (\Exception $e) {
103
            throw new \Exception($e->getMessage(), 1);
104
        }
105
    }
106
107
    /**
108
     * Return the last request header.
109
     *
110
     * @return \Automattic\WooCommerce\HttpClient\Request
111
     */
112
    public function getRequest()
113
    {
114
        try {
115
            return $this->client->http->getRequest();
116
        } catch (\Exception $e) {
117
            throw new \Exception($e->getMessage(), 1);
118
        }
119
    }
120
121
    /**
122
     * Return the http response headers from last request.
123
     *
124
     * @return \Automattic\WooCommerce\HttpClient\Response
125
     */
126
    public function getResponse()
127
    {
128
        try {
129
            return $this->client->http->getResponse();
130
        } catch (\Exception $e) {
131
            throw new \Exception($e->getMessage(), 1);
132
        }
133
    }
134
135
    /**
136
     * Count the total results and return it.
137
     *
138
     * @return int
139
     */
140
    public function countResults()
141
    {
142
        return (int) $this->getResponse()->getHeaders()[$this->headers['header_total']];
0 ignored issues
show
Bug introduced by
The property headers does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
143
    }
144
145
    /**
146
     * Count the total pages and return.
147
     *
148
     * @return mixed
149
     */
150
    public function countPages()
151
    {
152
        return (int) $this->getResponse()->getHeaders()[$this->headers['header_total_pages']];
153
    }
154
155
    /**
156
     * Return the current page number.
157
     *
158
     * @return int
159
     */
160
    public function current()
161
    {
162
        return !empty($this->getRequest()->getParameters()['page']) ? $this->getRequest()->getParameters()['page'] : 1;
163
    }
164
165
    /**
166
     * Return the previous page number.
167
     *
168
     * @return int|null
169
     */
170
    public function previous()
171
    {
172
        $currentPage = $this->current();
173
174
        return ($currentPage-- > 1) ? $currentPage : null;
175
    }
176
177
    /**
178
     * Return the next page number.
179
     *
180
     * @return int|null
181
     */
182
    public function next()
183
    {
184
        $currentPage = $this->current();
185
186
        return ($currentPage++ < $this->countPages()) ? $currentPage : null;
187
    }
188
}
189