Skip to content

Commit

Permalink
Add missing properties to promotion (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
iDiegoNL authored Oct 31, 2024
1 parent 1ef237e commit bb7fcd8
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 92 deletions.
4 changes: 3 additions & 1 deletion src/Mappers/Vouchers/PromotionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public function map(stdClass $data): Promotion
$data->voucher_limit ?? null,
$data->limit_per_contact ?? null,
$data->expiration_duration ?? null,
$attributes ?? []
$attributes ?? [],
$data->type,
$data->redemptions_per_voucher
);
}
}
26 changes: 25 additions & 1 deletion src/Models/Vouchers/Promotion.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class Promotion
*/
protected $description;

/**
* @var string|null
*/
protected $type;

/**
* @var int|null
*/
protected $redemptions_per_voucher;

/**
* @var int|null
*/
Expand Down Expand Up @@ -61,7 +71,9 @@ public function __construct(
?int $voucher_limit = null,
?int $limit_per_contact = null,
?int $expiration_duration = null,
array $attributes = []
array $attributes = [],
?string $type = null,
?int $redemptions_per_voucher = null
) {
$this->uuid = $uuid;
$this->name = $name;
Expand All @@ -70,6 +82,8 @@ public function __construct(
$this->limit_per_contact = $limit_per_contact;
$this->expiration_duration = $expiration_duration;
$this->attributes = $attributes;
$this->type = $type;
$this->redemptions_per_voucher = $redemptions_per_voucher;
}

public function getName(): string
Expand All @@ -82,6 +96,16 @@ public function getDescription(): string
return $this->description;
}

public function getType(): ?string
{
return $this->type;
}

public function getRedemptionsPerVoucher(): ?int
{
return $this->redemptions_per_voucher;
}

public function getVoucherLimit(): ?int
{
return $this->voucher_limit;
Expand Down
43 changes: 0 additions & 43 deletions src/Resources/OAuth/Vouchers/PromotionsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

namespace Piggy\Api\Resources\OAuth\Vouchers;

use Piggy\Api\Exceptions\PiggyRequestException;
use Piggy\Api\Mappers\Vouchers\PromotionMapper;
use Piggy\Api\Mappers\Vouchers\PromotionsMapper;
use Piggy\Api\Models\Vouchers\Promotion;
use Piggy\Api\Resources\Shared\Vouchers\BasePromotionsResource;

class PromotionsResource extends BasePromotionsResource
Expand All @@ -14,43 +10,4 @@ class PromotionsResource extends BasePromotionsResource
* @var string
*/
protected $resourceUri = '/api/v3/oauth/clients/promotions';

/**
* @return Promotion[]
*
* @throws PiggyRequestException
*/
public function list(int $page = 1, int $limit = 30): array
{
$response = $this->client->get($this->resourceUri, [
'page' => $page,
'limit' => $limit,
]);

$mapper = new PromotionsMapper();

return $mapper->map((array) $response->getData());
}

public function create(string $uuid, string $name, string $description): Promotion
{
$response = $this->client->post($this->resourceUri, [
'uuid' => $uuid,
'name' => $name,
'description' => $description,
]);

$mapper = new PromotionMapper();

return $mapper->map($response->getData());
}

public function findBy(string $promotionUuid): Promotion
{
$response = $this->client->get("$this->resourceUri/$promotionUuid");

$mapper = new PromotionMapper();

return $mapper->map($response->getData());
}
}
43 changes: 0 additions & 43 deletions src/Resources/Register/Vouchers/PromotionsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

namespace Piggy\Api\Resources\Register\Vouchers;

use Piggy\Api\Exceptions\PiggyRequestException;
use Piggy\Api\Mappers\Vouchers\PromotionMapper;
use Piggy\Api\Mappers\Vouchers\PromotionsMapper;
use Piggy\Api\Models\Vouchers\Promotion;
use Piggy\Api\Resources\Shared\Vouchers\BasePromotionsResource;

class PromotionsResource extends BasePromotionsResource
Expand All @@ -14,43 +10,4 @@ class PromotionsResource extends BasePromotionsResource
* @var string
*/
protected $resourceUri = '/api/v3/register/promotions';

/**
* @return Promotion[]
*
* @throws PiggyRequestException
*/
public function list(int $page = 1, int $limit = 30): array
{
$response = $this->client->get($this->resourceUri, [
'page' => $page,
'limit' => $limit,
]);

$mapper = new PromotionsMapper();

return $mapper->map((array) $response->getData());
}

public function create(string $uuid, string $name, string $description): Promotion
{
$response = $this->client->post($this->resourceUri, [
'uuid' => $uuid,
'name' => $name,
'description' => $description,
]);

$mapper = new PromotionMapper();

return $mapper->map($response->getData());
}

public function findBy(string $promotionUuid): Promotion
{
$response = $this->client->get("$this->resourceUri/$promotionUuid");

$mapper = new PromotionMapper();

return $mapper->map($response->getData());
}
}
8 changes: 6 additions & 2 deletions src/Resources/Shared/Vouchers/BasePromotionsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ public function list(int $page = 1, int $limit = 30): array
return $mapper->map((array) $response->getData());
}

public function create(string $uuid, string $name, string $description): Promotion
public function create(string $name, string $description, ?string $type = null, ?int $redemptionsPerVoucher = null, ?int $voucherLimit = null, ?int $limitPerContact = null, ?int $expirationDuration = null): Promotion
{
$response = $this->client->post($this->resourceUri, [
'uuid' => $uuid,
'name' => $name,
'description' => $description,
'type' => $type,
'redemptions_per_voucher' => $redemptionsPerVoucher,
'voucher_limit' => $voucherLimit,
'limit_per_contact' => $limitPerContact,
'expiration_duration' => $expirationDuration
]);

$mapper = new PromotionMapper();
Expand Down
4 changes: 3 additions & 1 deletion src/StaticMappers/Vouchers/PromotionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public static function map($data): Promotion
$data->voucher_limit ?? null,
$data->limit_per_contact ?? null,
$data->expiration_duration ?? null,
isset($data->attributes) ? $attributes : []
isset($data->attributes) ? $attributes : [],
$data->type,
$data->redemptions_per_voucher
);
}
}
18 changes: 17 additions & 1 deletion tests/OAuth/Vouchers/PromotionsResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ public function it_returns_promotion_after_creation()
'uuid' => '1234-abcd-5678-efgh',
'name' => 'Free Pizza',
'description' => 'Get your free pizza slice!',
'type' => 'SINGLE_USE',
'redemptions_per_voucher' => 1
]);

$promotion = $this->mockedClient->promotion->create('1234-abcd-5678-efgh', 'Free Pizza', 'Get your free pizza slice!');
$promotion = $this->mockedClient->promotion->create('Free Pizza', 'Get your free pizza slice!', 'SINGLE_USE', 1);

$this->assertEquals('1234-abcd-5678-efgh', $promotion->getUuid());
$this->assertEquals('Free Pizza', $promotion->getName());
$this->assertEquals('Get your free pizza slice!', $promotion->getDescription());
$this->assertEquals('SINGLE_USE', $promotion->getType());
$this->assertEquals(1, $promotion->getRedemptionsPerVoucher());
}

/**
Expand All @@ -37,6 +41,8 @@ public function it_returns_a_list_of_promotions()
'uuid' => '1234-abcd-5678-efgh',
'name' => 'Free Pizza',
'description' => 'Get your free pizza slice!',
'type' => 'SINGLE_USE',
'redemptions_per_voucher' => 1,
'voucher_limit' => 0,
'limit_per_contact' => null,
'expiration_duration' => null,
Expand All @@ -45,6 +51,8 @@ public function it_returns_a_list_of_promotions()
'uuid' => '9876-ijkl-5432-mnop',
'name' => 'Extra mozzarella',
'description' => 'Get an extra layer of cheese on your pizza!',
'type' => 'SINGLE_USE',
'redemptions_per_voucher' => 1,
'voucher_limit' => 0,
'limit_per_contact' => null,
'expiration_duration' => null,
Expand All @@ -57,10 +65,14 @@ public function it_returns_a_list_of_promotions()
$this->assertEquals('1234-abcd-5678-efgh', $promotions[0]->getUuid());
$this->assertEquals('Free Pizza', $promotions[0]->getName());
$this->assertEquals('Get your free pizza slice!', $promotions[0]->getDescription());
$this->assertEquals('SINGLE_USE', $promotions[0]->getType());
$this->assertEquals(1, $promotions[0]->getRedemptionsPerVoucher());
$this->assertEquals(0, $promotions[0]->getVoucherLimit());

$this->assertEquals('9876-ijkl-5432-mnop', $promotions[1]->getUuid());
$this->assertEquals('Extra mozzarella', $promotions[1]->getName());
$this->assertEquals('SINGLE_USE', $promotions[0]->getType());
$this->assertEquals(1, $promotions[0]->getRedemptionsPerVoucher());
$this->assertEquals('Get an extra layer of cheese on your pizza!', $promotions[1]->getDescription());
$this->assertEquals(0, $promotions[1]->getVoucherLimit());
}
Expand All @@ -74,12 +86,16 @@ public function it_can_find_a_promotion_by_uuid()
'uuid' => '1234-abcd-5678-efgh',
'name' => 'Free Pizza',
'description' => 'Get your free pizza slice!',
'type' => 'SINGLE_USE',
'redemptions_per_voucher' => 1,
]);

$promotion = $this->mockedClient->promotion->findBy('1234-abcd-5678-efgh');

$this->assertEquals('1234-abcd-5678-efgh', $promotion->getUuid());
$this->assertEquals('Free Pizza', $promotion->getName());
$this->assertEquals('Get your free pizza slice!', $promotion->getDescription());
$this->assertEquals('SINGLE_USE', $promotion->getType());
$this->assertEquals(1, $promotion->getRedemptionsPerVoucher());
}
}
14 changes: 14 additions & 0 deletions tests/OAuth/Vouchers/VoucherResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function it_creates_a_voucher()
'uuid' => '89da4cc4-1d51-4041-b829-6225fcdef11d',
'name' => 'Gratis Pizza',
'description' => 'Lekker gratis pizza',
'type' => 'SINGLE_USE',
'redemptions_per_voucher' => 1,
],
]
);
Expand Down Expand Up @@ -81,6 +83,8 @@ public function it_returns_a_list_of_vouchers()
'uuid' => '89da4cc4-1d51-4041-b829-6225fcdef11d',
'name' => 'Gratis Pizza',
'description' => 'Lekker gratis pizza',
'type' => 'SINGLE_USE',
'redemptions_per_voucher' => 1,
],
],
[
Expand All @@ -97,6 +101,8 @@ public function it_returns_a_list_of_vouchers()
'uuid' => '89da4cc4-1d51-4041-b829-6225fcdef11d',
'name' => 'Gratis Pizza',
'description' => 'Lekker gratis pizza',
'type' => 'SINGLE_USE',
'redemptions_per_voucher' => 1,
],
],
]
Expand Down Expand Up @@ -145,6 +151,8 @@ public function it_finds_a_voucher_by_code()
'uuid' => '89da4cc4-1d51-4041-b829-6225fcdef11d',
'name' => 'Gratis Pizza',
'description' => 'Lekker gratis pizza',
'type' => 'SINGLE_USE',
'redemptions_per_voucher' => 1,
],
]
);
Expand Down Expand Up @@ -182,6 +190,8 @@ public function it_can_redeem_a_voucher_that_is_already_linked_to_a_contact()
'uuid' => '89da4cc4-1d51-4041-b829-6225fcdef11d',
'name' => 'Gratis Pizza',
'description' => 'Lekker gratis pizza',
'type' => 'SINGLE_USE',
'redemptions_per_voucher' => 1,
],
'contact' => [
'uuid' => '123',
Expand Down Expand Up @@ -224,6 +234,8 @@ public function it_can_redeem_a_voucher_that_is_locked_by_supplying_a_release_ke
'uuid' => '89da4cc4-1d51-4041-b829-6225fcdef11d',
'name' => 'Gratis Pizza',
'description' => 'Lekker gratis pizza',
'type' => 'SINGLE_USE',
'redemptions_per_voucher' => 1,
],
'contact' => [
'uuid' => '123',
Expand Down Expand Up @@ -266,6 +278,8 @@ public function a_contact_cannot_redeem_a_voucher_that_is_already_linked_to_anot
'uuid' => '89da4cc4-1d51-4041-b829-6225fcdef11d',
'name' => 'Gratis Pizza',
'description' => 'Lekker gratis pizza',
'type' => 'SINGLE_USE',
'redemptions_per_voucher' => 1,
],
'contact' => [
'uuid' => '456',
Expand Down

0 comments on commit bb7fcd8

Please sign in to comment.