Skip to content

Commit 37bd854

Browse files
committed
Added WooCommerce methods, update composer.json, update namespace and class names
1 parent 449c5f9 commit 37bd854

File tree

6 files changed

+159
-32
lines changed

6 files changed

+159
-32
lines changed

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
"minimum-stability": "stable",
1616
"autoload": {
1717
"psr-4": {
18-
"Codexshaper\\Woocommerce\\": "src/"
18+
"Codexshaper\\WooCommerce\\": "src/"
1919
}
2020
},
2121
"extra": {
2222
"laravel": {
2323
"providers": [
24-
"Codexshaper\\Woocommerce\\WooCommerceServiceProvider"
24+
"Codexshaper\\WooCommerce\\WooCommerceServiceProvider"
2525
],
2626
"aliases": {
27-
"Woocommerce": "Codexshaper\\Woocommerce\\Facades\\WoocommerceFacade"
27+
"WooCommerce": "Codexshaper\\WooCommerce\\Facades\\WooCommerce"
2828
}
2929
}
3030
}

composer.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Facades/WooCommerce.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
namespace Codexshaper\WooCommerce\Facades;
3+
4+
use Illuminate\Support\Facades\Facade;
5+
6+
class WooCommerce extends Facade
7+
{
8+
/**
9+
* Get the registered name of the component.
10+
*
11+
* @return string
12+
*/
13+
protected static function getFacadeAccessor()
14+
{
15+
return 'Codexshaper\WooCommerce\WooCommerceApi';
16+
}
17+
}

src/Traits/WoocommerceTrait.php

+117-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,124 @@
11
<?php
22

3-
namespace Codexshaper\Woocommerce\Traits;
3+
namespace Codexshaper\WooCommerce\Traits;
44

5-
trait WoocommerceTrait
5+
trait WooCommerceTrait
66
{
7-
public function get($endpoints, $args = [])
7+
/**
8+
* GET method.
9+
* Retrieve data
10+
*
11+
* @param string $endpoint API endpoint.
12+
* @param array $options
13+
*
14+
* @return array
15+
*/
16+
public function all($endpoint='', $options = [])
17+
{
18+
return $this->client->get($endpoint, $options);
19+
}
20+
/**
21+
* POST method.
22+
* Insert data
23+
*
24+
* @param string $endpoint API endpoint.
25+
* @param array $data
26+
*
27+
* @return array
28+
*/
29+
public function create($endpoint, $data)
830
{
9-
// return $this->client->get($endpoints, $args);
10-
return config('woocommerce.store_url');
31+
return $this->client->post($endpoint, $data);
32+
}
33+
/**
34+
* PUT method.
35+
* Update data
36+
*
37+
* @param string $endpoint API endpoint.
38+
* @param array $data
39+
*
40+
* @return array
41+
*/
42+
public function update($endpoint, $data)
43+
{
44+
return $this->client->put($endpoint, $data);
45+
}
46+
/**
47+
* DELETE method.
48+
* Remove data
49+
*
50+
* @param string $endpoint API endpoint.
51+
* @param array $options
52+
*
53+
* @return array
54+
*/
55+
public function delete($endpoint, $options = [])
56+
{
57+
return $this->client->delete($endpoint, $options);
58+
}
59+
/**
60+
* Return the last request header
61+
*
62+
* @return \Automattic\WooCommerce\HttpClient\Request
63+
*/
64+
public function getRequest()
65+
{
66+
return $this->client->http->getRequest();
67+
}
68+
/**
69+
* Return the http response headers from last request
70+
*
71+
* @return \Automattic\WooCommerce\HttpClient\Response
72+
*/
73+
public function getResponse()
74+
{
75+
return $this->client->http->getResponse();
76+
}
77+
/**
78+
* Return the current page number
79+
*
80+
* @return int
81+
*/
82+
public function current()
83+
{
84+
return !empty($this->getRequest()->getParameters()['page']) ? $this->getRequest()->getParameters()['page'] : 1;
85+
}
86+
/**
87+
* Count the total results and return it
88+
*
89+
* @return int
90+
*/
91+
public function countResults()
92+
{
93+
return (int)$this->getResponse()->getHeaders()['X-WP-Total'];
94+
}
95+
/**
96+
* Count the total pages and return
97+
*
98+
* @return mixed
99+
*/
100+
public function countPages()
101+
{
102+
return (int)$this->getResponse()->getHeaders()['X-WP-TotalPages'];
103+
}
104+
/**
105+
* Return the previous page number
106+
*
107+
* @return int|null
108+
*/
109+
public function previous()
110+
{
111+
$currentPage = $this->current();
112+
return ( --$currentPage > 1) ? $currentPage ? null;
113+
}
114+
/**
115+
* Return the next page number
116+
*
117+
* @return int|null
118+
*/
119+
public function next()
120+
{
121+
$currentPage = $this->current();
122+
return ( ++$currentPage < $this->countPages()) ? $currentPage ? null;
11123
}
12124
}

src/WooCommerceServiceProvider.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Codexshaper\Woocommerce;
3+
namespace Codexshaper\WooCommerce;
44

5-
use Codexshaper\Woocommerce\WoocommerceApi;
5+
use Codexshaper\WooCommerce\WooCommerceApi;
66
use Illuminate\Support\ServiceProvider;
77

88
class WooCommerceServiceProvider extends ServiceProvider
@@ -14,9 +14,7 @@ class WooCommerceServiceProvider extends ServiceProvider
1414
* @return void
1515
*/
1616
public function boot()
17-
{
18-
$this->loadRoutesFrom(__DIR__.'/routes.php');
19-
17+
{
2018
$this->publishes([
2119
__DIR__.'/config/woocommerce.php' => config_path('woocommerce.php'),
2220
],'woocommerce');
@@ -34,9 +32,9 @@ public function register()
3432
__DIR__.'/config/woocommerce.php', 'woocommerce'
3533
);
3634

37-
$this->app->singleton('WoocommerceApi', function(){
38-
return new WoocommerceApi();
35+
$this->app->singleton('WooCommerceApi', function(){
36+
return new WooCommerceApi();
3937
});
40-
// $app->alias('Codexshaper\Woocommerce\WoocommerceApi', 'WoocommerceApi');
38+
$this->app->alias('Codexshaper\Woocommerce\WooCommerceApi', 'WoocommerceApi');
4139
}
4240
}

src/WoocommerceApi.php

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
<?php
22

3-
namespace Codexshaper\Woocommerce;
3+
namespace Codexshaper\WooCommerce;
44

55
use Automattic\WooCommerce\Client;
6-
use Codexshaper\Woocommerce\Traits\WoocommerceTrait;
6+
use Codexshaper\WooCommerce\Traits\WoocommerceTrait;
77

8-
class WoocommerceApi extends Client
8+
class WooCommerceApi
99
{
10-
use WoocommerceTrait;
10+
use WooCommerceTrait;
1111

1212
protected $client;
1313

1414
public function __construct()
1515
{
16-
$this->client = Parent::__construct(
17-
config('woocommerce.store_url'),
18-
config('woocommerce.consumer_key'),
19-
config('woocommerce.consumer_secret'),
20-
[
21-
'version' => 'wc/'.config('woocommerce.api_version'),
22-
'verify_ssl' => config('woocommerce.verify_ssl'),
23-
'wp_api' => config('woocommerce.wp_api_integration'),
24-
'query_string_auth' => config('woocommerce.query_string_auth'),
25-
'timeout' => config('woocommerce.timeout'),
26-
]);
16+
$this->client = new Client(
17+
config('woocommerce.store_url'),
18+
config('woocommerce.consumer_key'),
19+
config('woocommerce.consumer_secret'),
20+
[
21+
'version' => 'wc/'.config('woocommerce.api_version'),
22+
'wp_api' => config('woocommerce.wp_api_integration'),
23+
'verify_ssl' => config('woocommerce.verify_ssl'),
24+
'query_string_auth' => config('woocommerce.query_string_auth'),
25+
'timeout' => config('woocommerce.timeout'),
26+
]);
2727
}
2828

2929
}

0 commit comments

Comments
 (0)