Skip to content
This repository has been archived by the owner on Sep 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #50 from 1f991/adds-missing-attributes
Browse files Browse the repository at this point in the history
Adds missing optional attributes from Entities
  • Loading branch information
shrink authored Apr 17, 2018
2 parents 4f23605 + 1376dc6 commit c781929
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 22 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "squid/patreon",
"version": "1.1.0",
"version": "1.1.1",
"keywords": ["Patreon"],
"description": "PHP library for interacting with the Patreon API",
"type": "library",
Expand Down
7 changes: 7 additions & 0 deletions src/Patreon/Entities/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

class Address extends Entity
{
/**
* Resource type (from Patreon).
*
* @var string
*/
protected $type = 'address';

/**
* Name of the person to deliver to at this address.
* Example: John Doe
Expand Down
7 changes: 7 additions & 0 deletions src/Patreon/Entities/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

class Campaign extends Entity
{
/**
* Resource type (from Patreon).
*
* @var string
*/
protected $type = 'campaign';

/**
* Timestamp of the campaign creation, ISO 8601 combined date and time in UTC.
* Example: "2017-12-01T16:33:48+00:00"
Expand Down
7 changes: 7 additions & 0 deletions src/Patreon/Entities/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

class Card extends Entity
{
/**
* Resource type (from Patreon).
*
* @var string
*/
protected $type = 'card';

/**
* Type of payment method.
* Example: "card"
Expand Down
19 changes: 9 additions & 10 deletions src/Patreon/Entities/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ abstract class Entity
*/

const PATREON_URL = 'https://patreon.com';

/**
* The type of resource.
*
Expand Down Expand Up @@ -57,27 +58,25 @@ public function getEntityKey(): string
}

/**
* Set the Entity Resource type.
* Set the Entity ID.
*
* @param string $type Type of Resource
* @param string $id ID of the Entity.
*
* @return void
*/
public function setType(string $type): void
public function setId(string $id): void
{
$this->type = $type;
$this->id = (int) $id;
}

/**
* Set the Entity ID.
* Get the Entity Resource Type.
*
* @param string $id ID of the Entity.
*
* @return void
* @return string
*/
public function setId(string $id): void
public function getType(): string
{
$this->id = (int) $id;
return $this->type;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Patreon/Entities/Goal.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

class Goal extends Entity
{
/**
* Resource type (from Patreon).
*
* @var string
*/
protected $type = 'goal';

/**
* Amount in cents at which this goal is achieved.
*
Expand Down
7 changes: 7 additions & 0 deletions src/Patreon/Entities/Pledge.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

class Pledge extends Entity
{
/**
* Resource type (from Patreon).
*
* @var string
*/
protected $type = 'pledge';

/**
* Address associated with the Pledge if Reward `requires_shipping`.
*
Expand Down
7 changes: 7 additions & 0 deletions src/Patreon/Entities/Reward.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

class Reward extends Entity
{
/**
* Resource type (from Patreon).
*
* @var string
*/
protected $type = 'reward';

/**
* Amount in cents required to be eligible for this reward.
* Notes: This is legacy, do not use this. Use `amount_cents`.
Expand Down
7 changes: 7 additions & 0 deletions src/Patreon/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

class User extends Entity
{
/**
* Resource type (from Patreon).
*
* @var string
*/
protected $type = 'user';

/**
* "About Me" text.
* Example: "Hello World!"
Expand Down
1 change: 0 additions & 1 deletion src/Patreon/Hydrator/EntityHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ protected function hydrateResource(ResourceObject $resource): Entity
}

$parent->setId($resource->id());
$parent->setType($resource->type());

$this->saveEntityToCollection($parent);

Expand Down
16 changes: 12 additions & 4 deletions src/Patreon/Resources/Campaigns.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public function getMyCampaign(): Campaign
{
$this->onlyAvailableAuthenticated('getMyCampaign');

return $this->getHydratedEntity('current_user/campaigns');
return $this->getHydratedEntity(
$this->buildUrl('current_user/campaigns', Campaign::class)
);
}

/**
Expand All @@ -27,7 +29,9 @@ public function getMyCampaignWithPledges(): Campaign
{
$this->onlyAvailableAuthenticated('getMyCampaignWithPledges');

$campaign = $this->getHydratedEntity('current_user/campaigns');
$campaign = $this->getHydratedEntity(
$this->buildUrl('current_user/campaigns', Campaign::class)
);

return $this->attachPledges($campaign);
}
Expand All @@ -41,7 +45,9 @@ public function getMyCampaignWithPledges(): Campaign
*/
public function getCampaign(int $id): Campaign
{
return $this->getHydratedEntity("campaigns/{$id}");
return $this->getHydratedEntity(
$this->buildUrl("campaigns/{$id}", Campaign::class)
);
}

/**
Expand All @@ -53,7 +59,9 @@ public function getCampaign(int $id): Campaign
*/
public function getCampaignWithPledges(int $id): Campaign
{
$campaign = $this->getHydratedEntity("campaigns/{$id}");
$campaign = $this->getHydratedEntity(
$this->buildUrl("campaigns/{$id}", Campaign::class)
);

return $this->attachPledges($campaign);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Patreon/Resources/CurrentUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public function get(): User
{
$this->onlyAvailableAuthenticated('get');

return $this->getHydratedEntity('current_user');
return $this->getHydratedEntity(
$this->buildUrl('current_user', User::class)
);
}
}
11 changes: 8 additions & 3 deletions src/Patreon/Resources/Pledges.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Squid\Patreon\Resources;

use Squid\Patreon\Entities\Pledge;
use Squid\Patreon\Exceptions\SortOptionsAreInvalid;
use Tightenco\Collect\Support\Collection;

Expand Down Expand Up @@ -30,9 +31,11 @@ public function getCampaignPledges(int $campaign): Collection
{
$pledges = new Collection;

$url = $this->buildUrl("campaigns/{$campaign}/pledges", Pledge::class);

while (true) {
$page = $this->client->get(
"campaigns/{$campaign}/pledges?" . ($filter ?? null),
$url . '&' . ($filter ?? null),
$this->authenticated
);

Expand Down Expand Up @@ -71,15 +74,17 @@ public function getPageOfCampaignPledges(
throw SortOptionsAreInvalid::options($invalid, self::SORT_OPTIONS);
}

$parameters = http_build_query(
$url = $this->buildUrl(
"campaigns/{$campaign}/pledges",
Pledge::class,
[
'page[count]' => $count,
'sort' => implode(',', $sort) ?: null,
'page[cursor]' => $cursor
]
);

$result = $this->client->get("campaigns/{$campaign}/pledges?{$parameters}");
$result = $this->client->get($url);

return $this->hydrateDocument(
$result->document()
Expand Down
29 changes: 29 additions & 0 deletions src/Patreon/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,33 @@ public function isAuthenticated(): bool
{
return $this->authenticated;
}

/**
* Build a URL including the Entity's fields as we need to request each field
* explicitly.
*
* @param string $path Path of the endpoint
* @param string $entity Entity class
* @param array $parameters Any additional parameters to include in the URL
*
* @return string
*/
protected function buildUrl(
string $path,
string $entity,
array $parameters = []
): string {
$entity = new $entity;

$attributes = implode(array_keys(get_object_vars($entity)), ',');

$parameters = array_merge(
$parameters,
[
"fields[{$entity->getType()}]" => $attributes
]
);

return $path . '?' . http_build_query($parameters);
}
}
3 changes: 3 additions & 0 deletions tests/Patreon/Unit/Resources/PledgesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public function testGetPageOfCampaignPledgesCreatesValidRequestUrl(): void
$path .= '?page%5Bcount%5D=25&sort=-updated';
$path .= '&page%5Bcursor%5D=2017-12-02T15%3A21%3A20.121892%2B00%3A00';

$attributes = implode(array_keys(get_object_vars(new Pledge)), ',');
$path .= '&' . http_build_query(["fields[pledge]" => $attributes]);

$client = $this->createMock(Client::class);
$client->expects($this->once())
->method('get')
Expand Down
35 changes: 33 additions & 2 deletions tests/Patreon/Unit/Resources/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Squid\Patreon\Tests\Unit\Resources;

use Squid\Patreon\Api\Client;
use Squid\Patreon\Entities\Entity;
use Squid\Patreon\Entities\User;
use Squid\Patreon\Exceptions\PatreonReturnedError;
use Squid\Patreon\Resources\Resource;
Expand All @@ -29,14 +30,44 @@ public function testResourceThrowsExceptionWhenApiReturnsError(): void

$this->expectException(PatreonReturnedError::class);

$user = (new ResourceExample($client))->get($response);
(new ExampleResource($client))->get($response);
}

public function testBuildUrlReturnsCorrectUrl(): void
{
$client = $this->createMock(Client::class);
$resource = (new ExampleResource($client));

$this->assertEquals(
'example?fields%5Bentity%5D=a%2Cb%2Cc%2Cd%2Cid',
$resource->url()
);

$this->assertEquals(
'example?x=5&fields%5Bentity%5D=a%2Cb%2Cc%2Cd%2Cid',
$resource->url(['x' => 5])
);
}
}

class ResourceExample extends Resource
class ExampleResource extends Resource
{
public function get(JsonApiResponse $response): void
{
$this->hydrateDocument($response->document());
}

public function url($parameters = []): string
{
return $this->buildUrl('example', ExampleEntity::class, $parameters);
}
}

class ExampleEntity extends Entity
{
protected $type = 'entity';
public $a;
public $b;
public $c;
public $d;
}

0 comments on commit c781929

Please sign in to comment.