Skip to content

Commit

Permalink
Merge branch 'release/1.14.18'
Browse files Browse the repository at this point in the history
  • Loading branch information
angelomelonas committed Sep 17, 2020
2 parents ae825b7 + 9d1e4b7 commit 22def2d
Show file tree
Hide file tree
Showing 15 changed files with 287 additions and 162 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[//]: # (If there is a traceback please share it in a quote! You can do this by pasting the traceback text, highlighting it and pressing the quote button.)

## SDK version and environment
- Tested on [0.12.4](https://github.com/bunq/sdk_php/releases/tag/0.12.4)
- Tested on [1.14.1](https://github.com/bunq/sdk_php/releases/tag/1.14.1)
- [ ] Sandbox
- [ ] Production

Expand Down
22 changes: 20 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,25 @@

## [Unreleased](https://github.com/bunq/sdk_php/tree/HEAD)

[Full Changelog](https://github.com/bunq/sdk_php/compare/1.14.0...HEAD)
[Full Changelog](https://github.com/bunq/sdk_php/compare/1.14.1...HEAD)

**Fixed bugs:**

- Remove getUserObject call immediately after UserContext creation \(reduce rate-limit hits\) [\#199](https://github.com/bunq/sdk_php/issues/199)
- feature/sdk\_php\#199 Do not call getUser\(\) during UserContext creation [\#200](https://github.com/bunq/sdk_php/pull/200) ([angelomelonas](https://github.com/angelomelonas))

**Closed issues:**

- Replace /sandbox-user with /sandbox-user-person and /sandbox-user-company [\#202](https://github.com/bunq/sdk_php/issues/202)

**Merged pull requests:**

- feature/sdk\_php\#202 Deprecated /sandbox-user [\#203](https://github.com/bunq/sdk_php/pull/203) ([angelomelonas](https://github.com/angelomelonas))
- feature/sdk\_php\#199 Saving/Restoring ApiContext should also store/read the context user [\#201](https://github.com/bunq/sdk_php/pull/201) ([angelomelonas](https://github.com/angelomelonas))

## [1.14.1](https://github.com/bunq/sdk_php/tree/1.14.1) (2020-08-19)

[Full Changelog](https://github.com/bunq/sdk_php/compare/1.14.0...1.14.1)

**Fixed bugs:**

Expand Down Expand Up @@ -385,6 +403,7 @@
**Merged pull requests:**

- Fixes \#36 on php5.6 [\#37](https://github.com/bunq/sdk_php/pull/37) ([OGKevin](https://github.com/OGKevin))
- Added an .gitattributes file. [\#20](https://github.com/bunq/sdk_php/pull/20) ([cafferata](https://github.com/cafferata))

## [0.9.1](https://github.com/bunq/sdk_php/tree/0.9.1) (2017-08-07)

Expand Down Expand Up @@ -420,7 +439,6 @@
- \#28 remove phpstan as it does not support php 5.6 [\#29](https://github.com/bunq/sdk_php/pull/29) ([qurben](https://github.com/qurben))
- \#25 Update directory for composer scripts, also omit version. [\#26](https://github.com/bunq/sdk_php/pull/26) ([qurben](https://github.com/qurben))
- Updated composer.lock [\#23](https://github.com/bunq/sdk_php/pull/23) ([cafferata](https://github.com/cafferata))
- Added an .gitattributes file. [\#20](https://github.com/bunq/sdk_php/pull/20) ([cafferata](https://github.com/cafferata))
- Fixed signature newline handling \(Windows\) [\#9](https://github.com/bunq/sdk_php/pull/9) ([BabyDino](https://github.com/BabyDino))


Expand Down
5 changes: 4 additions & 1 deletion src/Context/BunqContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ private function __construct()
public static function loadApiContext(ApiContext $apiContext)
{
static::$apiContext = $apiContext;
static::$userContext = new UserContext($apiContext->getSessionContext()->getUserId());
static::$userContext = new UserContext(
$apiContext->getSessionContext()->getUserId(),
$apiContext->getSessionContext()->getUserReference()
);
static::$userContext->initMainMonetaryAccount();
}

Expand Down
162 changes: 153 additions & 9 deletions src/Context/SessionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
use bunq\Model\Core\SessionServer;
use bunq\Model\Core\Token;
use bunq\Model\Generated\Endpoint\UserApiKey;
use bunq\Model\Generated\Endpoint\UserCompany;
use bunq\Model\Generated\Endpoint\UserPaymentServiceProvider;
use bunq\Model\Generated\Endpoint\UserPerson;
use bunq\Util\ModelUtil;
use DateTime;
use JsonSerializable;

Expand All @@ -17,12 +21,16 @@ class SessionContext implements JsonSerializable
const FIELD_TOKEN = 'token';
const FIELD_EXPIRY_TIME = 'expiry_time';
const FIELD_USER_ID = 'user_id';
const FIELD_USER_PERSON = 'userPerson';
const FIELD_USER_COMPANY = 'userCompany';
const FIELD_USER_API_KEY = 'userApiKey';
const FIELD_USER_PAYMENT_SERVICE_PROVIDER = 'userPaymentServiceProvider';

/**
* Constants for manipulating expiry timestamp.
*/
const FORMAT_MICROTIME_PARTIAL = 'Y-m-d H:i:s.';
const FORMAT_MICROTIME = 'Y-m-d H:i:s.u';
const FORMAT_MICRO_TIME_PARTIAL = 'Y-m-d H:i:s.';
const FORMAT_MICRO_TIME = 'Y-m-d H:i:s.u';
const MICROSECONDS_IN_SECOND = 1000000;
const FORMAT_MICROSECONDS = '%06d';

Expand All @@ -41,6 +49,26 @@ class SessionContext implements JsonSerializable
*/
protected $userId;

/**
* @var UserPerson
*/
protected $userPerson;

/**
* @var UserCompany
*/
protected $userCompany;

/**
* @var UserApiKey
*/
protected $userApiKey;

/**
* @var UserPaymentServiceProvider
*/
protected $userPaymentServiceProvider;

/**
*/
private function __construct()
Expand All @@ -57,7 +85,11 @@ public static function create(SessionServer $sessionServer): SessionContext
$sessionContext = new static();
$sessionContext->sessionToken = $sessionServer->getSessionToken();
$sessionContext->expiryTime = static::calculateExpiryTime($sessionServer);
$sessionContext->userId = $sessionServer->getReferencedUser()->getId();
$sessionContext->userId = $sessionServer->getUserReference()->getId();
$sessionContext->userCompany = $sessionServer->getUserCompanyOrNull();
$sessionContext->userPerson = $sessionServer->getUserPersonOrNull();
$sessionContext->userApiKey = $sessionServer->getUserApiKeyOrNull();
$sessionContext->userPaymentServiceProvider = $sessionServer->getUserPaymentServiceProviderOrNull();

return $sessionContext;
}
Expand All @@ -71,7 +103,7 @@ private static function calculateExpiryTime(SessionServer $sessionServer): DateT
{
$expiryTime = microtime(true) + static::getSessionTimeout($sessionServer);

return static::microtimeToDateTime($expiryTime);
return static::microTimeToDateTime($expiryTime);
}

/**
Expand All @@ -81,7 +113,7 @@ private static function calculateExpiryTime(SessionServer $sessionServer): DateT
*/
private static function getSessionTimeout(SessionServer $sessionServer): int
{
$user = $sessionServer->getReferencedUser();
$user = $sessionServer->getUserReference();

if ($user instanceof UserApiKey) {
return $user->getRequestedByUser()->getReferencedObject()->getSessionTimeout();
Expand All @@ -95,11 +127,11 @@ private static function getSessionTimeout(SessionServer $sessionServer): int
*
* @return DateTime
*/
private static function microtimeToDateTime(float $microtime): DateTime
private static function microTimeToDateTime(float $microtime): DateTime
{
$microseconds = ($microtime - floor($microtime)) * self::MICROSECONDS_IN_SECOND;
$microsecondsFormatted = sprintf(self::FORMAT_MICROSECONDS, $microseconds);
$dateFormatted = date(self::FORMAT_MICROTIME_PARTIAL . $microsecondsFormatted, $microtime);
$dateFormatted = date(self::FORMAT_MICRO_TIME_PARTIAL . $microsecondsFormatted, $microtime);

return new DateTime($dateFormatted);
}
Expand All @@ -114,23 +146,90 @@ public static function restore(array $sessionContextBody): SessionContext
$sessionContext = new static();
$sessionContext->sessionToken = new Token($sessionContextBody[self::FIELD_TOKEN]);
$sessionContext->expiryTime = Datetime::createFromFormat(
self::FORMAT_MICROTIME,
self::FORMAT_MICRO_TIME,
$sessionContextBody[self::FIELD_EXPIRY_TIME]
);
$sessionContext->userId = $sessionContextBody[self::FIELD_USER_ID];
$sessionContext->userPerson = static::getUserPersonFromSessionOrNull($sessionContextBody);
$sessionContext->userCompany = static::getUserCompanyFromSessionOrNull($sessionContextBody);
$sessionContext->userApiKey = static::getUserApiKeyFromSessionOrNull($sessionContextBody);
$sessionContext->userPaymentServiceProvider =
static::getUserPaymentServiceProviderFromSessionOrNull($sessionContextBody);

return $sessionContext;
}

/**
* @param array $sessionContextBody
*
* @return UserPerson|null
*/
private static function getUserPersonFromSessionOrNull(array $sessionContextBody)
{
if (isset($sessionContextBody[self::FIELD_USER_PERSON])) {
return UserPerson::createFromJsonString(json_encode($sessionContextBody[self::FIELD_USER_PERSON]));
} else {
return null;
}
}

/**
* @param array $sessionContextBody
*
* @return UserCompany|null
*/
private static function getUserCompanyFromSessionOrNull(array $sessionContextBody)
{
if (isset($sessionContextBody[self::FIELD_USER_COMPANY])) {
return UserCompany::createFromJsonString(json_encode($sessionContextBody[self::FIELD_USER_COMPANY]));
} else {
return null;
}
}

/**
* @param array $sessionContextBody
*
* @return UserApiKey|null
*/
private static function getUserApiKeyFromSessionOrNull(array $sessionContextBody)
{
if (isset($sessionContextBody[self::FIELD_USER_API_KEY])) {
return UserApiKey::createFromJsonString(json_encode($sessionContextBody[self::FIELD_USER_API_KEY]));
} else {
return null;
}
}

/**
* @param array $sessionContextBody
*
* @return UserPaymentServiceProvider|null
*/
private static function getUserPaymentServiceProviderFromSessionOrNull(array $sessionContextBody)
{
if (isset($sessionContextBody[self::FIELD_USER_PAYMENT_SERVICE_PROVIDER])) {
return UserPaymentServiceProvider::createFromJsonString(
json_encode($sessionContextBody[self::FIELD_USER_PAYMENT_SERVICE_PROVIDER])
);
} else {
return null;
}
}

/**
* @return string[]
*/
public function jsonSerialize(): array
{
return [
self::FIELD_TOKEN => $this->getSessionToken()->getToken(),
self::FIELD_EXPIRY_TIME => $this->getExpiryTime()->format(self::FORMAT_MICROTIME),
self::FIELD_EXPIRY_TIME => $this->getExpiryTime()->format(self::FORMAT_MICRO_TIME),
self::FIELD_USER_ID => $this->getUserId(),
self::FIELD_USER_COMPANY => $this->getUserCompanyOrNull(),
self::FIELD_USER_PERSON => $this->getUserPersonOrNull(),
self::FIELD_USER_API_KEY => $this->getUserApiKeyOrNull(),
self::FIELD_USER_PAYMENT_SERVICE_PROVIDER => $this->getUserPaymentServiceProviderOrNull(),
];
}

Expand All @@ -150,11 +249,56 @@ public function getUserId(): int
return $this->userId;
}

/**
* @return UserPerson|null
*/
public function getUserPersonOrNull()
{
return $this->userPerson;
}

/**
* @return UserCompany|null
*/
public function getUserCompanyOrNull()
{
return $this->userCompany;
}

/**
* @return UserApiKey|null
*/
public function getUserApiKeyOrNull()
{
return $this->userApiKey;
}

/**
* @return UserPaymentServiceProvider|null
*/
public function getUserPaymentServiceProviderOrNull()
{
return $this->userPaymentServiceProvider;
}

/**
* @return DateTime
*/
public function getExpiryTime(): DateTime
{
return $this->expiryTime;
}

/**
* @return UserCompany|UserPerson|UserApiKey|UserPaymentServiceProvider
*/
public function getUserReference()
{
return ModelUtil::getUserReference(
$this->userPerson,
$this->userCompany,
$this->userApiKey,
$this->userPaymentServiceProvider
);
}
}
8 changes: 4 additions & 4 deletions src/Context/UserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class UserContext
*/
const ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND = 'No active monetary account found.';
const ERROR_COULD_NOT_DETERMINE_USER_ID = 'Both userPerson and userCompany are set, could not determine user id.';
const ERROR_USER_HAS_NOT_BEEN_SET = 'User has not been set.';
const ERROR_PRIMARY_MONETARY_ACCOUNT_HAS_NOT_BEEN_SET = 'Primary monetaryAccount is not set.';
const ERROR_UNEXPECTED_USER_INSTANCE = '"%s" is unexpected user instance.';

Expand Down Expand Up @@ -64,11 +63,13 @@ class UserContext
protected $userId;

/**
* @param int $userId
* @param $user UserPerson|UserCompany|UserApiKey|UserPaymentServiceProvider
*/
public function __construct(int $userId)
public function __construct(int $userId, $user)
{
$this->setUser($this->getUserObject());
$this->userId = $userId;
$this->setUser($user);
}

/**
Expand Down Expand Up @@ -123,7 +124,6 @@ public function initMainMonetaryAccount()

/**
* @return int
* @throws BunqException
*/
public function getUserId(): int
{
Expand Down
4 changes: 2 additions & 2 deletions src/Http/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ApiClient
self::SESSION_SERVER_URL => true,
self::PAYMENT_SERVICE_PROVIDER_CREDENTIAL_URL => true,
];
const SANDBOX_USER_URL = 'sandbox-user';
const SANDBOX_USER_URL = 'sandbox-user-person';
const DEVICE_SERVER_URL = 'device-server';
const INSTALLATION_URL = 'installation';
const SESSION_SERVER_URL = 'session-server';
Expand Down Expand Up @@ -86,7 +86,7 @@ class ApiClient
/**
* User agent constants.
*/
const HEADER_USER_AGENT_BUNQ_SDK_DEFAULT = 'bunq-sdk-php/1.14.1';
const HEADER_USER_AGENT_BUNQ_SDK_DEFAULT = 'bunq-sdk-php/1.14.18';

/**
* Binary request constants.
Expand Down
Loading

0 comments on commit 22def2d

Please sign in to comment.