-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractConnector.php
More file actions
125 lines (97 loc) · 3.35 KB
/
AbstractConnector.php
File metadata and controls
125 lines (97 loc) · 3.35 KB
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
<?php
namespace dLdL\WebService;
use dLdL\WebService\Adapter\CacheHelperInterface;
use dLdL\WebService\Exception\ConnectionException;
use dLdL\WebService\Exception\RequestException;
use dLdL\WebService\Exception\WebServiceException;
use dLdL\WebService\Adapter\LoggerHelper;
use dLdL\WebService\Http\Request;
use dLdL\WebService\Adapter\ParameterBag;
use dLdL\WebService\Adapter\ParameterBagInterface;
use Psr\Log\LoggerInterface;
abstract class AbstractConnector implements ConnectorInterface
{
/**
* @var null|CacheHelperInterface
*/
private $cache;
/**
* @var ParameterBagInterface
*/
private $parameters;
/**
* @var LoggerHelper
*/
private $log;
public function __construct()
{
$this->parameters = new ParameterBag();
}
abstract protected function handleRequest(Request $request);
public function sendRequest(Request $request)
{
if (!$this->isConnected() || $this->getHost() === null) {
throw new ConnectionException($this->currentConnector().' must be connected before sending data.');
}
if (!$this->supportsMethod($request->getMethod())) {
throw new RequestException('Method '.$request->getMethod().' is not supported by '.$this->currentConnector().'.');
}
if ($this->hasCache() && $this->getCache()->getPool()->hasItem($request)) {
return $this->getFromCache($request);
}
try {
$this->log->request($this, $request);
$response = $this->handleRequest($request);
$this->log->response($this, $response, $request);
if ($this->hasCache()) {
$this->saveToCache($request, $response);
}
} catch (WebServiceException $e) {
$this->log->requestFailure($this, $request, $e->getMessage());
throw new WebServiceException('Unable to contact WebService : '.$e->getMessage());
}
return $response;
}
private function getFromCache(Request $request)
{
$this->log->cacheGet($this->getHost(), $request, (new \ReflectionClass($this->getCache()))->getShortName());
$response = $this->getCache()->getPool()->getItem($request);
$this->log->response($this, $response, $request);
return $response;
}
private function saveToCache(Request $request, $response)
{
$duration = $this->getCache()->getConfig()->get($request, CacheHelperInterface::DEFAULT_DURATION);
$item = $this->getCache()->getPool()
->getItem($request)
->set($response)
->expiresAfter($duration)
;
$this->getCache()->getPool()->save($item);
$this->log->cacheAdd($this, $request, (new \ReflectionClass($this->getCache()))->getShortName(), $duration);
}
private function currentConnector()
{
return (new \ReflectionClass($this))->getShortName();
}
public function getParameters()
{
return $this->parameters;
}
public function setCache(CacheHelperInterface $cache = null)
{
$this->cache = $cache;
}
public function hasCache()
{
return null !== $this->cache;
}
public function getCache()
{
return $this->cache;
}
public function setLogger(LoggerInterface $logger)
{
$this->log = new LoggerHelper($logger);
}
}