diff --git a/src/Mappers/Vouchers/PromotionMapper.php b/src/Mappers/Vouchers/PromotionMapper.php index c642c9a..83f3f9b 100644 --- a/src/Mappers/Vouchers/PromotionMapper.php +++ b/src/Mappers/Vouchers/PromotionMapper.php @@ -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 ); } } diff --git a/src/Models/Vouchers/Promotion.php b/src/Models/Vouchers/Promotion.php index 4ae20d2..dbe0823 100644 --- a/src/Models/Vouchers/Promotion.php +++ b/src/Models/Vouchers/Promotion.php @@ -26,6 +26,16 @@ class Promotion */ protected $description; + /** + * @var string|null + */ + protected $type; + + /** + * @var int|null + */ + protected $redemptions_per_voucher; + /** * @var int|null */ @@ -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; @@ -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 @@ -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; diff --git a/src/Resources/OAuth/Vouchers/PromotionsResource.php b/src/Resources/OAuth/Vouchers/PromotionsResource.php index 3006163..7b8e693 100644 --- a/src/Resources/OAuth/Vouchers/PromotionsResource.php +++ b/src/Resources/OAuth/Vouchers/PromotionsResource.php @@ -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 @@ -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()); - } } diff --git a/src/Resources/Register/Vouchers/PromotionsResource.php b/src/Resources/Register/Vouchers/PromotionsResource.php index 0cc9a19..a7e739c 100644 --- a/src/Resources/Register/Vouchers/PromotionsResource.php +++ b/src/Resources/Register/Vouchers/PromotionsResource.php @@ -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 @@ -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()); - } } diff --git a/src/Resources/Shared/Vouchers/BasePromotionsResource.php b/src/Resources/Shared/Vouchers/BasePromotionsResource.php index 4785e94..d2d616e 100644 --- a/src/Resources/Shared/Vouchers/BasePromotionsResource.php +++ b/src/Resources/Shared/Vouchers/BasePromotionsResource.php @@ -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(); diff --git a/src/StaticMappers/Vouchers/PromotionMapper.php b/src/StaticMappers/Vouchers/PromotionMapper.php index ca5ab45..27de675 100644 --- a/src/StaticMappers/Vouchers/PromotionMapper.php +++ b/src/StaticMappers/Vouchers/PromotionMapper.php @@ -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 ); } } diff --git a/tests/OAuth/Vouchers/PromotionsResourceTest.php b/tests/OAuth/Vouchers/PromotionsResourceTest.php index 1f7c114..6b1e1b7 100644 --- a/tests/OAuth/Vouchers/PromotionsResourceTest.php +++ b/tests/OAuth/Vouchers/PromotionsResourceTest.php @@ -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()); } /** @@ -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, @@ -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, @@ -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()); } @@ -74,6 +86,8 @@ 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'); @@ -81,5 +95,7 @@ public function it_can_find_a_promotion_by_uuid() $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()); } } diff --git a/tests/OAuth/Vouchers/VoucherResourceTest.php b/tests/OAuth/Vouchers/VoucherResourceTest.php index 70aa729..d03f574 100644 --- a/tests/OAuth/Vouchers/VoucherResourceTest.php +++ b/tests/OAuth/Vouchers/VoucherResourceTest.php @@ -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, ], ] ); @@ -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, ], ], [ @@ -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, ], ], ] @@ -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, ], ] ); @@ -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', @@ -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', @@ -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',