Skip to content

Commit

Permalink
Added Create a Company
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdgeijn committed Feb 4, 2022
1 parent c0f8ae9 commit 10b1b09
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 8 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ $contact = $contacts->get( $company->getId(), $list[0]->getId() );

## Supported resources

| Component | all | get | update | delete |
|-----------|:------------------:|:------------------:|:------:|:------:|
| Companies | :white_check_mark: | :white_check_mark: | | |
| Contacts | :white_check_mark: | :white_check_mark: | | |
| Component | Fetch all | Fetch single | Create | Update | delete |
|-----------|:------------------:|:------------------:|:------------------:|:------:|:------:|
| Companies | :white_check_mark: | :white_check_mark: | :white_check_mark: | | |
| Contacts | :white_check_mark: | :white_check_mark: | | | |


## Links
Expand Down
55 changes: 52 additions & 3 deletions src/Requests/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,79 @@

use Mvdgeijn\Pax8\Responses\AccessToken;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;

class AbstractRequest
{
protected ?string $baseUrl = null;

protected AccessToken $accessToken;

protected ?array $errors = null;

public function __construct( AccessToken &$accessToken )
{
$this->baseUrl = config('pax8.url.api');

$this->accessToken = $accessToken;
}

protected function getRequest( $path, $query = [] )
protected function getRequest( $path, $query = [] ): ?ResponseInterface
{
$client = new Client(['base_uri' => config('pax8.url.api'), 'timeout' => 2.0]);
return $client->request('GET', $path, [
$client = new Client(['base_uri' => $this->baseUrl, 'timeout' => 2.0]);
$response = $client->request('GET', $path, [
'headers' => [
'content-type' => 'application/json',
'Authorization' => 'Bearer ' . $this->accessToken->accessToken
],
'query' => $query
]);

return $this->handleErrors( $response );
}

protected function postRequest( $path, array $data ): ?ResponseInterface
{
$client = new Client(['base_uri' => $this->baseUrl, 'timeout' => 2.0]);
$response = $client->request('POST', $path, [
'headers' => [
'content-type' => 'application/json',
'Authorization' => 'Bearer ' . $this->accessToken->accessToken
],
'form_params' => $data
]);

return $this->handleErrors( $response );
}

private function handleErrors( ResponseInterface &$response ): ?ResponseInterface
{
if( $response->getStatusCode() !== 200 ) {
$this->errors = null;

$data = json_decode($response->getBody());

if ($data !== null) {
switch ($response->getStatusCode()) {
case 400:
case 401:
$this->errors = [$data->error . ": " . $data->error_description];
break;
case 404:
case 422:
$this->errors = $data->errors;
break;
}
}

return null;
}

return $response;
}

public function getErrors( ): ?array
{
return $this->errors;
}
}
9 changes: 9 additions & 0 deletions src/Requests/CompanyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ public function get( string $id ): ?Company
return Company::parseCompany(json_decode( $response->getBody() ) );
else
return null;
}

public function create( Company $company ): ?Company
{
$response = $this->getRequest('/v1/companies', $company->createCompany() );

if ($response->getStatusCode() == 200)
return Company::parseCompany(json_decode( $response->getBody() ) );
else
return null;
}
}
88 changes: 87 additions & 1 deletion src/Responses/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Mvdgeijn\Pax8\Responses;

use Illuminate\Support\Collection;
use stdClass;

class Company
{
Expand All @@ -18,14 +19,20 @@ class Company

protected string $country;

protected string $phone;

protected string $website;

protected string $status;

protected bool $billOnBehalfOfEnabled = false;

protected bool $selfServiceAllowed = false;

protected bool $orderApprovalRequired = false;

protected string $externalId;


public static function createFromBody( string $body )
{
Expand All @@ -43,17 +50,41 @@ public static function createFromBody( string $body )

public static function parseCompany( object $data ): Company
{
return (new Company())
$company = new Company();

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

return $company;
}

public function createCompany( ): array
{
return [
'name' => $this->getName(),
'address' => [
'street' => $this->getStreet(),
'city' => $this->getCity(),
'postalCode' => $this->getZipcode(),
'country' => $this->getCountry()
],
'phone' => $this->getPhone(),
'website' => $this->getWebsite(),
'externalId' => $this->getExternalId(),
'billOnBehalfOfEnabled' => $this->isBillOnBehalfOfEnabled(),
'selfServiceAllowed' => $this->isSelfServiceAllowed(),
'orderApprovalRequired' => $this->isOrderApprovalRequired()
];
}

/**
Expand Down Expand Up @@ -240,5 +271,60 @@ public function getWebsite()
return $this->website;
}

/**
* @param string|null $externalId
* @return Company
*/
public function setExternalId(?string $externalId): Company
{
$this->externalId = $externalId;

return $this;
}

/**
* @return string
*/
public function getExternalId(): string
{
return $this->externalId;
}

/**
* @param bool $selfServiceAllowed
* @return Company
*/
public function setSelfServiceAllowed(bool $selfServiceAllowed): Company
{
$this->selfServiceAllowed = $selfServiceAllowed;
return $this;
}

/**
* @return bool
*/
public function isSelfServiceAllowed(): bool
{
return $this->selfServiceAllowed;
}

/**
* @param string $phone
* @return Company
*/
public function setPhone(string $phone): Company
{
$this->phone = $phone;
return $this;
}

/**
* @return string
*/
public function getPhone(): string
{
return $this->phone;
}


}

0 comments on commit 10b1b09

Please sign in to comment.