Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perks #31

Merged
merged 11 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.idea/
.phpunit.result.cache
test.php
composer.lock
composer.lock
.DS_Store
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Piggy PHP SDK #
With [Piggy's](https://www.piggy.eu/) all-in-one platform you can strengthen loyalty and automate every step. From reward programs, to branded giftcards and smart email marketing - Piggy takes care of it all.

Expand Down
7 changes: 7 additions & 0 deletions src/Http/Traits/SetsOAuthResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Piggy\Api\Resources\OAuth\Loyalty\Rewards\RewardAttributesResource;
use Piggy\Api\Resources\OAuth\Loyalty\Rewards\RewardsResource;
use Piggy\Api\Resources\OAuth\Loyalty\Tokens\LoyaltyTokensResource;
use Piggy\Api\Resources\OAuth\Perks\PerksResource;
use Piggy\Api\Resources\OAuth\PortalSessions\PortalSessionsResource;
use Piggy\Api\Resources\OAuth\PrepaidTransactionsResource;
use Piggy\Api\Resources\OAuth\Shops\ShopsResource;
Expand Down Expand Up @@ -161,6 +162,11 @@ trait SetsOAuthResources
*/
public $tier;

/**
* @var PerksResource
*/
public $perk;

/**
* @var PortalSessionsResource
*/
Expand Down Expand Up @@ -218,6 +224,7 @@ protected function setResources(BaseClient $client): void
$this->voucher = new VouchersResource($client);
$this->brandkit = new BrandkitResource($client);
$this->tier = new TiersResource($client);
$this->perk = new PerksResource($client);
$this->portalSessions = new PortalSessionsResource($client);
$this->forms = new FormsResource($client);
$this->loyaltyTransactionAttributes = new LoyaltyTransactionAttributesResource($client);
Expand Down
20 changes: 20 additions & 0 deletions src/Mappers/Perks/PerkMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Piggy\Api\Mappers\Perks;

use Piggy\Api\Models\Perks\Perk;
use stdClass;

class PerkMapper
{
public function map(stdClass $data): Perk
{
return new Perk(
$data->uuid,
$data->label,
$data->name,
$data->data_type,
$data->options ?? []
);
}
}
25 changes: 25 additions & 0 deletions src/Mappers/Perks/PerksMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Piggy\Api\Mappers\Perks;

use Piggy\Api\Models\Perks\Perk;
use stdClass;

class PerksMapper
{
/**
* @param stdClass[] $data
* @return Perk[]
*/
public function map(array $data): array
{
$mapper = new PerkMapper();

$perks = [];
foreach ($data as $item) {
$perks[] = $mapper->map($item);
}

return $perks;
}
}
148 changes: 148 additions & 0 deletions src/Models/Perks/Perk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

namespace Piggy\Api\Models\Perks;

use GuzzleHttp\Exception\GuzzleException;
use Piggy\Api\ApiClient;
use Piggy\Api\Exceptions\MaintenanceModeException;
use Piggy\Api\Exceptions\PiggyRequestException;
use Piggy\Api\Http\Responses\Response;
use Piggy\Api\StaticMappers\Perks\PerkMapper;
use Piggy\Api\StaticMappers\Perks\PerksMapper;

class Perk
{
/**
* @var string
*/
protected $uuid;

/**
* @var string
*/
protected $label;

/**
* @var string
*/
protected $name;

/**
* @var string
*/
protected $dataType;

/**
* @var mixed[]
*/
protected $options = [];

/**
* @var string
*/
const resourceUri = '/api/v3/oauth/clients/perks';

/**
* @param mixed[] $options
*/
public function __construct(
string $uuid,
string $label,
string $name,
string $dataType,
array $options
) {
$this->uuid = $uuid;
$this->label = $label;
$this->name = $name;
$this->dataType = $dataType;
$this->options = $options;
}

public function getUuid(): string
{
return $this->uuid;
}

public function getName(): string
{
return $this->name;
}

public function getLabel(): string
{
return $this->label;
}

public function getDataType(): string
{
return $this->dataType;
}

/**
* @return mixed[]
*/
public function getOptions(): array
{
return $this->options;
}

/**
* @param mixed[] $params
* @return Perk[]
*
* @throws MaintenanceModeException|GuzzleException|PiggyRequestException
*/
public static function list(array $params = []): array
{
$response = ApiClient::get(self::resourceUri, $params);

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

/**
* @param mixed[] $body
*
* @throws MaintenanceModeException|GuzzleException|PiggyRequestException
*/
public static function create(array $body): Perk
{
$response = ApiClient::post(self::resourceUri, $body);

return PerkMapper::map($response->getData());
}

/**
* @param mixed[] $params
*
* @throws MaintenanceModeException|GuzzleException|PiggyRequestException
*/
public static function get(string $perkUuid, array $params = []): Perk
{
$response = ApiClient::get(self::resourceUri."/$perkUuid", $params);

return PerkMapper::map($response->getData());
}

/**
* @param mixed[] $body
*
* @throws MaintenanceModeException|GuzzleException|PiggyRequestException
*/
public static function update(string $perkUuid, array $body): Perk
{
$response = ApiClient::put(self::resourceUri."/$perkUuid", $body);

return PerkMapper::map($response->getData());
}

/**
* @param mixed[] $params
*
* @throws MaintenanceModeException|GuzzleException|PiggyRequestException
*/
public static function delete(string $perkUuid, array $params = []): Response
{
return ApiClient::delete(self::resourceUri."/$perkUuid", $params);
}
}
92 changes: 92 additions & 0 deletions src/Resources/OAuth/Perks/PerksResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Piggy\Api\Resources\OAuth\Perks;

use Piggy\Api\Exceptions\PiggyRequestException;
use Piggy\Api\Models\Perks\Perk;
use Piggy\Api\Resources\BaseResource;
use Piggy\Api\Mappers\Perks\PerkMapper;
use Piggy\Api\Mappers\Perks\PerksMapper;

class PerksResource extends BaseResource
{
/**
* @var string
*/
protected $resourceUri = '/api/v3/oauth/clients/perks';

/**
* @param mixed[] $params
* @return Perk[]
*
* @throws PiggyRequestException
*/
public function list(array $params = []): array
{
$response = $this->client->get($this->resourceUri, $params);

$mapper = new PerksMapper();

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

/**
* @param mixed[] $options
*
* @throws PiggyRequestException
*/
public function create(string $label, string $name, string $dataType, array $options): Perk
{
$response = $this->client->post($this->resourceUri, [
'label' => $label,
'name' => $name,
'dataType' => $dataType,
'options' => $options,
]);

$mapper = new PerkMapper();

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

/**
* @param mixed[] $params
*
* @throws PiggyRequestException
*/
public function get(string $perkUuid, array $params = []): Perk
{
$response = $this->client->get("$this->resourceUri/$perkUuid", $params);

$mapper = new PerkMapper();

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

/**
* @throws PiggyRequestException
*/
public function update(string $perkUuid, string $label): Perk
{
$response = $this->client->put("$this->resourceUri/$perkUuid", [
'label' => $label,
]);

$mapper = new PerkMapper();

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

/**
* @param mixed[] $params
* @return null
*
* @throws PiggyRequestException
*/
public function delete(string $perkUuid, array $params = [])
{
$response = $this->client->destroy("$this->resourceUri/$perkUuid", $params);

return $response->getData();
}
}
Loading
Loading