|
1
|
|
|
<?php |
|
2
|
|
|
namespace LinusShops\CanadaPost; |
|
3
|
|
|
|
|
4
|
|
|
use Guzzle\Http\Message\RequestInterface; |
|
5
|
|
|
use Guzzle\Http\Message\Response; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* |
|
10
|
|
|
* |
|
11
|
|
|
* @author Sam Schmidt <[email protected]> |
|
12
|
|
|
* @since 2015-12-09 |
|
13
|
|
|
* @company Linus Shops |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class Service |
|
16
|
|
|
{ |
|
17
|
|
|
const HTTP_GET = 'GET'; |
|
18
|
|
|
const HTTP_POST = 'POST'; |
|
19
|
|
|
const HTTP_PUT = 'PUT'; |
|
20
|
|
|
const HTTP_DELETE = 'DELETE'; |
|
21
|
|
|
const HTTP_PATCH = 'PATCH'; |
|
22
|
|
|
|
|
23
|
|
|
protected $baseUrl; |
|
24
|
|
|
protected $userid; |
|
25
|
|
|
protected $password; |
|
26
|
|
|
protected $parameters = array(); |
|
27
|
|
|
protected $headers = array(); |
|
28
|
|
|
|
|
29
|
|
|
public function __construct($baseUrl, $userid, $password) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->baseUrl = $baseUrl; |
|
32
|
|
|
$this->userid = $userid; |
|
33
|
|
|
$this->password = $password; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function setLanguage($value) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->setHeader('Accept-language', $value); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function setHeader($name, $value) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->headers[$name] = $value; |
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function hasParameter($name) |
|
48
|
|
|
{ |
|
49
|
|
|
return isset($this->parameters[$name]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function setParameter($name, $value) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->parameters[$name] = $value; |
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getParameter($name, $default=null) |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->hasParameter($name) ? |
|
61
|
|
|
$this->parameters[$name] : $default; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getBaseUrl() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->baseUrl; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return RequestInterface |
|
71
|
|
|
*/ |
|
72
|
|
|
abstract protected function buildRequest(); |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return Response |
|
76
|
|
|
*/ |
|
77
|
|
|
public function send() |
|
78
|
|
|
{ |
|
79
|
|
|
$request = $this->buildRequest(); |
|
80
|
|
|
|
|
81
|
|
|
//Apply standard header defaults |
|
82
|
|
|
$request->addHeader('Accept-language', 'en-CA'); |
|
83
|
|
|
$request->addHeader('Authorization', base64_encode( |
|
84
|
|
|
$this->userid.':'.$this->password |
|
85
|
|
|
)); |
|
86
|
|
|
|
|
87
|
|
|
//Apply custom headers (with possible overwrite) |
|
88
|
|
|
$this->applyHeaders($request); |
|
89
|
|
|
|
|
90
|
|
|
return $request->send(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param RequestInterface $request |
|
95
|
|
|
*/ |
|
96
|
|
|
private function applyHeaders(RequestInterface $request) |
|
97
|
|
|
{ |
|
98
|
|
|
foreach ($this->headers as $field => $value) { |
|
99
|
|
|
$request->addHeader($field, $value); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|