Skip to content

Commit 7b83c5d

Browse files
committed
Merge pull request parse-community#198 from phelipealves/master
Add class ParseApp
2 parents ba0400e + 7f24f70 commit 7b83c5d

File tree

4 files changed

+269
-5
lines changed

4 files changed

+269
-5
lines changed

src/Parse/ParseApp.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Parse;
4+
5+
class ParseApp
6+
{
7+
public static $APP_NAME = 'appName';
8+
public static $CLIENT_CLASS_CREATION_ENABLED = 'clientClassCreationEnabled';
9+
public static $CLIENT_PUSH_ENABLED = 'clientPushEnabled';
10+
public static $REQUIRE_REVOCABLE_SESSION = 'requireRevocableSessions';
11+
public static $REVOKE_SESSION_ON_PASSWORD_CHANGE = 'revokeSessionOnPasswordChange';
12+
13+
/**
14+
* To fetch the keys and settings for all of the apps that you are a collaborator on.
15+
*
16+
* @throws ParseException
17+
*
18+
* @return array Containing the keys and settings for your apps.
19+
*/
20+
public static function fetchApps()
21+
{
22+
$result = ParseClient::_request(
23+
'GET',
24+
'apps',
25+
null,
26+
null,
27+
false,
28+
true
29+
);
30+
31+
return $result['results'];
32+
}
33+
34+
/**
35+
* To fetch the keys and settings of a single app.
36+
*
37+
* @param string $application_id
38+
*
39+
* @throws ParseException
40+
*
41+
* @return array Containing the keys and settings for your app.
42+
*/
43+
public static function fetchApp($application_id)
44+
{
45+
$result = ParseClient::_request(
46+
'GET',
47+
'apps/'.$application_id,
48+
null,
49+
null,
50+
false,
51+
true
52+
);
53+
54+
return $result;
55+
}
56+
57+
/**
58+
* Create a new app, that is owned by your account. The only required field for creating an app is the app name.
59+
*
60+
* @param array $data
61+
*
62+
* @throws ParseException
63+
*
64+
* @return array
65+
*/
66+
public static function createApp(array $data)
67+
{
68+
$result = ParseClient::_request(
69+
'POST',
70+
'apps',
71+
null,
72+
json_encode($data),
73+
false,
74+
true
75+
);
76+
77+
return $result;
78+
}
79+
80+
/**
81+
* You can change your app's name, as well as change your app's settings.
82+
*
83+
* @param string $application_id
84+
* @param array $data
85+
*
86+
* @throws ParseException
87+
*
88+
* @return array
89+
*/
90+
public static function updateApp($application_id, array $data)
91+
{
92+
$result = ParseClient::_request(
93+
'PUT',
94+
'apps/'.$application_id,
95+
null,
96+
json_encode($data),
97+
false,
98+
true
99+
);
100+
101+
return $result;
102+
}
103+
}

src/Parse/ParseClient.php

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Parse;
44

55
use Exception;
6+
use InvalidArgumentException;
67
use Parse\Internal\Encodable;
78

89
/**
@@ -50,6 +51,13 @@ final class ParseClient
5051
*/
5152
private static $enableCurlExceptions;
5253

54+
/**
55+
* The account key.
56+
*
57+
* @var string
58+
*/
59+
private static $accountKey;
60+
5361
/**
5462
* The object for managing persistence.
5563
*
@@ -92,8 +100,12 @@ final class ParseClient
92100
* @param string $rest_key Parse REST API Key
93101
* @param string $master_key Parse Master Key
94102
* @param bool $enableCurlExceptions Enable or disable Parse curl exceptions
103+
* @param null $email Parse Account Email
104+
* @param null $password Parse Account Password
105+
*
106+
* @throws Exception
95107
*/
96-
public static function initialize($app_id, $rest_key, $master_key, $enableCurlExceptions = true)
108+
public static function initialize($app_id, $rest_key, $master_key, $enableCurlExceptions = true, $account_key = null)
97109
{
98110
if (!ParseObject::hasRegisteredSubclass('_User')) {
99111
ParseUser::registerSubclass();
@@ -112,6 +124,7 @@ public static function initialize($app_id, $rest_key, $master_key, $enableCurlEx
112124
self::$restKey = $rest_key;
113125
self::$masterKey = $master_key;
114126
self::$enableCurlExceptions = $enableCurlExceptions;
127+
self::$accountKey = $account_key;
115128
if (!static::$storage) {
116129
if (session_status() === PHP_SESSION_ACTIVE) {
117130
self::setStorage(new ParseSessionStorage());
@@ -259,6 +272,7 @@ public static function _encodeArray($value, $allowParseObjects)
259272
* @param null $sessionToken Session Token.
260273
* @param null $data Data to provide with the request.
261274
* @param bool $useMasterKey Whether to use the Master Key.
275+
* @param bool $appRequest App request to create or modify a application
262276
*
263277
* @throws \Exception
264278
*
@@ -269,13 +283,19 @@ public static function _request(
269283
$relativeUrl,
270284
$sessionToken = null,
271285
$data = null,
272-
$useMasterKey = false
286+
$useMasterKey = false,
287+
$appRequest = false
273288
) {
274289
if ($data === '[]') {
275290
$data = '{}';
276291
}
277-
self::assertParseInitialized();
278-
$headers = self::_getRequestHeaders($sessionToken, $useMasterKey);
292+
if ($appRequest) {
293+
self::assertAppInitialized();
294+
$headers = self::_getAppRequestHeaders();
295+
} else {
296+
self::assertParseInitialized();
297+
$headers = self::_getRequestHeaders($sessionToken, $useMasterKey);
298+
}
279299

280300
$url = self::HOST_NAME.'/'.self::API_VERSION.'/'.ltrim($relativeUrl, '/');
281301
if ($method === 'GET' && !empty($data)) {
@@ -374,6 +394,18 @@ private static function assertParseInitialized()
374394
}
375395
}
376396

397+
/**
398+
* @throws Exception
399+
*/
400+
private static function assertAppInitialized()
401+
{
402+
if (self::$accountKey === null) {
403+
throw new Exception(
404+
'You must call Parse::initialize(..., $accountKey) before making any requests.'
405+
);
406+
}
407+
}
408+
377409
/**
378410
* @param $sessionToken
379411
* @param $useMasterKey
@@ -405,6 +437,27 @@ public static function _getRequestHeaders($sessionToken, $useMasterKey)
405437
return $headers;
406438
}
407439

440+
/**
441+
* @return array
442+
*/
443+
public static function _getAppRequestHeaders()
444+
{
445+
if (is_null(self::$accountKey) || empty(self::$accountKey)) {
446+
throw new InvalidArgumentException('A account key is required and can not be null or empty');
447+
} else {
448+
$headers[] = 'X-Parse-Account-Key: '.self::$accountKey;
449+
}
450+
451+
/*
452+
* Set an empty Expect header to stop the 100-continue behavior for post
453+
* data greater than 1024 bytes.
454+
* http://pilif.github.io/2007/02/the-return-of-except-100-continue/
455+
*/
456+
$headers[] = 'Expect: ';
457+
458+
return $headers;
459+
}
460+
408461
/**
409462
* Get remote Parse API url.
410463
*

tests/Parse/Helper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public static function setUp()
1717
ParseClient::initialize(
1818
'app-id-here',
1919
'rest-api-key-here',
20-
'master-key-here'
20+
'master-key-here',
21+
true,
22+
'account-key-here'
2123
);
2224
}
2325

tests/Parse/ParseAppTest.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Parse\Test;
4+
5+
use Parse\ParseApp;
6+
use PHPUnit_Framework_TestCase;
7+
8+
class ParseAppTest extends PHPUnit_Framework_TestCase
9+
{
10+
public static function setUpBeforeClass()
11+
{
12+
Helper::setUp();
13+
}
14+
15+
public function testFetchingApps()
16+
{
17+
self::_createApp(self::_getNewName());
18+
self::_createApp(self::_getNewName());
19+
20+
$apps = ParseApp::fetchApps();
21+
$this->assertGreaterThanOrEqual(2, $apps);
22+
}
23+
24+
public function testFetchSingleApp()
25+
{
26+
$app_created = self::_createApp(self::_getNewName());
27+
28+
$app = ParseApp::fetchApp($app_created['applicationId']);
29+
30+
$this->assertCount(13, $app);
31+
}
32+
33+
public function testFetchNotFound()
34+
{
35+
$invalid_application_id = '1YkU7V110nEDUqU7ctCEbLr6xcgQgdEkePuBaw6P';
36+
37+
$this->setExpectedException('Parse\ParseException', 'requested resource was not found');
38+
ParseApp::fetchApp($invalid_application_id);
39+
}
40+
41+
public function testCreateApp()
42+
{
43+
$app_name = self::_getNewName();
44+
45+
$app = ParseApp::createApp([
46+
'appName' => $app_name,
47+
]);
48+
49+
$this->assertEquals($app_name, $app['appName']);
50+
$this->assertEquals(true, $app['clientClassCreationEnabled']);
51+
$this->assertEquals(false, $app['clientPushEnabled']);
52+
$this->assertEquals(true, $app['requireRevocableSessions']);
53+
$this->assertEquals(true, $app['revokeSessionOnPasswordChange']);
54+
}
55+
56+
public function testNameAlreadyInAccount()
57+
{
58+
$app_name = self::_getNewName();
59+
60+
ParseApp::createApp([
61+
'appName' => $app_name,
62+
]);
63+
64+
$this->setExpectedException('Parse\ParseException', 'App name must not already be used in your account');
65+
ParseApp::createApp([
66+
'appName' => $app_name,
67+
]);
68+
}
69+
70+
public function testUpdateApp()
71+
{
72+
$app_name = self::_getNewName();
73+
$updated_name = self::_getNewName();
74+
$this->assertNotEquals($app_name, $updated_name);
75+
76+
$app = ParseApp::createApp([
77+
'appName' => $app_name,
78+
]);
79+
80+
$updated_app = ParseApp::updateApp($app['applicationId'], [
81+
'appName' => $updated_name,
82+
'clientClassCreationEnabled' => false,
83+
'clientPushEnabled' => true,
84+
'requireRevocableSessions' => false,
85+
'revokeSessionOnPasswordChange' => false,
86+
]);
87+
88+
$this->assertEquals($updated_name, $updated_app['appName']);
89+
$this->assertNotTrue($updated_name['clientClassCreationEnabled']);
90+
$this->assertNotFalse($updated_name['clientPushEnabled']);
91+
$this->assertNotTrue($updated_name['requireRevocableSessions']);
92+
$this->assertNotTrue($updated_name['revokeSessionOnPasswordChange']);
93+
}
94+
95+
private static function _createApp($name)
96+
{
97+
return ParseApp::createApp([
98+
'appName' => $name,
99+
]);
100+
}
101+
102+
private static function _getNewName()
103+
{
104+
return md5(uniqid(rand(), true));
105+
}
106+
}

0 commit comments

Comments
 (0)