Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare services for codegen #882

Merged
merged 1 commit into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
ob-stripe marked this conversation as resolved.
Show resolved Hide resolved
}
}
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