-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfb_request_monkey.php
964 lines (854 loc) · 28.6 KB
/
fb_request_monkey.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
<?php
require_once('sdk.php');
class FB_Request_Monkey {
const MAX_ITEMS_IN_BATCH = 50;
public static $actionKeyMap = array(
'query' => 'relative_url',
'token' => 'access_token',
'name' => 'name',
'method' => 'method',
'params' => 'params',
'label' => 'label',
'relative_url' => 'relative_url',
'access_token' => 'access_token',
);
public static $sdk = null;
public static $testArray = array();
/**
* sendOne function.
*
* Sends a single action
*
* @access public
* @static
* @param array $action the details of a single facebook request
* @param array $config (default: null) FB PHP SDK config details
* @return array facebook results
*/
public static function sendOne($action, $config = null, $options = array()) {
$actions = array($action);
return self::sendMany($actions, $config, $options);
}
/**
* sendMany function.
*
* Sends multiple actions. If a config array is passed in, the PHP SDK will initialize
*
* @access public
* @static
* @param array $actions an array of action arrays
* @param array $config (default: null) FB PHP SDK config details
* @param array $options (default: array)
* @return array facebook results
*/
public static function sendMany($actions, $config = null, $options = array()) {
self::validateActions($actions, $options);
// set allow errors if its in the options array, if not, set it as false
$allowErrors = isset($options['allowErrors']) ? $options['allowErrors'] : false;
$failsafeToken = isset($options['failsafeToken']) ? $options['failsafeToken'] : null;
// an access token that has been confirmed to be valid to ensure that a batch request will go out
self::initialize($config);
$results = array();
$processedResponses = self::getProcessedResponsesFromActions($actions, $allowErrors, $failsafeToken);
$results = self::addDataFromProcessedResponsesToResults($processedResponses, $results);
$overflowActions = self::getOverflowActions($processedResponses);
// if there any overflow actions
if(count($overflowActions) > 0) {
$overflowProcessedResponses = self::getProcessedResponsesFromActions($overflowActions, $allowErrors, $failsafeToken);
// because these are overflow requests, the sent result number is inaccurate, so it is set to zero
// to correct for the discrepency
$overflowProcessedResponses = self::setSentDataCountToZero($overflowProcessedResponses);
$results = FB_Request_Monkey::addDataFromProcessedResponsesToResults($overflowProcessedResponses, $results);
// combine the two response sets together so they can be checked
// for the number of results
$processedResponses = array_merge($processedResponses, $overflowProcessedResponses);
}
self::checkDataCount($processedResponses, $allowErrors);
return $results;
}
/**
* validateActions function.
*
* @access public
* @static
* @param mixed $actions
* @param mixed $options
* @return void
*/
public static function validateActions($actions, $options) {
__::each($actions, function($action) use($options){
if(isset($options['failsafeToken']) && $action['query'] == 'debug_token') {
throw new Exception("The Facebook API doesn't allow failsafe tokens on debug_token queries, please remove the failsafe token before continuing");
}
});
}
/**
* getProcessedResponsesFromActions function.
*
*
* @access public
* @static
* @param array $actions
* @return array
*/
public static function getProcessedResponsesFromActions($actions, $allowErrors, $failsafeToken) {
$actionCount = count($actions);
$responseQueue = self::getResponseQueueFromActions($actions, $failsafeToken);
$processedResponses = self::processResponseQueue($responseQueue, $actionCount, $allowErrors);
return $processedResponses;
}
/**
* getResponseQueueFromActions function.
*
* take the actions send them and get back the response queue
*
* @access public
* @static
* @param mixed $actions
* @param mixed $allowErrors
* @param mixed $failsafeToken
* @return void
*/
public static function getResponseQueueFromActions($actions, $failsafeToken) {
$callQueue = self::getCallQueue($actions);
$formattedCallQueue = self::formatCallQueue($callQueue, $failsafeToken);
$responseQueue = self::sendAllCalls($formattedCallQueue, $actions);
$responseQueue = self::fillInMissingData($responseQueue, $failsafeToken);
return $responseQueue;
}
/**
* fillInMissingData function.
*
* If there are more request in a batch than facebook can handle
* at a particular time, they return null instead of the
* this function handle resending request to get that missing
* data
*
* @access public
* @static
* @param mixed $responseQueue
* @return void
*/
public static function fillInMissingData($responseQueue, $failsafeToken) {
$fillInActions = self::getFillInActionsFromResponseQueue($responseQueue);
$responseQueue = self::addDataFromFillInActionsToResponseQueue($responseQueue, $fillInActions, $failsafeToken);
return $responseQueue;
}
public static function addDataFromFillInActionsToResponseQueue($responseQueue, $fillInActions, $failsafeToken) {
if(count($fillInActions) > 0) {
$fillInResponseQueue = self::getResponseQueueFromActions($fillInActions, $failsafeToken);
__::map($fillInResponseQueue, function($responsePackage) use(&$responseQueue){
$actions = $responsePackage['actions'];
$batches = $responsePackage['batches'];
__::map($batches, function($batch, $currentBatchIndex)use(&$responseQueue, $actions){
$associatedAction = $actions[$currentBatchIndex];
$responseIndex = $associatedAction['label'][0];
$batchIndex = $associatedAction['label'][1];
$responseQueue[$responseIndex]['batches'][$batchIndex] = $batch;
});
});
}
return $responseQueue;
}
public static function getFillInActionsFromResponseQueue($responseQueue) {
$fillInActions = array();
__::chain($responseQueue)
->map(function($responsePackage, $responseIndex) use(&$fillInActions){
$actions = $responsePackage['actions'];
$batches = $responsePackage['batches'];
__::map($batches, function($batch, $batchIndex) use($actions, &$fillInActions, $responseIndex){
if($batch == null) {
$fillInActions = FB_Request_Monkey::addFillInActionForNullBatch($responseIndex, $batchIndex, $fillInActions, $actions);
}
});
});
return $fillInActions;
}
public static function addFillInActionForNullBatch($responseIndex, $batchIndex, $fillInActions, $actions) {
$fillInAction = $actions[$batchIndex];
$fillInAction['label'] = array(
$responseIndex,
$batchIndex,
);
array_push($fillInActions, $fillInAction);
return $fillInActions;
}
/**
* getOverflowActions function.
*
* Facebook requests have an inherent limit on the number of results that can be returned
* in a single request. This function iterates over processed responses and generates new actions
* to get any results above and beyond the limit for that particular request
*
* @access public
* @static
* @param array $allProcessedResponses
* @return array
*/
public static function getOverflowActions($allProcessedResponses) {
return __::chain($allProcessedResponses)
->map(function($processedResponse) {
return FB_Request_Monkey::buildOverflowActionsForResponse($processedResponse);
})
->flatten(true)
// not all requests have over flow actions
// eliminate the empty arrays
->compact()
->value();
}
/**
* buildOverflowActionsForResponse function.
*
* Depending on the number of available results in the response,
* there could be one or more actions needed to get those results.
* This function handles generating all of these needed actions
*
* @access public
* @static
* @param array $processedResponse
* @return array an array of overflow actions
*/
public static function buildOverflowActionsForResponse($processedResponse) {
$overflowActions = array();
// if this response has more results than could be returned and it doesn't have any errors
if($processedResponse['hasMoreResults']) {
// get the action associated with the response
// so it can be sent again
$previousAction = $processedResponse['action'];
// the total number of results available on facebook
$count = $processedResponse['pageData']['count'];
// the max number of results that can be returned in a single request of this type
// note: this changes based on the type of request
$limit = $processedResponse['pageData']['limit'];
$offset = $processedResponse['pageData']['offset'];
$currentOffset = $offset;
// while there are results remaining to generate overflow actions for
while(($currentOffset + $limit) < $count) {
$currentOffset += $limit;
array_push($overflowActions, self::buildOverflowAction($previousAction, $currentOffset));
}
}
return $overflowActions;
}
/**
* buildOverflowAction function.
*
* Passes the updated offset into the params array of a previous action
* So it can be resent as an overflow action
*
* @access protected
* @param array $previousAction
* @param int $currentOffset
* @return array
*/
protected function buildOverflowAction($previousAction, $currentOffset) {
$params = isset($previousAction['params']) ? $previousAction['params'] : array();
$params['offset'] = $currentOffset;
$previousAction['params'] = $params;
return $previousAction;
}
/**
* processResponseQueue function.
*
* Responses can come back from Facebook in a variety of ways. They can be batched or unbatched.
* They can have data detailing counts and limits, or not, among other details. This function takes
* these responses in a variety of different formats, and conditionally processes them so they're all in a uniform
* format that can be used by other functions later on the program
*
* @access public
* @static
* @param array $responseQueue
* @param array $allowErrors
* @return array
*/
public static function processResponseQueue($responseQueue, $actionCount, $allowErrors) {
$allProcessedResponses = __::chain($responseQueue)
// iterate over all the returned response packages
->map(function($responsePackage) use($allowErrors){
// get all of the actions
$actions = $responsePackage['actions'];
// get all of the batches in this response package
$batches = $responsePackage['batches'];
$processedBatches = FB_Request_Monkey::processBatches($batches, $actions, $allowErrors);
return $processedBatches;
// if its a single response
})
->flatten(true)
->value();
// if there are multiple responses, flatten them into a single array
return $allProcessedResponses;
}
public static function processBatches($batches, $actions, $allowErrors) {
$responseIndex = 0;
$processedBatches = __::chain($batches)
// iterate over the responses
->map(function($batch) use(&$responseIndex, $actions, $allowErrors) {
// get the action associated
$action = $actions[$responseIndex];
$processedResponse = FB_Request_Monkey::processBatch($batch, $action, $allowErrors);
$responseIndex++;
return $processedResponse;
})
->value();
return $processedBatches;
}
/**
* addDataFromProcessedResponseToResults function.
*
* Gets the results out of the response and adds them to overall collections of results
* If there's an applied label for the action associated with this response
* add the data to the key for that label
* if there's not, key it as 'data'
*
* @access public
* @static
* @param array $processedResponse
* @param array $results
* @return array
*/
public static function addDataFromProcessedResponseToResults($processedResponse, $results) {
$action = $processedResponse['action'];
// get the label for the current resposne from the associated action
$label = isset($action['label']) ? $action['label'] : 'data';
if(is_array($label)) {
$labels = $label;
} else {
$labels = array($label);
}
// get the needed data out of the response
$result = $processedResponse['data'];
$updatedResults = self::recursivelyAddLabelsToResults($labels, $result, $results);
return $updatedResults;
}
/**
* recursivelyAddLabelsToResults function.
*
* Recursively add the results for the data into the correct labels
*
* @access public
* @static
* @param mixed $labels
* @param mixed $resultToAdd
* @param mixed $currentLevel
* @return void
*/
public static function recursivelyAddLabelsToResults($labels, $resultToAdd, $currentLevel) {
// get the current label
$currentLabel = array_shift($labels);
// if there are any labels left
if(count($labels) > 0) {
// if the key isn't set, set it with an empty array
if(!isset($currentLevel[$currentLabel])) {
$currentLevel[$currentLabel] = array();
}
// because there are more labels, call the function again
$currentLevel[$currentLabel] = self::recursivelyAddLabelsToResults($labels, $resultToAdd, $currentLevel[$currentLabel]);
return $currentLevel;
// this is the last level
} else {
// if the key isn't set, set it with an empty array
if(!isset($currentLevel[$currentLabel])) {
$currentLevel[$currentLabel] = array();
}
// add the results to this level
$currentLevel[$currentLabel] = array_merge($currentLevel[$currentLabel], $resultToAdd);
return $currentLevel;
}
}
/**
* sendAllCalls function.
*
* If there are more than 50 actions being sent, more than one actual request will be sent to Facebook
* This handles sending all of these requests and getting the response, handles some basic processing
* on the response so I can be properly handled further down the line
*
* @access public
* @static
* @param array $formattedCallQueue
* @param array $actions
* @return array
*/
public static function sendAllCalls($formattedCallQueue, $actions) {
$responsePackages = __::map($formattedCallQueue, function($formattedCall) use($actions) {
// is this a batch request or not
$batches = FB_Request_Monkey::transmit($formattedCall);
$responsePackage = array(
'batches' => $batches,
'actions' => $formattedCall['actions'],
);
return $responsePackage;
});
return $responsePackages;
}
/**
* processBatch function.
*
* Identifies if the response has only a single item, gets and stores the count, offset and limit
* gets the data for the response, identifies if the response has more results
*
* @access public
* @static
* @param array $response
* @param boolean $isBatched
* @param array $action
* @return array
*/
public static function processBatch($batch, $action, $allowErrors) {
$processedBatch = array();
$processedBatch['action'] = $action;
$hasOneItem = false;
$returnedDataCount = 0;
$hasErrors = false;
$hasMoreResults = false;
$count = null;
$limit = null;
// check if there are errors in the response
// the wrapper that data goes in is json_encoded in
// batch responses, but not in single responses
$body = json_decode($batch['body'], true);
// if its batched the count is wrapped in a 'body' key
$processedBatch = self::handleErrors($processedBatch, $batch, $action, $allowErrors);
// certain types of requests have their data stored in a data key, others don't
// this handles this different behavior
$data = isset($body['data']) ? $body['data'] : $body;
// if there's only one item, calling count on the data array will return
// an incorrect results,
$processedBatch['hasOneItem'] = $hasOneItem;
// if there's a count and limit specified, get them
// if not, set them as if there's a single result being returned
if(isset($body['count']) && isset($body['limit'])) {
$count = $body['count'];
$returnedDataCount = count($data);
$limit = $body['limit'];
$offset = $body['offset'];
} else {
$count = 1;
$returnedDataCount = 1;
$limit = 1;
$offset = 0;
}
$hasMoreResults = $count > $limit;
// add the needed variables into the response
$processedBatch['pageData'] = array(
'offset' => $offset,
'count' => $count,
'limit' => $limit,
'sentDataCount' => $count - $offset,
'returnedDataCount' => $returnedDataCount,
);
$processedBatch['hasMoreResults'] = $hasMoreResults;
$processedBatch['data'] = $data;
return $processedBatch;
}
public static function handleErrors($processedBatch, $batch, $action, $allowErrors) {
$hasErrors = self::batchHasErrors($batch);
$processedBatch['hasErrors'] = $hasErrors;
// if there's an error in the response
if($hasErrors) {
// if we don't want to throw error
if(!$allowErrors) {
self::generateException($batch, $action);
}
}
return $processedBatch;
}
/**
* batchResponseHasErrors function.
*
* Check if their are error is the batch response
*
* @access public
* @static
* @param array $response
* @return void
*/
public static function batchHasErrors($batch) {
$code = $batch['code'];
$body = json_decode($batch['body'], true);
return $code != 200;
}
/**
* generateException function.
*
* Turn a batch result that contains an error into an exception
*
* @access public
* @static
* @param array $response
* @param array $action
* @return void
*/
public static function generateException($response, $action) {
$output = json_encode($action);
$code = $response['code'];
$responseBody = json_decode($response['body'], true);
// make sure theres actually a message set
$messagePiece = isset($responseBody['error']['message']) ? $responseBody['error']['message'] : "Facebook API $code error";
$message = "$messagePiece in the following action: $output";
throw new Exception($message);
}
/**
* addDataFromProcessedResponsesToResults function.
*
* Get the results from the processed responses array and stored them in the correct format
*
* @access public
* @static
* @param array $processedResponses
* @param array $results
* @return array
*/
public static function addDataFromProcessedResponsesToResults($processedResponses, $results) {
__::each($processedResponses, function($processedResponse) use(&$results) {
$results = FB_Request_Monkey::addDataFromProcessedResponseToResults($processedResponse, $results);
});
return $results;
}
/**
* transmit function.
*
* Use the FB PHP SDK to send the request
*
* @access public
* @static
* @param array $call
* @return array facebook data
*/
public static function transmit($call) {
return self::$sdk->transmit($call);
}
public static function initialize($config) {
if(self::$sdk) {
} else {
self::$sdk = new SDK();
self::$sdk->initialize($config);
}
}
/**
* setSentDataCountToZero function.
*
* At the end of the transmission process, a function will confirm that the
* correct number of results are being returned. In the case of overflow actions,
* you don't want to count the results in that particular response twice, so set
* its number of contained results to zero
*
* @access public
* @static
* @param array $processedResponse
* @return array
*/
public static function setSentDataCountToZero($processedResponse) {
return __::map($processedResponse, function($response){
$response['pageData']['sentDataCount'] = 0;
return $response;
});
}
/**
* checkDataCount function.
*
* Make sure that all of the expected results are being returned,
* If not, throw an exception
*
* @access public
* @static
* @param mixed $processedResponses
* @return void
*/
public static function checkDataCount($processedResponses, $allowErrors) {
if(!$allowErrors) {
$reducedCounts = self::getReducedCounts($processedResponses);
$totalSent = $reducedCounts['total_sent'];
$totalReturned = $reducedCounts['total_returned'];
if($totalSent != $totalReturned) {
$problemAmount = abs($totalSent - $totalReturned);
$messagePiece = $totalSent > $totalReturned ? 'results missing' : 'extra results being returned';
$message = "Result Count Error: There are $problemAmount $messagePiece.";
throw new Exception($message);
}
}
}
/**
* getReducedCounts function.
*
* Reduce the processed responses to the total number of sent and received requests
*
* @access public
* @static
* @param array $processedResponses
* @return array
*/
public static function getReducedCounts($processedResponses) {
$reducedCounts = self::fancyReduce($processedResponses, function($next){
$returns = array();
$returns['total_sent'] = $next['pageData']['sentDataCount'];
$returns['total_returned'] = $next['pageData']['returnedDataCount'];
return $returns;
}, function($prev, $next){
$prev['total_sent'] += $next['pageData']['sentDataCount'];
$prev['total_returned'] += $next['pageData']['returnedDataCount'];
return $prev;
});
return $reducedCounts;
}
/**
* getCallQueue function.
*
* Turns actions into a discrete array of calls to be sent to facebook
* Only 50 actions can be in a single call, and batch calls need to be formatted different than
* single calls. This function handles this processing
*
* @access public
* @static
* @param array $actions
* @return array
*/
public static function getCallQueue($actions) {
$batch = array();
$callQueue = array();
__::each($actions, function($action) use(&$batch, &$callQueue) {
$action = FB_Request_Monkey::keyMapAction($action);
$returns = FB_Request_Monkey::addActionToCallQueue($action, $callQueue, $batch);
$batch = $returns['batch'];
$callQueue = $returns['call_queue'];
});
// if there's a left over batch,
// add it
if(count($batch) > 0) {
array_push($callQueue, $batch);
}
return $callQueue;
}
/**
* keyMapAction function.
*
* Convert the user friendly inputs into the specific keys that facebook needs to use
*
* @access public
* @static
* @param array $action
* @return array
*/
public static function keyMapAction($action) {
$results = __::map($action, function($value, $key){
$mappedKey = FB_Request_Monkey::$actionKeyMap[$key];
return array($mappedKey => $value);
});
return __::flatten($results, true);
}
/**
* addActionToCallQueue function.
*
* Adds an action to a batch, adds the batch to the call queue
*
* @access public
* @static
* @param mixed $action
* @param mixed $callQueue
* @param mixed $batch
* @return void
*/
public static function addActionToCallQueue($action, $callQueue, $batch) {
// add action to batch
array_push($batch, $action);
if(self::batchIsFull($batch)) {
// add the batch to the call queue
array_push($callQueue, $batch);
// reset the batch
$batch = array();
}
$returns = array(
'call_queue' => $callQueue,
'batch' => $batch,
);
return $returns;
}
/**
* batchIsFull function.
*
* Can the batch hold anymore actions?
*
* @access public
* @static
* @param array $batch
* @return boolean
*/
public static function batchIsFull($batch) {
return count($batch) >= self::MAX_ITEMS_IN_BATCH;
}
/**
* formatCallQueue function.
*
* iterate over the call queue and convert it into the format
* needed to be transmitted by the facebook SDK
* @access public
* @static
* @param array $callQueue
* @param string $failsafeToken
* @return array
*/
public static function formatCallQueue($callQueue, $failsafeToken) {
return __::map($callQueue, function($call) use($failsafeToken){
// if there are more than one actions in the call
return FB_Request_Monkey::formatCall($call, $failsafeToken);
});
}
/**
* formatMultiActionCall function.
*
* @access public
* @static
* @param array $call
* @param string $failsafeToken
* @return array
*/
public static function formatCall($call, $failsafeToken) {
return array(
'method' => 'POST',
'relative_url' => '',
'params' => self::getBatchParams($call, $failsafeToken),
'actions' => $call,
);
}
/**
* formatRelativeURL function.
*
* Add a forward slash to a the relative url
* so you don't have have to pointlessly include it
* Don't do this if its an empty url
*
* @access public
* @static
* @param string $relativeURL
* @return string
*/
public static function formatRelativeURL($relativeURL) {
return "/$relativeURL";
}
/**
* getBatchParams function.
*
* Converts actions in an array that will be passed in params
* array of batch calls
*
* @access public
* @static
* @param array $call
* @param string $failsafeToken
* @return array
*/
public static function getBatchParams($call, $failsafeToken) {
$preparedActions = __::map($call, function($action) use($failsafeToken){
$name = isset($action['name']) ? $action['name'] : null;
$params = isset($action['params']) ? $action['params'] : array();
$method = $action['method'];
$relativeURL = $action['relative_url'];
// add the access token to the params
$params['access_token'] = $action['access_token'];
$params = FB_Request_Monkey::handleBoundaryQueriesInParams($relativeURL, $params);
$relativeURL = FB_Request_Monkey::addParamsToRelativeURL($relativeURL, $params);
$preparedAction = array(
'method' => $method,
'relative_url' => FB_Request_Monkey::formatRelativeURL($relativeURL),
);
if($name) {
$preparedAction['name'] = $name;
}
return $preparedAction;
});
$batchParams = array(
'batch' => $preparedActions,
);
if($failsafeToken !== null) {
$batchParams['access_token'] = $failsafeToken;
}
return $batchParams;
}
/**
* handleBoundaryQueries function.
*
* Certain FB queries have erratic behavior that needs to be handled
* in different ways, the processing for handling them happens here
*
* @access public
* @static
* @return void
*/
public static function handleBoundaryQueriesInParams($relativeURL, $params) {
$boundaryQueryMap = array(
'debug_token' => function($relativeURL, $params){
unset($params['access_token']);
return $params;
},
);
if(isset($boundaryQueryMap[$relativeURL])) {
$params = $boundaryQueryMap[$relativeURL]($relativeURL, $params);
}
return $params;
}
/**
* addParamsToRelativeURL function.
*
* the params in batched actions needed to be added directly into the relativeURL
* this adds them to the relative URL
*
* @access public
* @static
* @param string $relativeURL
* @param array $params (default: null)
* @return string
*/
public static function addParamsToRelativeURL($relativeURL, $params) {
if(count($params) > 0) {
$encodedParams = self::jsonEncodeNonStringValues($params);
$convertedParams = self::convertParamsToURL($encodedParams);
$relativeURL .= $convertedParams;
}
return $relativeURL;
}
/**
* convertParamsToURL function.
*
* @access public
* @static
* @param array $params
* @return array
*/
public static function convertParamsToURL($params) {
return '?' . http_build_query($params, null, '&');
}
/**
* jsonEncodeNonStringValues function.
*
* @access public
* @static
* @param array $array
* @return array
*/
public static function jsonEncodeNonStringValues($array) {
foreach ($array as $key => $value) {
if (!is_string($value)) {
$array[$key] = json_encode($value);
}
}
return $array;
}
/**
* fancyReduce function.
*
* @access public
* @static
* @param array $array
* @param closure $onFirst
* @param closure $onNext
* @return mixed
*/
static function fancyReduce($array, $onFirst, $onNext) {
return __::reduce($array, function($prev, $next) use($onFirst, $onNext){
if($prev) {
return call_user_func($onNext, $prev, $next);
} else {
return call_user_func($onFirst, $next);
}
});
}
}