-
-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathParseCloud.php
More file actions
127 lines (115 loc) · 3.48 KB
/
ParseCloud.php
File metadata and controls
127 lines (115 loc) · 3.48 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
126
127
<?php
/**
* Class ParseCloud | Parse/ParseCloud.php
*/
namespace Parse;
/**
* Class ParseCloud - Facilitates calling Parse Cloud functions.
*
* @author Fosco Marotto <fjm@fb.com>
* @package Parse
*/
class ParseCloud
{
protected static $requestCallable;
/**
* Sets a callable to be used for making requests.
*
* This method allows injection of a mockable callable for testing purposes.
*
* @param callable $callable The callable to use for requests.
*/
public static function setRequestCallable(callable $callable)
{
self::$requestCallable = $callable;
}
/**
* Gets the callable used for making requests.
*
* If no callable has been set, it returns the default callable that calls ParseClient::_request.
*
* @return callable The callable used for requests.
*/
protected static function getRequestCallable()
{
if (!self::$requestCallable) {
self::$requestCallable = function($method, $path, $sessionToken = null, $data = null, $useMasterKey = false, $contentType = 'application/json', $returnHeaders = false) {
return ParseClient::_request($method, $path, $sessionToken, $data, $useMasterKey, $contentType, $returnHeaders);
};
}
return self::$requestCallable;
}
/**
* Makes a call to a Cloud function.
*
* @param string $name Cloud function name
* @param array $data Parameters to pass
* @param bool $useMasterKey Whether to use the Master Key
*
* @return mixed
*/
public static function run($name, $data = [], $useMasterKey = false)
{
$sessionToken = null;
if (ParseUser::getCurrentUser()) {
$sessionToken = ParseUser::getCurrentUser()->getSessionToken();
}
$response = call_user_func(
self::getRequestCallable(),
'POST',
'functions/'.$name,
$sessionToken,
json_encode(ParseClient::_encode($data, false)),
$useMasterKey
);
$returnVal = isset($response['result']) ? $response['result'] : [];
return ParseClient::_decode($returnVal);
}
/**
* Gets data for the current set of cloud jobs
*
* @return array
*/
public static function getJobsData()
{
$response = ParseClient::_request(
'GET',
'cloud_code/jobs/data',
null,
null,
true
);
return ParseClient::_decode($response);
}
/**
* Starts a given cloud job, which will process asynchronously
*
* @param string $jobName Name of job to run
* @param array $data Parameters to pass
* @return string Id for tracking job status
*/
public static function startJob($jobName, $data = [])
{
$response = ParseClient::_request(
'POST',
'jobs/'.$jobName,
null,
json_encode(ParseClient::_encode($data, false)),
true,
'application/json',
true
);
return ParseClient::_decode($response)['_headers']['X-Parse-Job-Status-Id'];
}
/**
* Gets job status by id
*
* @param string $jobStatusId Id of the job status to return
* @return array|ParseObject
*/
public static function getJobStatus($jobStatusId)
{
$query = new ParseQuery('_JobStatus');
return $query->get($jobStatusId, true);
}
}