Skip to content

Commit

Permalink
field methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nateiler committed Aug 12, 2019
1 parent 4a467a6 commit 7f18b43
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
76 changes: 61 additions & 15 deletions src/fields/Objects.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(),
Expand All @@ -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 {
Expand Down Expand Up @@ -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(),
Expand All @@ -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;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/fields/ObjectsFieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7f18b43

Please sign in to comment.