Skip to content

Commit

Permalink
Add ContactSubscriptions, SubscriptionTypes and LoyaltyTokens to Regi…
Browse files Browse the repository at this point in the history
…ster resources (#28)

* Add ContactSubscriptions, SubscriptionTypes and LoyaltyTokens to Register resources

* Remove ununsed getId()
  • Loading branch information
iDiegoNL authored Jul 3, 2024
1 parent 5db95f4 commit a6cd576
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 74 deletions.
25 changes: 24 additions & 1 deletion src/Http/Traits/SetsRegisterResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -39,11 +42,21 @@ trait SetsRegisterResources
*/
public $rewards;

/**
* @var LoyaltyTokensResource
*/
public $loyaltyToken;

/**
* @var ContactIdentifiersResource
*/
public $contactIdentifiers;

/**
* @var ContactSubscriptionsResource
*/
public $contactSubscriptions;

/**
* @var GiftcardTransactionsResource
*/
Expand All @@ -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);
}
}
6 changes: 3 additions & 3 deletions src/Models/Contacts/SubscriptionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SubscriptionType
protected $active;

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

Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion src/Resources/BaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

abstract class BaseResource
{
/**
* @var string
*/
protected $resourceUri;

/**
* @var BaseClient
*/
Expand All @@ -17,6 +22,5 @@ abstract class BaseResource
public function __construct(BaseClient $client)
{
$this->client = $client;

}
}
52 changes: 2 additions & 50 deletions src/Resources/OAuth/ContactSubscriptionsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
21 changes: 2 additions & 19 deletions src/Resources/OAuth/SubscriptionTypesResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
13 changes: 13 additions & 0 deletions src/Resources/Register/ContactSubscriptionsResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Piggy\Api\Resources\Register;

use Piggy\Api\Resources\Shared\BaseContactSubscriptionsResource;

class ContactSubscriptionsResource extends BaseContactSubscriptionsResource
{
/**
* @var string
*/
protected $resourceUri = '/api/v3/register/contact-subscriptions';
}
41 changes: 41 additions & 0 deletions src/Resources/Register/Loyalty/Tokens/LoyaltyTokensResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Piggy\Api\Resources\Register\Loyalty\Tokens;

use Piggy\Api\Exceptions\PiggyRequestException;
use Piggy\Api\Mappers\Loyalty\Receptions\CreditReceptionMapper;
use Piggy\Api\Models\Loyalty\Receptions\CreditReception;
use Piggy\Api\Resources\BaseResource;

class LoyaltyTokensResource extends BaseResource
{
/**
* @var string
*/
protected $resourceUri = '/api/v3/register/loyalty-tokens';

/**
* @throws PiggyRequestException
*/
public function claim(string $version, string $uniqueId, int $timestamp, string $hash, string $contactUuid, ?int $credits = null, ?string $unitName = null, ?float $unitValue = null, ?int $shopId = null, ?string $shopUuid = null): CreditReception
{
$inputValues = [
'version' => $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());
}
}
13 changes: 13 additions & 0 deletions src/Resources/Register/SubscriptionTypesResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Piggy\Api\Resources\Register;

use Piggy\Api\Resources\Shared\BaseSubscriptionTypesResource;

class SubscriptionTypesResource extends BaseSubscriptionTypesResource
{
/**
* @var string
*/
protected $resourceUri = '/api/v3/register/subscription-types';
}
56 changes: 56 additions & 0 deletions src/Resources/Shared/BaseContactSubscriptionsResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Piggy\Api\Resources\Shared;

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;

abstract class BaseContactSubscriptionsResource extends BaseResource
{
/**
* @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());
}
}
25 changes: 25 additions & 0 deletions src/Resources/Shared/BaseSubscriptionTypesResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Piggy\Api\Resources\Shared;

use Piggy\Api\Exceptions\PiggyRequestException;
use Piggy\Api\Mappers\Contacts\SubscriptionTypesMapper;
use Piggy\Api\Models\Contacts\SubscriptionType;
use Piggy\Api\Resources\BaseResource;

abstract class BaseSubscriptionTypesResource extends BaseResource
{
/**
* @return SubscriptionType[]
*
* @throws PiggyRequestException
*/
public function list(): array
{
$response = $this->client->get($this->resourceUri);

$mapper = new SubscriptionTypesMapper();

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

0 comments on commit a6cd576

Please sign in to comment.