/
YiiDHL.php
209 lines (183 loc) · 5.67 KB
/
YiiDHL.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* YiiDHL class file.
*
* @author Richard González Alberto <damnpoet@gmail.com>
* @link https://github.com/damnpoet/yiidhl
* @package YiiDHL
*/
/**
* YiiDHL is a Yii module for faster and easier DHL integration.
*
* You may configure it as below. Check the public attributes and setter
* methods of this class for more options.
* <pre>
* return array(
* ...
* 'import => array(
* ...
* 'ext.yiidhl.models.*',
* ),
* 'components' => array(
* 'dhl' => array(
* 'class' => 'ext.yiidhl.YiiDHL',
* 'testMode' => true,
* 'useProxy' => true,
* 'proxyHost' => 'host:8080',
* 'proxyAuth' => 'username:password',
* ),
* ...
* )
* );
* </pre>
*
* Example usage:
* <pre>
* $dhlOrderInfo = Yii::app()->dhl->find($trackingNumber);
* </pre>
*/
class YiiDHL extends CApplicationComponent {
/**
* @var bool whether to log messages using Yii::log().
* Defaults to true.
*/
public $logging = true;
/**
* @var bool whether to use a proxy server.
* Defaults to false.
*/
public $useProxy = false;
/**
* @var string the proxy host data. Ex. proxy.host.com:8080
* Defaults to empty
*/
public $proxyHost = '';
/**
* @var string the proxy authentification data. The string should be formated as
* 'username:password'.
* Defaults to empty
*/
public $proxyAuth = '';
/**
* @var bool wheter to use test mode or not. Test mode can be used to test your
* application before going to live mode.
* Defaults to true
*/
public $testMode = true;
/**
* @var string the userId provided by DHL.
* Defaults to empty
*/
public $dhlSiteId = 'DServiceVal';
/**
* @var string the password provided by DHL.
* Defaults to empty
*/
public $dhlPassword = 'testServVal';
/**
* @var mixed Holds the DHLTracker tracker
*/
protected $tracker;
/**
* @var mixed Holds the DHLRequestHandler requestHandler
*/
protected $requestHandler;
/**
* @var mixed Holds the DHLCapabilityAndQuoteHandler capabilityAndQuoteHandler
*/
protected $capabilityAndQuoteHandler;
/**
* Calls the {@link registerScripts()} method.
*/
public function init() {
parent::init();
}
/**
* Requests a tracking number info from DHL.
*
* The return value is the {@link DHLOrderInfo} returned, or null in case it
* the number wasn't found.
*
* @param int $trackingNumber
* @return DHLOrderInfo
*/
public function find($trackingNumber) {
if ($this->logging === true)
self::logTrackingRequest($trackingNumber);
return $this->getTracker()->single($trackingNumber);
}
/**
* Books a new shipping on DHL.
*
* The return value is boolean meaning, booked or not.
*
* @return bool
*/
public function book() {
return $this->getRequestHandler()->bookRequest();
}
/**
* Query webserver for capability.
*
* The return value is a DHLService array, or empty array if nothing returned.
*
* @return DHLCapabilityResponse
*/
public function getServices($options = array()) {
$additionalOptions = array(
'siteId'=>$this->dhlSiteId,
'sitePassword'=>$this->dhlPassword,
);
$options = array_merge($additionalOptions, $options);
$response = $this->getCapabilityAndQuoteHandler()->queryCapability($options);
return !is_null($response) ? $response->services : array();
}
/**
* Logs a tracking request using Yii::log.
* @return string log message
*/
public static function logTrackingRequest($trackingNumber) {
$msg = "Tracking number #{$trackingNumber}\n";
Yii::log($msg, CLogger::LEVEL_INFO, 'ext.yiidhl.YiiDHL');
return $msg;
}
/**
* Gets the DHLTracker {@link DHLTracker} class instance
* @return DHLTracker
*/
public function getTracker() {
if ($this->tracker === null) {
$this->tracker = new DHLTracker($this->testMode);
$this->tracker->setAuth($this->dhlSiteId, $this->dhlPassword);
if ($this->useProxy)
$this->tracker->setProxyInfo($this->proxyHost, $this->proxyAuth, true);
}
return $this->tracker;
}
/**
* Gets the DHLRequestHandler {@link DHLRequestHandler} class instance
* @return DHLRequestHandler
*/
public function getRequestHandler() {
if ($this->requestHandler === null) {
$this->requestHandler = new DHLRequestHandler($this->testMode);
$this->requestHandler->setAuth($this->dhlSiteId, $this->dhlPassword);
if ($this->useProxy)
$this->requestHandler->setProxyInfo($this->proxyHost, $this->proxyAuth, true);
}
return $this->requestHandler;
}
/**
* Gets the DHLCapabilityAndQuoteHandler {@link DHLCapabilityAndQuoteHandler} class instance
* @return DHLCapabilityAndQuoteHandler
*/
public function getCapabilityAndQuoteHandler() {
if ($this->capabilityAndQuoteHandler === null) {
$this->capabilityAndQuoteHandler = new DHLCapabilityAndQuoteHandler($this->testMode);
$this->capabilityAndQuoteHandler->setAuth($this->dhlSiteId, $this->dhlPassword);
if ($this->useProxy)
$this->capabilityAndQuoteHandler->setProxyInfo($this->proxyHost, $this->proxyAuth, true);
}
return $this->capabilityAndQuoteHandler;
}
}