1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Codexshaper\WooCommerce\PHP; |
4
|
|
|
|
5
|
|
|
use Automattic\WooCommerce\Client; |
6
|
|
|
use Codexshaper\WooCommerce\PHP\Helpers\Config; |
7
|
|
|
use Codexshaper\WooCommerce\PHP\Traits\WooCommerceTrait; |
8
|
|
|
|
9
|
|
|
class WooCommerce |
10
|
|
|
{ |
11
|
|
|
use WooCommerceTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
*@var \Automattic\WooCommerce\Client |
15
|
|
|
*/ |
16
|
|
|
protected static $client; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
*@var array |
20
|
|
|
*/ |
21
|
|
|
protected static $headers = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Build Woocommerce connection. |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
protected $properties = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get Inaccessible Property. |
33
|
|
|
* |
34
|
|
|
* @param string $name |
35
|
|
|
* |
36
|
|
|
* @return int|string|array|object|null |
37
|
|
|
*/ |
38
|
|
|
public function __get($name) |
39
|
|
|
{ |
40
|
|
|
return $this->$name; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set Option. |
45
|
|
|
* |
46
|
|
|
* @param string $name |
47
|
|
|
* @param string $value |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function __set($name, $value) |
52
|
|
|
{ |
53
|
|
|
$this->properties[$name] = $value; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function __call($method, $parameters) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
if (!method_exists($this, $method)) { |
59
|
|
|
preg_match_all('/((?:^|[A-Z])[a-z]+)/', $method, $partials); |
60
|
|
|
$method = array_shift($partials[0]); |
61
|
|
|
if (!method_exists($this, $method)) { |
62
|
|
|
throw new \Exception('Sorry! you are calling wrong method'); |
63
|
|
|
} |
64
|
|
|
array_unshift($parameters, strtolower(implode('_', $partials[0]))); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->$method(...$parameters); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function __callStatic($method, $parameters) |
71
|
|
|
{ |
72
|
|
|
return (new static() )->$method(...$parameters); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function instance() |
76
|
|
|
{ |
77
|
|
|
try { |
78
|
|
|
static::$headers = [ |
79
|
|
|
'header_total' => Config::get('woocommerce.header_total'), |
80
|
|
|
'header_total_pages' => Config::get('woocommerce.header_total_pages'), |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
static::$client = new Client( |
84
|
|
|
Config::get('woocommerce.store_url'), |
85
|
|
|
Config::get('woocommerce.consumer_key'), |
86
|
|
|
Config::get('woocommerce.consumer_secret'), |
87
|
|
|
[ |
88
|
|
|
'version' => 'wc/'.Config::get('woocommerce.api_version'), |
89
|
|
|
'wp_api' => Config::get('woocommerce.wp_api'), |
90
|
|
|
'verify_ssl' => Config::get('woocommerce.verify_ssl'), |
91
|
|
|
'query_string_auth' => Config::get('woocommerce.query_string_auth'), |
92
|
|
|
'timeout' => Config::get('woocommerce.timeout'), |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
} catch (\Exception $e) { |
96
|
|
|
throw new \Exception($e->getMessage(), 1); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.