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

More DRY/ Refactoring #56

Merged
merged 6 commits into from
Aug 21, 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
10 changes: 10 additions & 0 deletions src/Contract/BuildMultipleObjectsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace PlugAndPay\Sdk\Contract;

interface BuildMultipleObjectsInterface
{
public static function buildMulti(array $data): array;
}
12 changes: 12 additions & 0 deletions src/Contract/BuildObjectInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace PlugAndPay\Sdk\Contract;

use PlugAndPay\Sdk\Entity\AbstractEntity;

interface BuildObjectInterface
{
public static function build(array $data): array | AbstractEntity;
}
3 changes: 2 additions & 1 deletion src/Director/BodyTo/BodyToAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace PlugAndPay\Sdk\Director\BodyTo;

use PlugAndPay\Sdk\Contract\BuildObjectInterface;
use PlugAndPay\Sdk\Entity\Address;
use PlugAndPay\Sdk\Enum\CountryCode;

class BodyToAddress
class BodyToAddress implements BuildObjectInterface
{
public static function build(array $data): Address
{
Expand Down
22 changes: 6 additions & 16 deletions src/Director/BodyTo/BodyToComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@

use DateTimeImmutable;
use Exception;
use PlugAndPay\Sdk\Contract\BuildMultipleObjectsInterface;
use PlugAndPay\Sdk\Contract\BuildObjectInterface;
use PlugAndPay\Sdk\Entity\Comment;
use PlugAndPay\Sdk\Entity\CommentInternal;
use PlugAndPay\Sdk\Traits\BuildMultipleObjects;

class BodyToComment
class BodyToComment implements BuildObjectInterface, BuildMultipleObjectsInterface
{
use BuildMultipleObjects;

/**
* @throws Exception
*/
Expand All @@ -22,19 +27,4 @@ public static function build(array $data): Comment
->setUpdatedAt(new DateTimeImmutable($data['updated_at']))
->setValue($data['value']);
}

/**
* @param $comments
* @return Comment[]
* @throws Exception
*/
public static function buildMulti($comments): array
{
$result = [];
foreach ($comments as $comment) {
$result[] = self::build($comment);
}

return $result;
}
}
3 changes: 2 additions & 1 deletion src/Director/BodyTo/BodyToContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace PlugAndPay\Sdk\Director\BodyTo;

use PlugAndPay\Sdk\Contract\BuildObjectInterface;
use PlugAndPay\Sdk\Entity\Contact;
use PlugAndPay\Sdk\Enum\TaxExempt;

class BodyToContact
class BodyToContact implements BuildObjectInterface
{
public static function build(array $data): Contact
{
Expand Down
27 changes: 12 additions & 15 deletions src/Director/BodyTo/BodyToDiscounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@

namespace PlugAndPay\Sdk\Director\BodyTo;

use PlugAndPay\Sdk\Contract\BuildMultipleObjectsInterface;
use PlugAndPay\Sdk\Contract\BuildObjectInterface;
use PlugAndPay\Sdk\Entity\Discount;
use PlugAndPay\Sdk\Enum\DiscountType;
use PlugAndPay\Sdk\Traits\BuildMultipleObjects;

class BodyToDiscounts
class BodyToDiscounts implements BuildObjectInterface, BuildMultipleObjectsInterface
{
/**
* @return Discount[]
*/
public static function buildMany(array $data): array
{
$result = [];
foreach ($data as $discount) {
$result[] = (new Discount())
->setAmount((float) $discount['amount'])
->setAmountWithTax((float) $discount['amount_with_tax'])
->setCode($discount['code'])
->setType(DiscountType::from($discount['type']));
}
use BuildMultipleObjects;

return $result;
public static function build(array $discount): Discount
{
return (new Discount())
->setAmount((float) $discount['amount'])
->setAmountWithTax((float) $discount['amount_with_tax'])
->setCode($discount['code'])
->setType(DiscountType::from($discount['type']));
}
}
5 changes: 3 additions & 2 deletions src/Director/BodyTo/BodyToItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

namespace PlugAndPay\Sdk\Director\BodyTo;

use PlugAndPay\Sdk\Contract\BuildObjectInterface;
use PlugAndPay\Sdk\Entity\Item;
use PlugAndPay\Sdk\Entity\ItemInternal;
use PlugAndPay\Sdk\Enum\ItemType;

class BodyToItems
class BodyToItems implements BuildObjectInterface
{
/**
* @return Item[]
Expand All @@ -27,7 +28,7 @@ public static function build(array $data): array
->setType($itemData['type'] ? ItemType::from($itemData['type']) : ItemType::STANDARD);

if (isset($itemData['discounts'])) {
$item->setDiscounts(BodyToDiscounts::buildMany($itemData['discounts']));
$item->setDiscounts(BodyToDiscounts::buildMulti($itemData['discounts']));
}

if (isset($itemData['tax'])) {
Expand Down
23 changes: 7 additions & 16 deletions src/Director/BodyTo/BodyToOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@

use DateTimeImmutable;
use Exception;
use PlugAndPay\Sdk\Contract\BuildMultipleObjectsInterface;
use PlugAndPay\Sdk\Contract\BuildObjectInterface;
use PlugAndPay\Sdk\Entity\Order;
use PlugAndPay\Sdk\Entity\OrderInternal;
use PlugAndPay\Sdk\Enum\InvoiceStatus;
use PlugAndPay\Sdk\Enum\Mode;
use PlugAndPay\Sdk\Enum\Source;
use PlugAndPay\Sdk\Exception\DecodeResponseException;
use PlugAndPay\Sdk\Traits\BuildMultipleObjects;

class BodyToOrder
class BodyToOrder implements BuildObjectInterface, BuildMultipleObjectsInterface
{
use BuildMultipleObjects;

/**
* @throws DecodeResponseException
* @throws Exception
Expand Down Expand Up @@ -49,7 +54,7 @@ public static function build(array $data): Order
}

if (isset($data['total_discounts'])) {
$order->setTotalDiscounts(BodyToDiscounts::buildMany($data['total_discounts']));
$order->setTotalDiscounts(BodyToDiscounts::buildMulti($data['total_discounts']));
}

if (isset($data['taxes'])) {
Expand All @@ -67,20 +72,6 @@ public static function build(array $data): Order
return $order;
}

/**
* @return Order[]
* @throws DecodeResponseException
*/
public static function buildMulti(array $data): array
{
$result = [];
foreach ($data as $order) {
$result[] = self::build($order);
}

return $result;
}

/**
* @throws DecodeResponseException
* @codeCoverageIgnore
Expand Down
3 changes: 2 additions & 1 deletion src/Director/BodyTo/BodyToOrderBilling.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace PlugAndPay\Sdk\Director\BodyTo;

use PlugAndPay\Sdk\Contract\BuildObjectInterface;
use PlugAndPay\Sdk\Entity\OrderBilling;

class BodyToOrderBilling
class BodyToOrderBilling implements BuildObjectInterface
{
public static function build(array $data): OrderBilling
{
Expand Down
3 changes: 2 additions & 1 deletion src/Director/BodyTo/BodyToOrderPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

use DateTimeImmutable;
use Exception;
use PlugAndPay\Sdk\Contract\BuildObjectInterface;
use PlugAndPay\Sdk\Entity\Payment;
use PlugAndPay\Sdk\Entity\PaymentInternal;
use PlugAndPay\Sdk\Enum\PaymentMethod;
use PlugAndPay\Sdk\Enum\PaymentProvider;
use PlugAndPay\Sdk\Enum\PaymentStatus;
use PlugAndPay\Sdk\Enum\PaymentType;

class BodyToOrderPayment
class BodyToOrderPayment implements BuildObjectInterface
{
/**
* @throws Exception
Expand Down
20 changes: 6 additions & 14 deletions src/Director/BodyTo/BodyToPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

namespace PlugAndPay\Sdk\Director\BodyTo;

use PlugAndPay\Sdk\Contract\BuildMultipleObjectsInterface;
use PlugAndPay\Sdk\Contract\BuildObjectInterface;
use PlugAndPay\Sdk\Entity\Price;
use PlugAndPay\Sdk\Entity\PriceInternal;
use PlugAndPay\Sdk\Enum\Interval;
use PlugAndPay\Sdk\Traits\BuildMultipleObjects;

class BodyToPrice
class BodyToPrice implements BuildObjectInterface, BuildMultipleObjectsInterface
{
use BuildMultipleObjects;

public static function build(array $data): Price
{
return (new PriceInternal())
Expand All @@ -22,17 +27,4 @@ public static function build(array $data): Price
->setRegular(BodyToPriceRegular::build($data['regular']))
->setTiers(BodyToPriceTier::buildMulti($data['tiers']));
}

/**
* @return Price[]
*/
public static function buildMulti(array $data): array
{
$prices = [];
foreach ($data as $price) {
$prices[] = self::build($price);
}

return $prices;
}
}
3 changes: 2 additions & 1 deletion src/Director/BodyTo/BodyToPriceFirst.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace PlugAndPay\Sdk\Director\BodyTo;

use PlugAndPay\Sdk\Contract\BuildObjectInterface;
use PlugAndPay\Sdk\Entity\PriceFirst;

class BodyToPriceFirst
class BodyToPriceFirst implements BuildObjectInterface
{
public static function build(array $data): PriceFirst
{
Expand Down
3 changes: 2 additions & 1 deletion src/Director/BodyTo/BodyToPriceOriginal.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace PlugAndPay\Sdk\Director\BodyTo;

use PlugAndPay\Sdk\Contract\BuildObjectInterface;
use PlugAndPay\Sdk\Entity\PriceOriginal;

class BodyToPriceOriginal
class BodyToPriceOriginal implements BuildObjectInterface
{
public static function build(array $data): PriceOriginal
{
Expand Down
3 changes: 2 additions & 1 deletion src/Director/BodyTo/BodyToPriceRegular.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace PlugAndPay\Sdk\Director\BodyTo;

use PlugAndPay\Sdk\Contract\BuildObjectInterface;
use PlugAndPay\Sdk\Entity\PriceRegular;

class BodyToPriceRegular
class BodyToPriceRegular implements BuildObjectInterface
{
public static function build(array $data): PriceRegular
{
Expand Down
20 changes: 6 additions & 14 deletions src/Director/BodyTo/BodyToPriceTier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,20 @@

namespace PlugAndPay\Sdk\Director\BodyTo;

use PlugAndPay\Sdk\Contract\BuildMultipleObjectsInterface;
use PlugAndPay\Sdk\Contract\BuildObjectInterface;
use PlugAndPay\Sdk\Entity\PriceTier;
use PlugAndPay\Sdk\Traits\BuildMultipleObjects;

class BodyToPriceTier
class BodyToPriceTier implements BuildObjectInterface, BuildMultipleObjectsInterface
{
use BuildMultipleObjects;

public static function build(array $data): PriceTier
{
return (new PriceTier())
->setAmount((float) $data['amount'])
->setAmountWithTax((float) $data['amount_with_tax'])
->setQuantity($data['quantity']);
}

/**
* @return PriceTier[]
*/
public static function buildMulti(array $data): array
{
$tiers = [];
foreach ($data as $tier) {
$tiers[] = self::build($tier);
}

return $tiers;
}
}
3 changes: 2 additions & 1 deletion src/Director/BodyTo/BodyToPricingShipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace PlugAndPay\Sdk\Director\BodyTo;

use PlugAndPay\Sdk\Contract\BuildObjectInterface;
use PlugAndPay\Sdk\Entity\Shipping;

class BodyToPricingShipping
class BodyToPricingShipping implements BuildObjectInterface
{
public static function build(array $data): Shipping
{
Expand Down
3 changes: 2 additions & 1 deletion src/Director/BodyTo/BodyToPricingTax.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
namespace PlugAndPay\Sdk\Director\BodyTo;

use function array_key_exists;
use PlugAndPay\Sdk\Contract\BuildObjectInterface;
use PlugAndPay\Sdk\Entity\PricingTax;
use PlugAndPay\Sdk\Entity\PricingTaxInternal;
use PlugAndPay\Sdk\Entity\TaxProfileInternal;
use PlugAndPay\Sdk\Entity\TaxRateInternal;
use PlugAndPay\Sdk\Enum\CountryCode;

class BodyToPricingTax
class BodyToPricingTax implements BuildObjectInterface
{
public static function build(array $data): PricingTax
{
Expand Down
3 changes: 2 additions & 1 deletion src/Director/BodyTo/BodyToPricingTrial.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace PlugAndPay\Sdk\Director\BodyTo;

use PlugAndPay\Sdk\Contract\BuildObjectInterface;
use PlugAndPay\Sdk\Entity\PricingTrial;

class BodyToPricingTrial
class BodyToPricingTrial implements BuildObjectInterface
{
public static function build(array $data): PricingTrial
{
Expand Down
Loading
Loading