Skip to content

Commit

Permalink
Created InvoiceRequest, Invoice and InvoiceItem classes. Also code cl…
Browse files Browse the repository at this point in the history
…eanup.
  • Loading branch information
mvdgeijn committed Feb 7, 2022
1 parent 4ddd645 commit 8c52751
Show file tree
Hide file tree
Showing 12 changed files with 866 additions and 103 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"require": {
"php": "^7.4|^8.0",
"ext-json": "*",
"ext-bcmath": "*",
"guzzlehttp/guzzle": "^7.0.1"
}
}
71 changes: 71 additions & 0 deletions src/Requests/InvoiceRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Mvdgeijn\Pax8\Requests;

use Mvdgeijn\Pax8\Collections\PaginatedCollection;
use Mvdgeijn\Pax8\Responses\AbstractResponse;
use Mvdgeijn\Pax8\Responses\Invoice;
use Mvdgeijn\Pax8\Responses\InvoiceItem;

class InvoiceRequest extends AbstractRequest
{
/**
* Returns a paginated list of all your invoices filtered by optional parameters
*
* Check https://docs.pax8.com/api/v1#tag/Invoices for possible
* options
*
* @param array $options
* @return PaginatedCollection|null
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function list(array $options = [] ): ?PaginatedCollection
{
$response = $this->getRequest( '/v1/invoices', $options );

if ($response->getStatusCode() == 200)
return Invoice::createFromBody( $response->getBody() );
else
return null;
}

/**
* Returns a single invoice record matching the invoiceId you specify
*
* @param string $invoiceId
* @return Invoice|null
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Exception
*/
public function get(string $invoiceId): ?Invoice
{
$response = $this->getRequest('/v1/invoices/' . $invoiceId);

if ($response->getStatusCode() == 200)
return Invoice::parse(json_decode($response->getBody()));
else
return null;
}

/**
* Returns a paginated list of all your invoice items filtered by invoiceId
* and optional parameters
*
* Check https://docs.pax8.com/api/v1#operation/findPartnerInvoiceItems
* for possible options
*
* @param array $options
* @return PaginatedCollection|null
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function listItems(string $invoiceId, array $options = []): ?PaginatedCollection
{
$response = $this->getRequest( "/v1/invoices/$invoiceId/items", $options );

if ($response->getStatusCode() == 200)
return InvoiceItem::createFromBody( $response->getBody() );
else
return null;
}

}
48 changes: 44 additions & 4 deletions src/Responses/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
use Carbon\Carbon;
use Mvdgeijn\Pax8\Collections\PaginatedCollection;

abstract class AbstractResponse
class AbstractResponse
{
protected string $id;

public static function createFromBody( string $body ): PaginatedCollection
/**
* @param string $body
* @return PaginatedCollection
* @throws \Exception
*/
public static function createFromBody(string $body ): PaginatedCollection
{
$json = json_decode( $body );
$collection = PaginatedCollection::create( $json->page );
Expand All @@ -20,16 +25,51 @@ public static function createFromBody( string $body ): PaginatedCollection
return $collection;
}

abstract public static function parse( object $item ): AbstractResponse;
/**
* @param object $item
* @return AbstractResponse
* @throws \Exception
*/
public static function parse(object $item)
{
$response = new (static::class)();

foreach( $item as $key => $value )
{
$method = 'set' . ucfirst( $key );
if( method_exists($response,$method ) )
{
$response->{$method}($value);
} else
{
throw new \Exception( "$method : $key" );
}
}

return $response;
}

public static function getDate( $date ): Carbon
/**
* @param $date
* @return Carbon
*/
public static function getDate($date ): Carbon
{
if( gettype($date) == "string" )
$date = Carbon::parse( $date );

return $date;
}

/**
* @param $amount
* @return string
*/
public static function getAmount($amount ): string
{
return bcmul( $amount, 1, 2 );
}

/**
* @param mixed $id
* @return AbstractResponse
Expand Down
6 changes: 6 additions & 0 deletions src/Responses/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Mvdgeijn\Pax8\Requests\CompanyRequest;
use Mvdgeijn\Pax8\Requests\ContactRequest;
use Mvdgeijn\Pax8\Requests\InvoiceRequest;
use Mvdgeijn\Pax8\Requests\OrderRequest;
use Mvdgeijn\Pax8\Requests\ProductRequest;
use Mvdgeijn\Pax8\Requests\SubscriptionRequest;
Expand Down Expand Up @@ -60,4 +61,9 @@ public function orderRequest(): OrderRequest
{
return new OrderRequest($this);
}

public function invoiceRequest(): InvoiceRequest
{
return new InvoiceRequest($this);
}
}
28 changes: 8 additions & 20 deletions src/Responses/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,6 @@ class Company extends AbstractResponse

protected ?string $externalId;

public static function parse( object $item ): Company
{
$company = new Company();

$company
->setId($item->id)
->setName($item->name)
->setStreet($item->address->street)
->setCity($item->address->city)
->setZipcode($item->address->postalCode)
->setCountry($item->address->country)
->setWebsite($item->website)
->setExternalId($item->externalId ?? null)
->setStatus($item->status)
->setBillOnBehalfOfEnabled($item->billOnBehalfOfEnabled)
->setOrderApprovalRequired($item->orderApprovalRequired);

return $company;
}

public function createCompany( ): array
{
return [
Expand All @@ -67,6 +47,14 @@ public function createCompany( ): array
];
}

public function setAddress( $address )
{
$this->setStreet( $address->street );
$this->setCity( $address->city );
$this->setZipcode( $address->postalCode );
$this->setCountry( $address->country );
}

/**
* @param mixed $name
* @return Company
Expand Down
14 changes: 0 additions & 14 deletions src/Responses/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,6 @@ class Contact extends AbstractResponse

protected ContactType $types;

public static function parse( object $item ): Contact
{
return (new Contact())
->setId( $item->id )
->setFirstName($item->firstName )
->setLastName($item->lastName )
->setEmail($item->email)
->setPhone($item->phone)
->setPhoneCountryCode($item->phoneCountryCode)
->setPhoneNumber($item->phoneNumber)
->setcreatedDate($item->createdDate)
->setTypes( $item->types );
}

/**
* Create contact array for contact:create request
*
Expand Down
Loading

0 comments on commit 8c52751

Please sign in to comment.