Skip to content

Commit

Permalink
Prepare services for codegen (#882)
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Mar 14, 2020
1 parent 5ad662d commit 317cf8b
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 279 deletions.
86 changes: 7 additions & 79 deletions lib/Service/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ public function __construct($client)
$this->client = $client;
}

/**
* Returns the base path for requests issued by this service. Must be
* implemented by all concrete subclasses.
*
* @return string the base path for requests issued by this service
*/
abstract public function basePath();

/**
* Gets the client used by this service to send requests.
*
Expand All @@ -40,85 +32,21 @@ public function getClient()
return $this->client;
}

protected function allObjects($params, $opts)
{
return $this->request('get', $this->basePath(), $params, $opts);
}

protected function allNestedObjects($nestedPath, $parentId, $params, $opts)
{
return $this->request('get', $this->baseNestedPath($parentId, $nestedPath), $params, $opts);
}

protected function createObject($params, $opts)
{
return $this->request('post', $this->basePath(), $params, $opts);
}

protected function createNestedObject($nestedPath, $parentId, $params, $opts)
{
return $this->request('post', $this->baseNestedPath($parentId, $nestedPath), $params, $opts);
}

protected function deleteObject($id, $params, $opts)
{
return $this->request('delete', $this->instancePath($id), $params, $opts);
}

protected function deleteNestedObject($nestedPath, $parentId, $id, $params, $opts)
{
return $this->request('delete', $this->instanceNestedPath($parentId, $nestedPath, $id), $params, $opts);
}

protected function retrieveObject($id, $params, $opts)
{
return $this->request('get', $this->instancePath($id), $params, $opts);
}

protected function retrieveNestedObject($nestedPath, $parentId, $id, $params, $opts)
{
return $this->request('get', $this->instanceNestedPath($parentId, $nestedPath, $id), $params, $opts);
}

protected function updateObject($id, $params, $opts)
{
return $this->request('post', $this->instancePath($id), $params, $opts);
}

protected function updateNestedObject($nestedPath, $parentId, $id, $params, $opts)
{
return $this->request('post', $this->instanceNestedPath($parentId, $nestedPath, $id), $params, $opts);
}

protected function request($method, $path, $params, $opts)
{
return $this->getClient()->request($method, $path, $params, $opts);
}

protected function baseNestedPath($parentId, $nestedPath)
{
return $this->instancePath($parentId) . '/' . $nestedPath;
}

protected function instancePath($id)
{
if (null === $id || '' === \trim($id)) {
$msg = 'The resource ID cannot be null or whitespace.';

throw new \Stripe\Exception\InvalidArgumentException($msg);
}

return $this->basePath() . '/' . \urlencode($id);
}

protected function instanceNestedPath($parentId, $nestedPath, $id)
protected function buildPath($basePath, ...$ids)
{
if (null === $id || '' === \trim($id)) {
$msg = 'The resource ID cannot be null or whitespace.';
foreach ($ids as $id) {
if (null === $id || '' === \trim($id)) {
$msg = 'The resource ID cannot be null or whitespace.';

throw new \Stripe\Exception\InvalidArgumentException($msg);
throw new \Stripe\Exception\InvalidArgumentException($msg);
}
}

return $this->baseNestedPath($parentId, $nestedPath) . '/' . \urlencode($id);
return \sprintf($basePath, ...\array_map('\urlencode', $ids));
}
}
45 changes: 20 additions & 25 deletions lib/Service/CouponService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,71 @@

class CouponService extends AbstractService
{
public function basePath()
{
return '/v1/coupons';
}

/**
* List all coupons.
*
* @param array $params
* @param array $opts
* @param null|array $params
* @param null|array|\Stripe\Util\RequestOptions $opts
*
* @return \Stripe\Collection
*/
public function all($params = [], $opts = [])
public function all($params = null, $opts = null)
{
return $this->allObjects($params, $opts);
return $this->request('get', '/v1/coupons', $params, $opts);
}

/**
* Create a coupon.
*
* @param array $params
* @param array $opts
* @param null|array $params
* @param null|array|\Stripe\Util\RequestOptions $opts
*
* @return \Stripe\Coupon
*/
public function create($params = [], $opts = [])
public function create($params = null, $opts = null)
{
return $this->createObject($params, $opts);
return $this->request('post', '/v1/coupons', $params, $opts);
}

/**
* Delete a coupon.
*
* @param string $id
* @param array $params
* @param array $opts
* @param null|array $params
* @param null|array|\Stripe\Util\RequestOptions $opts
*
* @return \Stripe\Coupon
*/
public function delete($id, $params = [], $opts = [])
public function delete($id, $params = null, $opts = null)
{
return $this->deleteObject($id, $params, $opts);
return $this->request('delete', $this->buildPath('/v1/coupons/%s', $id), $params, $opts);
}

/**
* Retrieve a coupon.
*
* @param string $id
* @param array $params
* @param array $opts
* @param null|array $params
* @param null|array|\Stripe\Util\RequestOptions $opts
*
* @return \Stripe\Coupon
*/
public function retrieve($id, $params = [], $opts = [])
public function retrieve($id, $params = null, $opts = null)
{
return $this->retrieveObject($id, $params, $opts);
return $this->request('get', $this->buildPath('/v1/coupons/%s', $id), $params, $opts);
}

/**
* Update a coupon.
*
* @param string $id
* @param array $params
* @param array $opts
* @param null|array $params
* @param null|array|\Stripe\Util\RequestOptions $opts
*
* @return \Stripe\Coupon
*/
public function update($id, $params = [], $opts = [])
public function update($id, $params = null, $opts = null)
{
return $this->updateObject($id, $params, $opts);
return $this->request('post', $this->buildPath('/v1/coupons/%s', $id), $params, $opts);
}
}
Loading

0 comments on commit 317cf8b

Please sign in to comment.