-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from bunq/hotifx/bunq_context_class_name
Renamed filename accordingly. #114
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
namespace bunq\Context; | ||
|
||
use bunq\Exception\BunqException; | ||
|
||
class BunqContext | ||
{ | ||
/** | ||
* Error constants. | ||
*/ | ||
const ERROR_API_CONTEXT_HAS_NOT_BEEN_LOADED = 'apiContext has not been loaded.'; | ||
const ERROR_USER_CONTEXT_NOT_LOADED = 'userContext has not been loaded, you can load this by loading apiContext.'; | ||
|
||
/** | ||
* @var ApiContext | ||
*/ | ||
protected static $apiContext; | ||
|
||
/** | ||
* @var UserContext | ||
*/ | ||
protected static $userContext; | ||
|
||
/** | ||
*/ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @param ApiContext $apiContext | ||
*/ | ||
public static function loadApiContext(ApiContext $apiContext) | ||
{ | ||
static::$apiContext = $apiContext; | ||
static::$userContext = new UserContext($apiContext->getSessionContext()->getUserId()); | ||
static::$userContext->initMainMonetaryAccount(); | ||
} | ||
|
||
/** | ||
* @return ApiContext | ||
* @throws BunqException | ||
*/ | ||
public static function getApiContext(): ApiContext | ||
{ | ||
if (is_null(static::$apiContext)) { | ||
throw new BunqException(self::ERROR_API_CONTEXT_HAS_NOT_BEEN_LOADED); | ||
} else { | ||
return static::$apiContext; | ||
} | ||
} | ||
|
||
/** | ||
* @return UserContext | ||
* @throws BunqException | ||
*/ | ||
public static function getUserContext(): UserContext | ||
{ | ||
if (is_null(static::$userContext)) { | ||
throw new BunqException(self::ERROR_USER_CONTEXT_NOT_LOADED); | ||
} else { | ||
return static::$userContext; | ||
} | ||
} | ||
} |