8000 added an exception when calling auth/create and initialize hasn't bee… · oauth-io/sdk-php@6b29f54 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6b29f54

Browse files
committed
added an exception when calling auth/create and initialize hasn't been called
1 parent 516ed17 commit 6b29f54

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace OAuth_io;
4+
5+
class NotInitializedException extends \Exception {
6+
7+
}

src/OAuth_io/OAuth.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
class OAuth {
55

66
private $injector;
7+
private $initialized = false;
78

89
/**
910
*
@@ -50,6 +51,7 @@ public function initialize($key, $secret) {
5051
$this->injector->config['app_key'] = $key;
5152
$this->injector->config['app_secret'] = $secret;
5253
$this->initSession();
54+
$this->initialized = true;
5355
}
5456

5557
public function getAppKey() {
@@ -79,6 +81,9 @@ public function generateStateToken() {
7981
}
8082

8183
public function auth($code) {
84+
if (!$this->initialized) {
85+
throw new NotInitializedException('You must initialize the OAuth instance.');
86+
}
8287
$request = $this->injector->getRequest();
8388
$response = $request->make_request(array(
8489
'method' => 'POST',
@@ -93,14 +98,17 @@ public function auth($code) {
9398
)
9499
));
95100
$result = $response->body;
96-
101+
97102
if (isset($result->provider)) {
98-
$this->injector->session['oauthio']['auth'][$result->provider] = json_decode(json_encode($result), true);
103+
$this->injector->session['oauthio']['auth'][$result->provider] = json_decode(json_encode($result) , true);
99104
}
100-
return json_decode(json_encode($result), true);
105+
return json_decode(json_encode($result) , true);
101106
}
102107

103108
public function create($provider) {
109+
if (!$this->initialized) {
110+
throw new NotInitializedException('You must initialize the OAuth instance.');
111+
}
104112
if (isset($this->injector->session['oauthio']['auth'][$provider])) {
105113
$request = new Request();
106114
$request->initialize($provider);

tests/InitialTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,28 @@ public function testSetOauthdUrlSetsUrlInObject() {
5454
$this->fail('methods are missing');
5555
}
5656
}
57+
58+
public function testCallingAuthOrCreateWhenNotInitializedThrowsAnException() {
59+
if (method_exists($this->oauth, 'initialize') ) {
60+
$passed_auth = false;
61+
$passed_create = false;
62+
try {
63+
$this->oauth->auth('somecode');
64+
} catch (\OAuth_io\NotInitializedException $e) {
65+
$passed_auth = true;
66+
}
67+
68+
try {
69+
$this->oauth->create('somecode');
70+
} catch (\OAuth_io\NotInitializedException $e) {
71+
$passed_create = true;
72+
}
73+
74+
75+
$this->assertTrue($passed_auth);
76+
$this->assertTrue($passed_create);
77+
} else {
78+
$this->fail('methods are missing');
79+
}
80+
}
5781
}

0 commit comments

Comments
 (0)
0