From 9a54c32c9bff51d6208739d89cdce06026174935 Mon Sep 17 00:00:00 2001 From: Angelo Date: Thu, 10 Sep 2020 10:58:04 +0200 Subject: [PATCH 01/10] Update ISSUE_TEMPLATE.md --- .github/ISSUE_TEMPLATE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 9e30fcd8..c4681e97 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -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 From 352a1ceaab05a6b7c32fc79562deccccffc37fbe Mon Sep 17 00:00:00 2001 From: Angelo Melonas Date: Thu, 10 Sep 2020 13:48:30 +0200 Subject: [PATCH 02/10] feature/sdk_php#199 Do not call getUser() during UserContext creation. --- src/Context/BunqContext.php | 5 ++++- src/Context/SessionContext.php | 31 ++++++++++++++++++++++++------- src/Context/UserContext.php | 8 ++++---- tests/Context/ApiContextTest.php | 4 ++-- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/Context/BunqContext.php b/src/Context/BunqContext.php index 81d3cf0a..22607d92 100644 --- a/src/Context/BunqContext.php +++ b/src/Context/BunqContext.php @@ -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()->getUserObject() + ); static::$userContext->initMainMonetaryAccount(); } diff --git a/src/Context/SessionContext.php b/src/Context/SessionContext.php index 986d8de9..b35bc041 100644 --- a/src/Context/SessionContext.php +++ b/src/Context/SessionContext.php @@ -4,6 +4,9 @@ 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 DateTime; use JsonSerializable; @@ -21,8 +24,8 @@ class SessionContext implements JsonSerializable /** * 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'; @@ -41,6 +44,11 @@ class SessionContext implements JsonSerializable */ protected $userId; + /** + * @var UserApiKey|UserCompany|UserPaymentServiceProvider|UserPerson + */ + private $user; + /** */ private function __construct() @@ -58,6 +66,7 @@ public static function create(SessionServer $sessionServer): SessionContext $sessionContext->sessionToken = $sessionServer->getSessionToken(); $sessionContext->expiryTime = static::calculateExpiryTime($sessionServer); $sessionContext->userId = $sessionServer->getReferencedUser()->getId(); + $sessionContext->user = $sessionServer->getReferencedUser(); return $sessionContext; } @@ -71,7 +80,7 @@ private static function calculateExpiryTime(SessionServer $sessionServer): DateT { $expiryTime = microtime(true) + static::getSessionTimeout($sessionServer); - return static::microtimeToDateTime($expiryTime); + return static::microTimeToDateTime($expiryTime); } /** @@ -95,11 +104,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); } @@ -114,7 +123,7 @@ 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]; @@ -129,7 +138,7 @@ 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(), ]; } @@ -150,6 +159,14 @@ public function getUserId(): int return $this->userId; } + /** + * @return UserApiKey|UserCompany|UserPaymentServiceProvider|UserPerson + */ + public function getUserObject() + { + return $this->user; + } + /** * @return DateTime */ diff --git a/src/Context/UserContext.php b/src/Context/UserContext.php index 03db8b67..9a48be94 100644 --- a/src/Context/UserContext.php +++ b/src/Context/UserContext.php @@ -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.'; @@ -64,10 +63,12 @@ class UserContext protected $userId; /** + * @param int $userId + * @param $user UserApiKey|UserCompany|UserPaymentServiceProvider|UserPerson */ - public function __construct(int $userId) + public function __construct(int $userId, $user) { - $this->setUser($this->getUserObject()); + $this->setUser($user); $this->userId = $userId; } @@ -123,7 +124,6 @@ public function initMainMonetaryAccount() /** * @return int - * @throws BunqException */ public function getUserId(): int { diff --git a/tests/Context/ApiContextTest.php b/tests/Context/ApiContextTest.php index 383c1765..29c4f45d 100644 --- a/tests/Context/ApiContextTest.php +++ b/tests/Context/ApiContextTest.php @@ -65,12 +65,12 @@ public function testAutoUpdateBunqContext() $contextJson = json_decode($apiContext->toJson(), true); $expireTime = DateTime::createFromFormat( - SessionContext::FORMAT_MICROTIME, + SessionContext::FORMAT_MICRO_TIME, $contextJson[ApiContext::FIELD_SESSION_CONTEXT][SessionContext::FIELD_EXPIRY_TIME] ); $expireTime->sub(new \DateInterval(self::DATE_TIME_INTERVAL_ONE_YEAR)); $contextJson[ApiContext::FIELD_SESSION_CONTEXT][SessionContext::FIELD_EXPIRY_TIME] = - $expireTime->format(SessionContext::FORMAT_MICROTIME); + $expireTime->format(SessionContext::FORMAT_MICRO_TIME); $expiredApiContext = ApiContext::fromJson(json_encode($contextJson)); From 0ac5cf56edfe7caa574998d6123afb0481faa0db Mon Sep 17 00:00:00 2001 From: Angelo Melonas Date: Thu, 10 Sep 2020 15:35:50 +0200 Subject: [PATCH 03/10] feature/sdk_php#199 Renamed function. --- src/Context/BunqContext.php | 2 +- src/Context/SessionContext.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Context/BunqContext.php b/src/Context/BunqContext.php index 22607d92..7ee97966 100644 --- a/src/Context/BunqContext.php +++ b/src/Context/BunqContext.php @@ -37,7 +37,7 @@ public static function loadApiContext(ApiContext $apiContext) static::$apiContext = $apiContext; static::$userContext = new UserContext( $apiContext->getSessionContext()->getUserId(), - $apiContext->getSessionContext()->getUserObject() + $apiContext->getSessionContext()->getUser() ); static::$userContext->initMainMonetaryAccount(); } diff --git a/src/Context/SessionContext.php b/src/Context/SessionContext.php index b35bc041..e19fbefd 100644 --- a/src/Context/SessionContext.php +++ b/src/Context/SessionContext.php @@ -162,7 +162,7 @@ public function getUserId(): int /** * @return UserApiKey|UserCompany|UserPaymentServiceProvider|UserPerson */ - public function getUserObject() + public function getUser() { return $this->user; } From 5052e7bd2e20df5d10d48e7f72d4221636ae24ee Mon Sep 17 00:00:00 2001 From: Angelo Melonas Date: Thu, 10 Sep 2020 18:01:25 +0200 Subject: [PATCH 04/10] feature/sdk_php#199 Saving/Restoring ApiContext should also store/read the context user. --- src/Context/BunqContext.php | 2 +- src/Context/SessionContext.php | 96 ++++++++++++++++++++++++++++++-- src/Context/UserContext.php | 4 +- src/Model/Core/SessionServer.php | 45 +++++++-------- src/Util/ModelUtil.php | 56 +++++++++++++++++-- tests/bunq-psd2-test.conf | 8 ++- 6 files changed, 171 insertions(+), 40 deletions(-) diff --git a/src/Context/BunqContext.php b/src/Context/BunqContext.php index 7ee97966..7e37dfce 100644 --- a/src/Context/BunqContext.php +++ b/src/Context/BunqContext.php @@ -37,7 +37,7 @@ public static function loadApiContext(ApiContext $apiContext) static::$apiContext = $apiContext; static::$userContext = new UserContext( $apiContext->getSessionContext()->getUserId(), - $apiContext->getSessionContext()->getUser() + $apiContext->getSessionContext()->getReferencedUser() ); static::$userContext->initMainMonetaryAccount(); } diff --git a/src/Context/SessionContext.php b/src/Context/SessionContext.php index e19fbefd..7a27558e 100644 --- a/src/Context/SessionContext.php +++ b/src/Context/SessionContext.php @@ -7,6 +7,7 @@ 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; @@ -20,6 +21,10 @@ 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. @@ -45,9 +50,24 @@ class SessionContext implements JsonSerializable protected $userId; /** - * @var UserApiKey|UserCompany|UserPaymentServiceProvider|UserPerson + * @var UserPerson */ - private $user; + protected $userPerson; + + /** + * @var UserCompany + */ + protected $userCompany; + + /** + * @var UserApiKey + */ + protected $userApiKey; + + /** + * @var UserPaymentServiceProvider + */ + protected $userPaymentServiceProvider; /** */ @@ -66,7 +86,10 @@ public static function create(SessionServer $sessionServer): SessionContext $sessionContext->sessionToken = $sessionServer->getSessionToken(); $sessionContext->expiryTime = static::calculateExpiryTime($sessionServer); $sessionContext->userId = $sessionServer->getReferencedUser()->getId(); - $sessionContext->user = $sessionServer->getReferencedUser(); + $sessionContext->userCompany = $sessionServer->getUserCompanyOrNull(); + $sessionContext->userPerson = $sessionServer->getUserPersonOrNull(); + $sessionContext->userApiKey = $sessionServer->getUserApiKeyOrNull(); + $sessionContext->userPaymentServiceProvider = $sessionServer->getUserPaymentServiceProviderOrNull(); return $sessionContext; } @@ -127,10 +150,30 @@ public static function restore(array $sessionContextBody): SessionContext $sessionContextBody[self::FIELD_EXPIRY_TIME] ); $sessionContext->userId = $sessionContextBody[self::FIELD_USER_ID]; + $sessionContext->userPerson = static::getUserOrNull($sessionContextBody, self::FIELD_USER_PERSON); + $sessionContext->userCompany = static::getUserOrNull($sessionContextBody, self::FIELD_USER_COMPANY); + $sessionContext->userApiKey = static::getUserOrNull($sessionContextBody, self::FIELD_USER_API_KEY); + $sessionContext->userPaymentServiceProvider = + static::getUserOrNull($sessionContextBody, self::FIELD_USER_PAYMENT_SERVICE_PROVIDER); return $sessionContext; } + /** + * @param array $sessionContextBody + * @param string $userType + * + * @return UserPerson|UserCompany|UserApiKey|UserPaymentServiceProvider|null + */ + private static function getUserOrNull(array $sessionContextBody, string $userType) + { + if (isset($sessionContextBody[$userType])) { + return UserPerson::createFromJsonString(json_encode($sessionContextBody[$userType])); + } else { + return null; + } + } + /** * @return string[] */ @@ -140,6 +183,10 @@ public function jsonSerialize(): array self::FIELD_TOKEN => $this->getSessionToken()->getToken(), 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(), ]; } @@ -160,11 +207,35 @@ public function getUserId(): int } /** - * @return UserApiKey|UserCompany|UserPaymentServiceProvider|UserPerson + * @return UserPerson|null + */ + public function getUserPersonOrNull() + { + return $this->userPerson; + } + + /** + * @return UserCompany|null + */ + public function getUserCompanyOrNull() + { + return $this->userCompany; + } + + /** + * @return UserApiKey|null */ - public function getUser() + public function getUserApiKeyOrNull() { - return $this->user; + return $this->userApiKey; + } + + /** + * @return UserPaymentServiceProvider|null + */ + public function getUserPaymentServiceProviderOrNull() + { + return $this->userPaymentServiceProvider; } /** @@ -174,4 +245,17 @@ public function getExpiryTime(): DateTime { return $this->expiryTime; } + + /** + * @return UserCompany|UserPerson|UserApiKey|UserPaymentServiceProvider + */ + public function getReferencedUser() + { + return ModelUtil::getReferencedUser( + $this->userPerson, + $this->userCompany, + $this->userApiKey, + $this->userPaymentServiceProvider + ); + } } diff --git a/src/Context/UserContext.php b/src/Context/UserContext.php index 9a48be94..55ee0d59 100644 --- a/src/Context/UserContext.php +++ b/src/Context/UserContext.php @@ -64,12 +64,12 @@ class UserContext /** * @param int $userId - * @param $user UserApiKey|UserCompany|UserPaymentServiceProvider|UserPerson + * @param $user UserPerson|UserCompany|UserApiKey|UserPaymentServiceProvider */ public function __construct(int $userId, $user) { - $this->setUser($user); $this->userId = $userId; + $this->setUser($user); } /** diff --git a/src/Model/Core/SessionServer.php b/src/Model/Core/SessionServer.php index e445d054..866e622c 100644 --- a/src/Model/Core/SessionServer.php +++ b/src/Model/Core/SessionServer.php @@ -2,22 +2,17 @@ namespace bunq\Model\Core; use bunq\Context\ApiContext; -use bunq\Exception\BunqException; use bunq\Http\ApiClient; 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; /** */ class SessionServer extends BunqModel { - /** - * Error constants. - */ - const ERROR_NULL_FIELDS = 'All fields of an extended model or object are null.'; - /** * Field constants. */ @@ -90,46 +85,48 @@ public function getSessionToken(): Token } /** - * @return UserCompany + * @return UserCompany|null */ - public function getUserCompany() + public function getUserCompanyOrNull() { return $this->userCompany; } /** - * @return UserPerson + * @return UserPerson|null */ - public function getUserPerson() + public function getUserPersonOrNull() { return $this->userPerson; } /** - * @return UserApiKey + * @return UserApiKey|null */ - public function getUserApiKey(): UserApiKey + public function getUserApiKeyOrNull() { return $this->userApiKey; } + /** + * @return UserPaymentServiceProvider|null + */ + public function getUserPaymentServiceProviderOrNull() + { + return $this->userPaymentServiceProvider; + } + /** * @return UserCompany|UserPerson|UserApiKey|UserPaymentServiceProvider - * @throws BunqException */ public function getReferencedUser() { - if ((is_null($this->userPerson) && is_null($this->userApiKey)) && !is_null($this->userCompany) && is_null($this->userPaymentServiceProvider)) { - return $this->userCompany; - } elseif (is_null($this->userCompany) && is_null($this->userApiKey) && !is_null($this->userPerson) && is_null($this->userPaymentServiceProvider)) { - return $this->userPerson; - } elseif (is_null($this->userCompany) && is_null($this->userCompany) && !is_null($this->userApiKey) && is_null($this->userPaymentServiceProvider)) { - return $this->userApiKey; - } elseif (is_null($this->userCompany) && is_null($this->userCompany) && is_null($this->userApiKey) && !is_null($this->userPaymentServiceProvider)) { - return $this->userPaymentServiceProvider; - } else { - throw new BunqException(self::ERROR_NULL_FIELDS); - } + return ModelUtil::getReferencedUser( + $this->userPerson, + $this->userCompany, + $this->userApiKey, + $this->userPaymentServiceProvider + ); } /** diff --git a/src/Util/ModelUtil.php b/src/Util/ModelUtil.php index 9694321e..2fa6365e 100644 --- a/src/Util/ModelUtil.php +++ b/src/Util/ModelUtil.php @@ -3,6 +3,10 @@ use bunq\Exception\BunqException; use bunq\Model\Core\BunqModel; +use bunq\Model\Generated\Endpoint\UserApiKey; +use bunq\Model\Generated\Endpoint\UserCompany; +use bunq\Model\Generated\Endpoint\UserPaymentServiceProvider; +use bunq\Model\Generated\Endpoint\UserPerson; /** */ @@ -27,6 +31,7 @@ class ModelUtil * Error constants. */ const ERROR_MODEL_NOT_DEFINED = 'Found model "%s" which is not defined.'; + const ERROR_NULL_FIELDS = 'All fields of an extended model or object are null.'; /** * @param string $json @@ -111,11 +116,13 @@ public static function formatResponseArray(array $array): array */ public static function snakeCaseToCamelCase(string $field): string { - return lcfirst(str_replace( - self::DELIMITER_UNDERSCORE, - self::STRING_EMPTY, - ucwords($field, self::DELIMITER_UNDERSCORE) - )); + return lcfirst( + str_replace( + self::DELIMITER_UNDERSCORE, + self::STRING_EMPTY, + ucwords($field, self::DELIMITER_UNDERSCORE) + ) + ); } /** @@ -127,4 +134,43 @@ public static function camelCaseToSnakeCase(string $field): string { return strtolower(preg_replace(self::REGEX_CAPITAL, self::REPLACEMENT_UNDERSCORE, $field)); } + + /** + * @param UserPerson $userPerson + * @param UserCompany $userCompany + * @param UserApiKey $userApiKey + * @param UserPaymentServiceProvider $userPaymentServiceProvider + * + * @return UserCompany|UserPerson|UserApiKey|UserPaymentServiceProvider + * @throws BunqException + */ + public static function getReferencedUser( + UserPerson $userPerson = null, + UserCompany $userCompany = null, + UserApiKey $userApiKey = null, + UserPaymentServiceProvider $userPaymentServiceProvider = null + ) { + if ((is_null($userPerson) && is_null($userApiKey)) + && !is_null($userCompany) + && is_null($userPaymentServiceProvider)) { + return $userCompany; + } elseif (is_null($userCompany) + && is_null($userApiKey) + && !is_null($userPerson) + && is_null($userPaymentServiceProvider)) { + return $userPerson; + } elseif (is_null($userCompany) + && is_null($userCompany) + && !is_null($userApiKey) + && is_null($userPaymentServiceProvider)) { + return $userApiKey; + } elseif (is_null($userCompany) + && is_null($userCompany) + && is_null($userApiKey) + && !is_null($userPaymentServiceProvider)) { + return $userPaymentServiceProvider; + } else { + throw new BunqException(self::ERROR_NULL_FIELDS); + } + } } diff --git a/tests/bunq-psd2-test.conf b/tests/bunq-psd2-test.conf index 2da0ae81..51d8d656 100644 --- a/tests/bunq-psd2-test.conf +++ b/tests/bunq-psd2-test.conf @@ -11,6 +11,10 @@ "session_context": { "token": "32ff254f211f831f658bc2a10aef93328a15d73adca02f1d53ed023ff2b056e4", "expiry_time": "2019-09-10 01:03:57.107873", - "user_id": 28728 + "user_id": 28728, + "userPerson": null, + "userCompany": null, + "userApiKey": null, + "userPaymentServiceProvider": null } -} \ No newline at end of file +} From c7f3003b9364ed089b318fc9bad8584bdec3e94f Mon Sep 17 00:00:00 2001 From: Angelo Melonas Date: Fri, 11 Sep 2020 12:27:40 +0200 Subject: [PATCH 05/10] feature/sdk_php#199 Resolved PR comment and renamed function. --- src/Context/BunqContext.php | 2 +- src/Context/SessionContext.php | 69 ++++++++++++++++++++++++++------ src/Model/Core/SessionServer.php | 4 +- src/Util/ModelUtil.php | 2 +- 4 files changed, 60 insertions(+), 17 deletions(-) diff --git a/src/Context/BunqContext.php b/src/Context/BunqContext.php index 7e37dfce..058d515b 100644 --- a/src/Context/BunqContext.php +++ b/src/Context/BunqContext.php @@ -37,7 +37,7 @@ public static function loadApiContext(ApiContext $apiContext) static::$apiContext = $apiContext; static::$userContext = new UserContext( $apiContext->getSessionContext()->getUserId(), - $apiContext->getSessionContext()->getReferencedUser() + $apiContext->getSessionContext()->getUserReference() ); static::$userContext->initMainMonetaryAccount(); } diff --git a/src/Context/SessionContext.php b/src/Context/SessionContext.php index 7a27558e..0d1e348d 100644 --- a/src/Context/SessionContext.php +++ b/src/Context/SessionContext.php @@ -85,7 +85,7 @@ 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(); @@ -113,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(); @@ -150,25 +150,68 @@ public static function restore(array $sessionContextBody): SessionContext $sessionContextBody[self::FIELD_EXPIRY_TIME] ); $sessionContext->userId = $sessionContextBody[self::FIELD_USER_ID]; - $sessionContext->userPerson = static::getUserOrNull($sessionContextBody, self::FIELD_USER_PERSON); - $sessionContext->userCompany = static::getUserOrNull($sessionContextBody, self::FIELD_USER_COMPANY); - $sessionContext->userApiKey = static::getUserOrNull($sessionContextBody, self::FIELD_USER_API_KEY); + $sessionContext->userPerson = static::getUserPersonFromSessionOrNull($sessionContextBody); + $sessionContext->userCompany = static::getUserCompanyFromSessionOrNull($sessionContextBody); + $sessionContext->userApiKey = static::getUserApiKeyFromSessionOrNull($sessionContextBody); $sessionContext->userPaymentServiceProvider = - static::getUserOrNull($sessionContextBody, self::FIELD_USER_PAYMENT_SERVICE_PROVIDER); + static::getUserPaymentServiceProviderFromSessionOrNull($sessionContextBody); return $sessionContext; } /** * @param array $sessionContextBody - * @param string $userType * - * @return UserPerson|UserCompany|UserApiKey|UserPaymentServiceProvider|null + * @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 getUserOrNull(array $sessionContextBody, string $userType) + private static function getUserPaymentServiceProviderFromSessionOrNull(array $sessionContextBody) { - if (isset($sessionContextBody[$userType])) { - return UserPerson::createFromJsonString(json_encode($sessionContextBody[$userType])); + if (isset($sessionContextBody[self::FIELD_USER_PAYMENT_SERVICE_PROVIDER])) { + return UserPaymentServiceProvider::createFromJsonString( + json_encode($sessionContextBody[self::FIELD_USER_PAYMENT_SERVICE_PROVIDER]) + ); } else { return null; } @@ -249,9 +292,9 @@ public function getExpiryTime(): DateTime /** * @return UserCompany|UserPerson|UserApiKey|UserPaymentServiceProvider */ - public function getReferencedUser() + public function getUserReference() { - return ModelUtil::getReferencedUser( + return ModelUtil::getUserReference( $this->userPerson, $this->userCompany, $this->userApiKey, diff --git a/src/Model/Core/SessionServer.php b/src/Model/Core/SessionServer.php index 866e622c..7dbe2dc8 100644 --- a/src/Model/Core/SessionServer.php +++ b/src/Model/Core/SessionServer.php @@ -119,9 +119,9 @@ public function getUserPaymentServiceProviderOrNull() /** * @return UserCompany|UserPerson|UserApiKey|UserPaymentServiceProvider */ - public function getReferencedUser() + public function getUserReference() { - return ModelUtil::getReferencedUser( + return ModelUtil::getUserReference( $this->userPerson, $this->userCompany, $this->userApiKey, diff --git a/src/Util/ModelUtil.php b/src/Util/ModelUtil.php index 2fa6365e..4f4b3302 100644 --- a/src/Util/ModelUtil.php +++ b/src/Util/ModelUtil.php @@ -144,7 +144,7 @@ public static function camelCaseToSnakeCase(string $field): string * @return UserCompany|UserPerson|UserApiKey|UserPaymentServiceProvider * @throws BunqException */ - public static function getReferencedUser( + public static function getUserReference( UserPerson $userPerson = null, UserCompany $userCompany = null, UserApiKey $userApiKey = null, From 92b36152ad7f59685545aac13c59239bbef29067 Mon Sep 17 00:00:00 2001 From: Angelo Melonas Date: Fri, 11 Sep 2020 17:59:36 +0200 Subject: [PATCH 06/10] feature/sdk_php#202 Deprecated /sandbox-user --- src/Http/ApiClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/ApiClient.php b/src/Http/ApiClient.php index 66c2aa67..006a17b3 100644 --- a/src/Http/ApiClient.php +++ b/src/Http/ApiClient.php @@ -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'; From e48471350fc8db2aca2cbb333ea2a7f0af64ca77 Mon Sep 17 00:00:00 2001 From: Angelo Melonas Date: Fri, 11 Sep 2020 18:15:46 +0200 Subject: [PATCH 07/10] feature/sdk_php#202 Deprecated /sandbox-user --- src/Model/Core/SandboxUserInternal.php | 12 ++++++------ tests/Context/Psd2ApiContextTest.php | 1 - 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Model/Core/SandboxUserInternal.php b/src/Model/Core/SandboxUserInternal.php index 6c39b394..15bee5c4 100644 --- a/src/Model/Core/SandboxUserInternal.php +++ b/src/Model/Core/SandboxUserInternal.php @@ -4,12 +4,12 @@ use bunq\Context\ApiContext; use bunq\Exception\BunqException; use bunq\Http\ApiClient; -use bunq\Model\Generated\Endpoint\BunqResponseSandboxUser; -use bunq\Model\Generated\Endpoint\SandboxUser; +use bunq\Model\Generated\Endpoint\BunqResponseSandboxUserPerson; +use bunq\Model\Generated\Endpoint\SandboxUserPerson; /** */ -class SandboxUserInternal extends SandboxUser +class SandboxUserInternal extends SandboxUserPerson { /** * Error constants. @@ -20,13 +20,13 @@ class SandboxUserInternal extends SandboxUser * @param string[] $customHeaders * @param ApiContext|null $apiContext * - * @return BunqResponseSandboxUser + * @return BunqResponseSandboxUserPerson * @throws BunqException */ public static function create( array $customHeaders = [], ApiContext $apiContext = null - ): BunqResponseSandboxUser { + ): BunqResponseSandboxUserPerson { if (is_null($apiContext)) { throw new BunqException(self::ERROR_API_CONTEXT_IS_NULL); } @@ -41,7 +41,7 @@ public static function create( $customHeaders ); - return BunqResponseSandboxUser::castFromBunqResponse( + return BunqResponseSandboxUserPerson::castFromBunqResponse( static::fromJson($responseRaw, self::OBJECT_TYPE_POST) ); } diff --git a/tests/Context/Psd2ApiContextTest.php b/tests/Context/Psd2ApiContextTest.php index 497bc95e..33b15251 100644 --- a/tests/Context/Psd2ApiContextTest.php +++ b/tests/Context/Psd2ApiContextTest.php @@ -22,7 +22,6 @@ class Psd2ApiContextTest extends TestCase */ const FILE_TEST_CONFIGURATION = __DIR__ . '/PSD2/bunq-psd2-test.conf'; const FILE_TEST_OAUTH = __DIR__ . '/PSD2/bunq-oauth-test.conf'; - const FILE_TEST_CERTIFICATE = __DIR__ . '/PSD2/certificate.cert'; const FILE_TEST_CERTIFICATE_CHAIN = __DIR__ . '/PSD2/certificate.cert'; const FILE_TEST_PRIVATE_KEY = __DIR__ . '/PSD2/private.pem'; From 6ff446c6d4116bc3145a7b2bdea6a2564038de6c Mon Sep 17 00:00:00 2001 From: Angelo Melonas Date: Thu, 17 Sep 2020 12:37:15 +0200 Subject: [PATCH 08/10] Regenerated code based on the latest tag. --- .../Endpoint/BunqResponseSandboxUser.php | 17 ---- src/Model/Generated/Endpoint/Company.php | 16 ++++ src/Model/Generated/Endpoint/SandboxUser.php | 85 ------------------- 3 files changed, 16 insertions(+), 102 deletions(-) delete mode 100644 src/Model/Generated/Endpoint/BunqResponseSandboxUser.php delete mode 100644 src/Model/Generated/Endpoint/SandboxUser.php diff --git a/src/Model/Generated/Endpoint/BunqResponseSandboxUser.php b/src/Model/Generated/Endpoint/BunqResponseSandboxUser.php deleted file mode 100644 index 4fa91504..00000000 --- a/src/Model/Generated/Endpoint/BunqResponseSandboxUser.php +++ /dev/null @@ -1,17 +0,0 @@ -nameFieldForRequest = $name; @@ -129,6 +140,7 @@ public function __construct( $this->uboFieldForRequest = $ubo; $this->chamberOfCommerceNumberFieldForRequest = $chamberOfCommerceNumber; $this->legalFormFieldForRequest = $legalForm; + $this->subscriptionTypeFieldForRequest = $subscriptionType; $this->avatarUuidFieldForRequest = $avatarUuid; } @@ -142,6 +154,8 @@ public function __construct( * ultimate beneficiary owners. Minimum zero, maximum four. * @param string|null $chamberOfCommerceNumber The company's chamber of * commerce number. + * @param string|null $subscriptionType The subscription type for the + * company. * @param string|null $avatarUuid The public UUID of the company's avatar. * @param string[] $customHeaders * @@ -155,6 +169,7 @@ public static function create( string $legalForm, array $ubo = null, string $chamberOfCommerceNumber = null, + string $subscriptionType = null, string $avatarUuid = null, array $customHeaders = [] ): BunqResponseInt { @@ -172,6 +187,7 @@ public static function create( self::FIELD_UBO => $ubo, self::FIELD_CHAMBER_OF_COMMERCE_NUMBER => $chamberOfCommerceNumber, self::FIELD_LEGAL_FORM => $legalForm, + self::FIELD_SUBSCRIPTION_TYPE => $subscriptionType, self::FIELD_AVATAR_UUID => $avatarUuid, ], $customHeaders diff --git a/src/Model/Generated/Endpoint/SandboxUser.php b/src/Model/Generated/Endpoint/SandboxUser.php deleted file mode 100644 index 0fa42019..00000000 --- a/src/Model/Generated/Endpoint/SandboxUser.php +++ /dev/null @@ -1,85 +0,0 @@ -post( - vsprintf( - self::ENDPOINT_URL_CREATE, - [] - ), - [], - $customHeaders - ); - - return BunqResponseSandboxUser::castFromBunqResponse( - static::fromJson($responseRaw, self::OBJECT_TYPE_POST) - ); - } - - /** - * The API key of the newly created sandbox user. - * - * @return string - */ - public function getApiKey() - { - return $this->apiKey; - } - - /** - * @param string $apiKey - * - * @deprecated User should not be able to set values via setters, use - * constructor. - */ - public function setApiKey($apiKey) - { - $this->apiKey = $apiKey; - } - - /** - * @return bool - */ - public function isAllFieldNull() - { - if (!is_null($this->apiKey)) { - return false; - } - - return true; - } -} From 8931d19d9f985d02cf5a8b07447b5c9091afaf09 Mon Sep 17 00:00:00 2001 From: Angelo Melonas Date: Thu, 17 Sep 2020 12:38:20 +0200 Subject: [PATCH 09/10] Regenerated changelog. --- CHANGELOG.md | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b78a4eb8..a85f03e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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:** @@ -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) @@ -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)) From 9d1e4b7c8caf32bb09413fd033b2e83a5e0ab7be Mon Sep 17 00:00:00 2001 From: Angelo Melonas Date: Thu, 17 Sep 2020 12:38:47 +0200 Subject: [PATCH 10/10] Version bump to 1.14.18. --- src/Http/ApiClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/ApiClient.php b/src/Http/ApiClient.php index 006a17b3..c64da719 100644 --- a/src/Http/ApiClient.php +++ b/src/Http/ApiClient.php @@ -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.