Functions   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 137
c 0
b 0
f 0
wmc 27
lcom 1
cbo 1
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getUserKeys() 0 7 1
A get() 0 31 3
A post() 0 6 2
A put() 0 6 2
A delete() 0 6 2
A options() 0 6 2
A getOrdersID() 0 11 3
A getOptions() 0 4 1
A getOption() 0 8 3
A synched() 0 8 4
A wc_api_connect() 0 28 3
1
<?php
2
/**
3
 * m'Manager | Invoices Management System
4
 * 
5
 * This content is released under the Proprietary License (Proprietary)
6
 *
7
 * Copyright (c) 2017, Eric Claver AKAFFOU - All Rights Reserved
8
 * Unauthorized copying of this file, via any medium is strictly prohibited
9
 * Proprietary and confidential
10
 * 
11
 * @package m'Manager
12
 * @author  Eric Claver AKAFFOU
13
 * @copyright   Copyright (c) 2017, on'Eric Computing, Inc. (https://www.onericcomputing.com/)
14
 * @license https://www.mmanager.fr  Proprietary License
15
 * @link    https://codecanyon.net/item/mmanager-invoices-management-system/19866435?s_rank=1
16
 * @since   Version 1.0.0
17
 * @filesource
18
 */
19
20
namespace Mmanager\Extensions\Woocommerce;
21
use Mmanager\Extensions\Database\Builder;
22
use Automattic\WooCommerce\HttpClient\HttpClientException;
23
use Automattic\WooCommerce\Client;
24
25
class Functions {
26
	protected $db;
27
	protected $builder;
28
	protected $options;
29
	
30
	public function __construct() {
31
		$this->builder = new Builder('woocommerce');
32
		$this->db = $this->builder->getDB();
33
		$this->options = $this->getOptions();
34
	}
35
	public function getUserKeys() {
36
		return array(
37
			'store_url' => $this->getOption('home'),
38
			'consumer_key' => $this->getOption('wpt_wc_api_consumer_consumer_key'),
39
			'consumer_secret' => $this->getOption('wpt_wc_api_consumer_consumer_secret')
40
		);
41
	}
42
	public function get($endpoint, $params) {
43
		if ($this->synched()) {
44
			try {
45
				// Array of response results.
46
				$woocommerce = $this->wc_api_connect();
47
				$results = $woocommerce->get($endpoint, $params);
48
				// Example: ['customers' => [[ 'id' => 8, 'created_at' => '2015-05-06T17:43:51Z', 'email' => ...
49
50
				// Last request data.
51
				$lastRequest = $woocommerce->http->getRequest();
52
				$lastRequest->getUrl(); // Requested URL (string).
53
				$lastRequest->getMethod(); // Request method (string).
54
				$lastRequest->getParameters(); // Request parameters (array).
55
				$lastRequest->getHeaders(); // Request headers (array).
56
				$lastRequest->getBody(); // Request body (JSON).
57
58
				// Last response data.
59
				$lastResponse = $woocommerce->http->getResponse();
60
				$lastResponse->getCode(); // Response code (int).
61
				$lastResponse->getHeaders(); // Response headers (array).
62
				$lastResponse->getBody(); // Response body (JSON).
63
64
				return $results;
65
66
			} catch (HttpClientException $e) {
0 ignored issues
show
Bug introduced by
The class Automattic\WooCommerce\H...ent\HttpClientException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
67
				$e->getMessage(); // Error message.
68
				$e->getRequest(); // Last request data.
69
				$e->getResponse(); // Last response data.
70
			}
71
		}
72
	}
73
	public function post($endpoint, $data) {
74
		if ($this->synched()) {
75
			$woocommerce = $this->wc_api_connect();
76
			return $woocommerce->post($endpoint, $data);
77
		}
78
	}
79
	public function put($endpoint, $data) {
80
		if ($this->synched()) {
81
			$woocommerce = $this->wc_api_connect();
82
			return $woocommerce->put($endpoint, $data);
83
		}
84
	}
85
	public function delete($endpoint, $params = []) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
		if ($this->synched()) {
87
			$woocommerce = $this->wc_api_connect();
88
			return $woocommerce->delete($endpoint, $params = []);
89
		}
90
	}
91
	public function options($endpoint) {
92
		if ($this->synched()) {
93
			$woocommerce = $this->wc_api_connect();
94
			return $woocommerce->options($endpoint);
95
		}
96
	}
97
	public function getOrdersID() {
98
		$ids = [];
99
		$query = "SELECT ID FROM wp_posts where post_type = 'shop_order'";
100
		$results = $this->db->get_results($query);
101
		if ($results) {
102
			foreach ($results as $id) {
103
				array_push($ids, $id->ID);
104
			}
105
		}
106
		return $ids;
107
	}
108
	public function getOptions() {
109
		$query = "SELECT * FROM wp_options";
110
		return $this->db->get_results($query);
111
	}
112
	public function getOption($option_name) {
113
		$options = $this->getOptions();
114
		foreach ($options as $option) {
115
			if ($option->option_name == $option_name) {
116
				return $option->option_value;
117
			}
118
		}
119
	}
120
	public function synched() {
121
		$options = $this->options;
122
		foreach ($options as $option) {
123
			if ($option->option_name === 'wpt_wc_mmanager_connect_sync_data' && $option->option_value == 'on') {
124
				return true;
125
			}
126
		}
127
	}
128
	/**
129
	 * Connect to the WooCommerce API.
130
	 *
131
	 * @return \Automattic\WooCommerce\Client|bool
132
	 */
133
	public function wc_api_connect() {
134
		static $connection;
135
	 
136
		if ( isset( $connection ) ) {
137
			return $connection;
138
		}
139
	 
140
		$keys = $this->getUserKeys();
141
142
		if ( ! $keys ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $keys of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
143
			$connection = false;
144
	 
145
			return $connection;
146
		}
147
	 
148
		$connection = new Client(
149
			$keys['store_url'],
150
			$keys['consumer_key'],
151
			$keys['consumer_secret'],
152
			array(
153
				'wp_api'     => true,
154
				'version'    => 'wc/v2',
155
				'verify_ssl' => false, // Allow self-signed certificates (remove for prod)
156
			)
157
		);
158
	 
159
		return $connection;
160
	}
161
}
162
163