forked from FACT-Finder/FACT-Finder-PHP-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code-mockup-dataprovider.php
104 lines (86 loc) · 2.84 KB
/
code-mockup-dataprovider.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
<?php
interface RequestFactoryInterface
{
/**
* @returns Request
*/
public function getRequest();
}
class EasyCurlRequestFactory implements RequestFactoryInterface
{
private $dataProvider = new EasyCurlDataProvider();
public function getRequest()
{
$request = new CurlRequest($dataProvider);
$this->dataProvider->register($request);
}
}
abstract class AbstractDataProvider
{
protected $urlBuilder = new UrlBuilder();
private $requests = array();
private $responses = array();
public function register(Request $request)
{
$this->requests[$request->id] = $request;
$this->responses[$request->id] = null;
}
public function getResponse($request)
{
$response = $this->$responses[$request];
if (is_null($response) || $this->hasUrlChanged($request))
$this->$responses[$request->id] = $this->loadResponse($request);
return $this->$responses[$request->id];
}
private function hasUrlChanged($request)
{
// uses $request->getPreviousUrl() and $this->urlBuilder
}
/**
* @return Response
*/
abstract protected function loadResponse($request);
}
class EasyCurlDataProvider extends AbstractDataProvider
{
protected function loadResponse($id)
{
//...
}
}
class Request
{
private static $nextId = 0;
private $id;
private $dataProvider;
protected $httpHeaderFields;
protected $action;
protected $parameters;
protected $connectTimeout;
protected $timeout;
protected $connectionOptions;
protected $previousUrl;
protected $response;
public function __construct($dataProvider)
{
$this->dataProvider = $dataProvider;
$this->id = self::$nextId++;
}
// for everyone
public function getHttpHeaderFields() { return $this->httpHeaderFields; }
public function getParameters() { return $this->parameters; }
// for Adapters
public function setAction($action) { $this->action = $action; }
public function setConnectTimeout($timeout) { $this->connectTimeout = $timeout; }
public function setTimeout($timeout) { $this->timeout = $timeout; }
public function getResponse() { return $this->dataProvider->getResponse($this->id); }
// for DataProvider
public function getAction() { return $this->action; }
public function isResponseSet() { return empty($this->response); }
public function setReponse($response) { $this->response = $response; }
public function getPreviousUrl() { return $this->previousUrl; }
public function getConnectTimeout() { return $this->connectTimeout; }
public function getTimeout() { return $this->timeout; }
public function getConnectionOptions() { return $this->connectionOptions; }
public function setConnectionOptions($co) { array_merge($this->connectionOptions, $co); }
}