From 7f18b4363b38b2d591429747e97cb9ada45af6ad Mon Sep 17 00:00:00 2001 From: nateiler Date: Mon, 12 Aug 2019 07:44:41 -0600 Subject: [PATCH] field methods --- CHANGELOG.md | 3 ++ composer.json | 2 +- src/fields/Objects.php | 76 ++++++++++++++++++++++------ src/fields/ObjectsFieldInterface.php | 24 +++++++++ 4 files changed, 89 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b950ec5..e34388a 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ Changelog ========= +## 1.1.0 - 2019-08-12 +### Added +- Fields have `pushToStripe` and `pullFromStripe` methods which assist in working with the Stripe API. ## 1.0.1 - 2019-05-23 ### Added diff --git a/composer.json b/composer.json index b5b2ace..f465667 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "flipboxfactory/craft-stripe", "description": "Stripe Integration for Craft CMS", - "version": "1.0.1", + "version": "1.1.0", "type": "craft-plugin", "license": "proprietary", "keywords": [ diff --git a/src/fields/Objects.php b/src/fields/Objects.php index b748b34..f011258 100644 --- a/src/fields/Objects.php +++ b/src/fields/Objects.php @@ -148,18 +148,21 @@ public function getCache(): CacheInterface /******************************************* - * SYNC TO + * PUSH TO *******************************************/ /** - * @inheritdoc + * @param ElementInterface $element + * @param string|null $objectId + * @param null $transformer + * @return ApiResource|null * @throws \Throwable */ - public function syncToStripe( + public function pushToStripe( ElementInterface $element, string $objectId = null, $transformer = null - ): bool { + ) { /** @var Element $element */ $id = $objectId ?: $this->resolveObjectIdFromElement($element); @@ -185,13 +188,15 @@ public function syncToStripe( if (empty($objectId)) { if (null === ($objectId = $object->id)) { Stripe::error("Unable to determine object id from response"); - return false; + return null; } - return $this->addAssociation($element, $objectId); + if (!$this->addAssociation($element, $objectId)) { + return null; + } } - return true; + return $object; } catch (\Exception $e) { call_user_func_array( new PopulateElementErrorsFromUpsertResponse(), @@ -203,27 +208,30 @@ public function syncToStripe( ); } - return false; + return null; } /******************************************* - * SYNC FROM + * PULL FROM *******************************************/ /** - * @@inheritdoc + * @param ElementInterface $element + * @param string|null $objectId + * @param null $transformer + * @return ApiResource|null * @throws \Throwable */ - public function syncFromStripe( + public function pullFromStripe( ElementInterface $element, string $objectId = null, $transformer = null - ): bool { + ) { $id = $objectId ?: $this->resolveObjectIdFromElement($element); if (null === $id) { - return false; + return null; } try { @@ -252,7 +260,12 @@ public function syncFromStripe( ); } - return Craft::$app->getElements()->saveElement($element); + if (!Craft::$app->getElements()->saveElement($element)) { + return null; + } + + return $object; + } catch (\Exception $e) { call_user_func_array( new PopulateElementErrorsFromUpsertResponse(), @@ -264,7 +277,40 @@ public function syncFromStripe( ); } - return false; + return null; + } + + + /******************************************* + * SYNC TO + *******************************************/ + + /** + * @inheritdoc + * @throws \Throwable + */ + public function syncToStripe( + ElementInterface $element, + string $objectId = null, + $transformer = null + ): bool { + return $this->pushToStripe($element, $objectId, $transformer) !== null; + } + + /******************************************* + * SYNC FROM + *******************************************/ + + /** + * @@inheritdoc + * @throws \Throwable + */ + public function syncFromStripe( + ElementInterface $element, + string $objectId = null, + $transformer = null + ): bool { + return $this->pullFromStripe($element, $objectId, $transformer) !== null; } /** diff --git a/src/fields/ObjectsFieldInterface.php b/src/fields/ObjectsFieldInterface.php index 2e37809..77b73e7 100644 --- a/src/fields/ObjectsFieldInterface.php +++ b/src/fields/ObjectsFieldInterface.php @@ -26,6 +26,30 @@ public function readFromStripe( string $id ): ApiResource; + /** + * @param ElementInterface $element + * @param string|null $objectId + * @param callable|array|string $transformer + * @return ApiResource|null + */ + public function pullFromStripe( + ElementInterface $element, + string $objectId = null, + $transformer = null + ); + + /** + * @param ElementInterface $element + * @param string|null $objectId + * @param callable|array|string $transformer + * @return ApiResource|null + */ + public function pushToStripe( + ElementInterface $element, + string $objectId = null, + $transformer = null + ) ; + /** * @param ElementInterface $element * @param string|null $objectId