-
Notifications
You must be signed in to change notification settings - Fork 1
/
TwixBuilder.php
220 lines (181 loc) · 6.32 KB
/
TwixBuilder.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
210
211
212
213
214
215
216
217
218
219
220
<?php
/** Created By Thunder33345 **/
namespace Thunder33345\Twix;
use Thunder33345\Twix\Exception\IllegalIdException;
use Thunder33345\Twix\Exception\InvalidClosure\IllegalStaticException;
use Thunder33345\Twix\Exception\InvalidClosure\InvalidArgumentAcceptException;
use Thunder33345\Twix\Exception\InvalidClosure\InvalidArgumentAcceptTypeException;
use Thunder33345\Twix\Exception\InvalidClosure\InvalidArgumentCountException;
use Thunder33345\Twix\Exception\InvalidMethodException;
use Thunder33345\Twix\Exception\InvalidTokensException;
use Thunder33345\Twix\Exception\MissingValueException;
use Thunder33345\Twix\Objects\CompletionError;
use Thunder33345\Twix\Objects\FetchError;
class TwixBuilder
{
//Mandatory
private $tokens,$url,$method;
//Optionals
private $fields,$then,$id;
//Errors
private $error,$fetchError;
static public function get()
{
return new static();
}
/**
* API Tokens
* @param array $tokens
* @return $this
*/
public function setTokens(array $tokens)
{
$required = ['oauth_access_token','oauth_access_token_secret','consumer_key','consumer_secret'];
foreach($required as $checkFor){
if(!isset($tokens[$checkFor])) {
throw InvalidTokensException::render($checkFor);
}
}
$this->tokens = $tokens;
return $this;
}
/**
* Endpoint URL
* @param string $url
* @return $this
*/
public function setUrl(string $url)
{
$this->url = $url;
return $this;
}
/**
* Methodc(Twix::REQUEST_POST OR Twix::REQUEST_GET)
* @param $method
* @return $this
*/
public function setMethod($method)
{
$method = strtolower($method);//be not so strict
if($method !== Twix::REQUEST_GET AND $method !== Twix::REQUEST_POST) {
throw InvalidMethodException::render($method);
}
$this->method = $method;
return $this;
}
/**
* Fields to be posted
* @param array $fields
* @return $this
*/
public function setFields(array $fields)
{
$this->fields = $fields;
return $this;
}
/**
* What to do after the completion of the task
* This will be executed at the main thread
* @param \Closure $function
* @return $this
* @throws \Exception
*
* Please keep your function free of static variables (use($vars)) as it seems to be unstable used in async
* Your function is expected to be
* function (TwixResult $result) {/do something/}
* the only variable that will be returned is TwixResult which will contain everything you need
*/
public function then(\Closure $function)
{
$reflect = new \ReflectionFunction($function);
$parameters = $reflect->getParameters();
$static = $reflect->getStaticVariables();
if(count($static) > 0) throw IllegalStaticException::render();
if(count($parameters) !== 1) throw InvalidArgumentCountException::render();
foreach($parameters as $parameter){
if($parameter->getType() === null) throw InvalidArgumentAcceptException::render('TwixResult');
if((string)$parameter->getType() !== TwixResult::class) throw InvalidArgumentAcceptTypeException::render(TwixResult::class,(string)$parameter->getType());
}
$this->then = $function;
return $this;
}
/*
* something serialize-able, only useful if you have a then, useful for identification proposes
*/
public function setId($id)
{
try{
serialize($id);
}
catch(\Exception$exception){
throw IllegalIdException::render($exception);
}
$this->id = $id;
return $this;
}
/**
* Called when there's a exception thrown onCompletion
* @param \Closure $error
* @return $this
*/
public function error(\Closure $error)
{
$reflect = new \ReflectionFunction($error);
$static = $reflect->getStaticVariables();
$parameters = $reflect->getParameters();
if(count($static) > 0) throw IllegalStaticException::render();
if(count($parameters) !== 1) throw InvalidArgumentCountException::render();
foreach($parameters as $parameter){
if($parameter->getType() === null) throw InvalidArgumentAcceptException::render('CompletionError');
if((string)$parameter->getType() !== CompletionError::class) throw InvalidArgumentAcceptTypeException::render('CompletionError',(string)$parameter->getType());
}
$this->error = $error;
return $this;
}
/**
* Called when there's a exception thrown onRun
* @param \Closure $error
* @return $this
*/
public function fetchError(\Closure $error)
{
$reflect = new \ReflectionFunction($error);
$static = $reflect->getStaticVariables();
$parameters = $reflect->getParameters();
if(count($static) > 0) throw IllegalStaticException::render();
if(count($parameters) !== 1) throw InvalidArgumentCountException::render();
foreach($parameters as $parameter){
if($parameter->getType() === null) throw InvalidArgumentAcceptException::render('FetchError');
if((string)$parameter->getType() !== FetchError::class) throw InvalidArgumentAcceptTypeException::render('FetchError',(string)$parameter->getType());
}
$this->fetchError = $error;
return $this;
}
/*
* Returns the result
*/
public function getResult()
{
if(!isset($this->tokens)) throw MissingValueException::render('Tokens','TwixBuilder::setToken()');
if(!isset($this->url)) throw MissingValueException::render('Url','TwixBuilder::setUrl()');
if(!isset($this->method)) throw MissingValueException::render('Method','TwixBuilder::setMethod');
$token = $this->tokens;
$url = $this->url;
$method = $this->method;
if(isset($this->fields)) $fields = $this->fields; else $fields = [];
if(isset($this->then)) $then = $this->then; else $then = null;
if(isset($this->id) AND $this->id !== null) $id = $this->id; else $id = [];
if(isset($this->error)) $error = $this->error; else $error = null;
if(isset($this->fetchError)) $fetchError = $this->fetchError; else $fetchError = null;
$result = new TwixFetcher($token,$url,$method,$fields,$then,$id,$error,$fetchError);
return $result;
}
/*
* Getters, they have absolutely no reason to be here...
*/
public function getTokens() { return $this->tokens; }
public function getUrl() { return $this->url; }
public function getMethod() { return $this->method; }
public function getFields() { return $this->fields; }
public function getId() { return $this->id; }
}