1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @category Brownie/CartsGuru |
4
|
|
|
* @author Brownie <[email protected]> |
5
|
|
|
* @license http://www.gnu.org/copyleft/lesser.html |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Brownie\CartsGuru; |
9
|
|
|
|
10
|
|
|
use Brownie\CartsGuru\HTTPClient\HTTPClient; |
11
|
|
|
use Brownie\CartsGuru\Model\Cart; |
12
|
|
|
use Brownie\CartsGuru\Model\Order; |
13
|
|
|
use Brownie\CartsGuru\Model\DataModel; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* CartsGuru API. |
17
|
|
|
*/ |
18
|
|
|
class CartsGuru |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var HTTPClient |
23
|
|
|
*/ |
24
|
|
|
private $httpClient; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Site ID. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $siteId; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Sets incoming data. |
35
|
|
|
* |
36
|
|
|
* @param HTTPClient $httpClient HTTP client. |
37
|
|
|
* @param string $siteId Site ID. |
38
|
|
|
*/ |
39
|
2 |
|
public function __construct(HTTPClient $httpClient, $siteId) |
40
|
|
|
{ |
41
|
|
|
$this |
42
|
2 |
|
->setHttpClient($httpClient) |
43
|
2 |
|
->setSiteId($siteId); |
44
|
2 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Sets an HTTP client. |
48
|
|
|
* Returns the current object. |
49
|
|
|
* |
50
|
|
|
* @param HTTPClient $httpClient Http client |
51
|
|
|
* |
52
|
|
|
* @return self |
53
|
|
|
*/ |
54
|
2 |
|
private function setHttpClient(HTTPClient $httpClient) |
55
|
|
|
{ |
56
|
2 |
|
$this->httpClient = $httpClient; |
57
|
2 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns HTTP client. |
62
|
|
|
* |
63
|
|
|
* @return HTTPClient |
64
|
|
|
*/ |
65
|
2 |
|
private function getHttpClient() |
66
|
|
|
{ |
67
|
2 |
|
return $this->httpClient; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set site ID. |
72
|
|
|
* |
73
|
|
|
* @param string $siteId Site ID |
74
|
|
|
* |
75
|
|
|
* @return self |
76
|
|
|
*/ |
77
|
2 |
|
private function setSiteId($siteId) |
78
|
|
|
{ |
79
|
2 |
|
$this->siteId = $siteId; |
80
|
2 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return site ID. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
2 |
|
private function getSiteId() |
89
|
|
|
{ |
90
|
2 |
|
return $this->siteId; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Track Cart. |
95
|
|
|
* Returns the status of the request. |
96
|
|
|
* |
97
|
|
|
* @param Cart $cart Description of the cart. |
98
|
|
|
* |
99
|
|
|
* @return boolean |
100
|
|
|
*/ |
101
|
1 |
|
public function trackCart(Cart $cart) |
102
|
|
|
{ |
103
|
1 |
|
return $this->track($cart); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Track Order. |
108
|
|
|
* Returns the status of the request. |
109
|
|
|
* |
110
|
|
|
* @param Order $order Description of the order. |
111
|
|
|
* |
112
|
|
|
* @return boolean |
113
|
|
|
*/ |
114
|
1 |
|
public function trackOrder(Order $order) |
115
|
|
|
{ |
116
|
1 |
|
return $this->track($order); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Request track. |
121
|
|
|
* Returns the status of the request. |
122
|
|
|
* |
123
|
|
|
* @param DataModel $model Description of the data. |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
2 |
|
private function track(DataModel $model) |
128
|
|
|
{ |
129
|
2 |
|
$model->setSiteId($this->getSiteId()); |
|
|
|
|
130
|
2 |
|
$model->validate(); |
131
|
|
|
|
132
|
|
|
$response = $this |
133
|
2 |
|
->getHttpClient() |
134
|
2 |
|
->request( |
135
|
2 |
|
HTTPClient::HTTP_CODE_200, |
136
|
2 |
|
$model->getEndpoint(), |
137
|
2 |
|
$model, |
138
|
2 |
|
HTTPClient::HTTP_METHOD_POST |
139
|
|
|
); |
140
|
|
|
|
141
|
2 |
|
return isset($response['response']['status']) && ('success' == $response['response']['status']); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|