diff --git a/src/Http/Traits/SetsRegisterResources.php b/src/Http/Traits/SetsRegisterResources.php index 5a72b37..8286d2b 100644 --- a/src/Http/Traits/SetsRegisterResources.php +++ b/src/Http/Traits/SetsRegisterResources.php @@ -3,8 +3,10 @@ namespace Piggy\Api\Http\Traits; use Piggy\Api\Http\BaseClient; +use Piggy\Api\Resources\Register\Loyalty\Tokens\LoyaltyTokensResource; use Piggy\Api\Resources\Register\ContactIdentifiersResource; use Piggy\Api\Resources\Register\Contacts\ContactsResource; +use Piggy\Api\Resources\Register\ContactSubscriptionsResource; use Piggy\Api\Resources\Register\Giftcards\GiftcardsResource; use Piggy\Api\Resources\Register\Giftcards\GiftcardTransactionsResource; use Piggy\Api\Resources\Register\Loyalty\Program\LoyaltyProgramResource; @@ -13,6 +15,7 @@ use Piggy\Api\Resources\Register\Loyalty\Rewards\RewardsResource; use Piggy\Api\Resources\Register\PrepaidTransactionResource; use Piggy\Api\Resources\Register\Registers\RegisterResource; +use Piggy\Api\Resources\Register\SubscriptionTypesResource; /** * Trait SetsRegisterResources @@ -39,11 +42,21 @@ trait SetsRegisterResources */ public $rewards; + /** + * @var LoyaltyTokensResource + */ + public $loyaltyToken; + /** * @var ContactIdentifiersResource */ public $contactIdentifiers; + /** + * @var ContactSubscriptionsResource + */ + public $contactSubscriptions; + /** * @var GiftcardTransactionsResource */ @@ -64,20 +77,30 @@ trait SetsRegisterResources */ public $creditReceptions; - /** @var LoyaltyProgramResource */ + /** + * @var LoyaltyProgramResource + */ public $loyaltyProgram; + /** + * @var SubscriptionTypesResource + */ + public $subscriptionTypes; + protected function setResources(BaseClient $client): void { $this->registers = new RegisterResource($client); $this->contacts = new ContactsResource($client); $this->giftcards = new GiftcardsResource($client); $this->rewards = new RewardsResource($client); + $this->loyaltyToken = new LoyaltyTokensResource($client); $this->contactIdentifiers = new ContactIdentifiersResource($client); + $this->contactSubscriptions = new ContactSubscriptionsResource($client); $this->giftcardTransactions = new GiftcardTransactionsResource($client); $this->prepaidTransactions = new PrepaidTransactionResource($client); $this->rewardReceptions = new RewardReceptionsResource($client); $this->creditReceptions = new CreditReceptionsResource($client); $this->loyaltyProgram = new LoyaltyProgramResource($client); + $this->subscriptionTypes = new SubscriptionTypesResource($client); } } diff --git a/src/Models/Contacts/SubscriptionType.php b/src/Models/Contacts/SubscriptionType.php index 49e38bb..3316284 100644 --- a/src/Models/Contacts/SubscriptionType.php +++ b/src/Models/Contacts/SubscriptionType.php @@ -31,7 +31,7 @@ class SubscriptionType protected $active; /** - * @var string + * @var string|null */ protected $strategy; @@ -84,12 +84,12 @@ public function setActive(bool $active): void $this->active = $active; } - public function getStrategy(): string + public function getStrategy(): ?string { return $this->strategy; } - public function setStrategy(string $strategy): void + public function setStrategy(?string $strategy): void { $this->strategy = $strategy; } diff --git a/src/Resources/BaseResource.php b/src/Resources/BaseResource.php index 841a051..622353e 100644 --- a/src/Resources/BaseResource.php +++ b/src/Resources/BaseResource.php @@ -6,6 +6,11 @@ abstract class BaseResource { + /** + * @var string + */ + protected $resourceUri; + /** * @var BaseClient */ @@ -17,6 +22,5 @@ abstract class BaseResource public function __construct(BaseClient $client) { $this->client = $client; - } } diff --git a/src/Resources/OAuth/ContactSubscriptionsResource.php b/src/Resources/OAuth/ContactSubscriptionsResource.php index d14c6ab..4949217 100644 --- a/src/Resources/OAuth/ContactSubscriptionsResource.php +++ b/src/Resources/OAuth/ContactSubscriptionsResource.php @@ -2,60 +2,12 @@ namespace Piggy\Api\Resources\OAuth; -use Piggy\Api\Exceptions\PiggyRequestException; -use Piggy\Api\Mappers\Contacts\SubscriptionMapper; -use Piggy\Api\Mappers\Contacts\SubscriptionsMapper; -use Piggy\Api\Models\Contacts\Subscription; -use Piggy\Api\Resources\BaseResource; +use Piggy\Api\Resources\Shared\BaseContactSubscriptionsResource; -class ContactSubscriptionsResource extends BaseResource +class ContactSubscriptionsResource extends BaseContactSubscriptionsResource { /** * @var string */ protected $resourceUri = '/api/v3/oauth/clients/contact-subscriptions'; - - /** - * @return Subscription[] - * - * @throws PiggyRequestException - */ - public function list(string $contactUuid): array - { - $response = $this->client->get("$this->resourceUri/$contactUuid", [ - 'contact_uuid' => $contactUuid, - ]); - - $mapper = new SubscriptionsMapper(); - - return $mapper->map($response->getData()); - } - - /** - * @throws PiggyRequestException - */ - public function subscribe(string $contactUuid, string $subscriptionTypeUuid): Subscription - { - $response = $this->client->put("$this->resourceUri/$contactUuid/subscribe", [ - 'subscription_type_uuid' => $subscriptionTypeUuid, - ]); - - $mapper = new SubscriptionMapper(); - - return $mapper->map($response->getData()); - } - - /** - * @throws PiggyRequestException - */ - public function unsubscribe(string $contactUuid, string $subscriptionTypeUuid): Subscription - { - $response = $this->client->put("$this->resourceUri/$contactUuid/unsubscribe", [ - 'subscription_type_uuid' => $subscriptionTypeUuid, - ]); - - $mapper = new SubscriptionMapper(); - - return $mapper->map($response->getData()); - } } diff --git a/src/Resources/OAuth/SubscriptionTypesResource.php b/src/Resources/OAuth/SubscriptionTypesResource.php index c8d033a..6eef82c 100644 --- a/src/Resources/OAuth/SubscriptionTypesResource.php +++ b/src/Resources/OAuth/SubscriptionTypesResource.php @@ -2,29 +2,12 @@ namespace Piggy\Api\Resources\OAuth; -use Piggy\Api\Exceptions\PiggyRequestException; -use Piggy\Api\Mappers\Contacts\SubscriptionTypesMapper; -use Piggy\Api\Models\Contacts\SubscriptionType; -use Piggy\Api\Resources\BaseResource; +use Piggy\Api\Resources\Shared\BaseSubscriptionTypesResource; -class SubscriptionTypesResource extends BaseResource +class SubscriptionTypesResource extends BaseSubscriptionTypesResource { /** * @var string */ protected $resourceUri = '/api/v3/oauth/clients/subscription-types'; - - /** - * @return SubscriptionType[] - * - * @throws PiggyRequestException - */ - public function list(): array - { - $response = $this->client->get($this->resourceUri); - - $mapper = new SubscriptionTypesMapper(); - - return $mapper->map($response->getData()); - } } diff --git a/src/Resources/Register/ContactSubscriptionsResource.php b/src/Resources/Register/ContactSubscriptionsResource.php new file mode 100644 index 0000000..4dae8d9 --- /dev/null +++ b/src/Resources/Register/ContactSubscriptionsResource.php @@ -0,0 +1,13 @@ + $version, + 'shop_id' => $shopId, + 'shop_uuid' => $shopUuid, + 'unique_id' => $uniqueId, + 'timestamp' => $timestamp, + 'hash' => $hash, + 'contact_uuid' => $contactUuid, + 'credits' => $credits, + 'unit_name' => $unitName, + 'unit_value' => $unitValue, + ]; + + $response = $this->client->post($this->resourceUri, $inputValues); + + $mapper = new CreditReceptionMapper(); + + return $mapper->map($response->getData()); + } +} diff --git a/src/Resources/Register/SubscriptionTypesResource.php b/src/Resources/Register/SubscriptionTypesResource.php new file mode 100644 index 0000000..462d1fa --- /dev/null +++ b/src/Resources/Register/SubscriptionTypesResource.php @@ -0,0 +1,13 @@ +client->get("$this->resourceUri/$contactUuid", [ + 'contact_uuid' => $contactUuid, + ]); + + $mapper = new SubscriptionsMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function subscribe(string $contactUuid, string $subscriptionTypeUuid): Subscription + { + $response = $this->client->put("$this->resourceUri/$contactUuid/subscribe", [ + 'subscription_type_uuid' => $subscriptionTypeUuid, + ]); + + $mapper = new SubscriptionMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function unsubscribe(string $contactUuid, string $subscriptionTypeUuid): Subscription + { + $response = $this->client->put("$this->resourceUri/$contactUuid/unsubscribe", [ + 'subscription_type_uuid' => $subscriptionTypeUuid, + ]); + + $mapper = new SubscriptionMapper(); + + return $mapper->map($response->getData()); + } +} diff --git a/src/Resources/Shared/BaseSubscriptionTypesResource.php b/src/Resources/Shared/BaseSubscriptionTypesResource.php new file mode 100644 index 0000000..a8fd876 --- /dev/null +++ b/src/Resources/Shared/BaseSubscriptionTypesResource.php @@ -0,0 +1,25 @@ +client->get($this->resourceUri); + + $mapper = new SubscriptionTypesMapper(); + + return $mapper->map($response->getData()); + } +}