From 10b1b0900b3e00c7b9abbcabb8eeefda93add8df Mon Sep 17 00:00:00 2001 From: bHosted Date: Fri, 4 Feb 2022 22:14:48 +0100 Subject: [PATCH] Added Create a Company --- README.md | 8 +-- src/Requests/AbstractRequest.php | 55 ++++++++++++++++++-- src/Requests/CompanyRequest.php | 9 ++++ src/Responses/Company.php | 88 +++++++++++++++++++++++++++++++- 4 files changed, 152 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d5e5db1..fdec402 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Requests/AbstractRequest.php b/src/Requests/AbstractRequest.php index 5e76f9d..ca35ef5 100644 --- a/src/Requests/AbstractRequest.php +++ b/src/Requests/AbstractRequest.php @@ -4,6 +4,7 @@ use Mvdgeijn\Pax8\Responses\AccessToken; use GuzzleHttp\Client; +use Psr\Http\Message\ResponseInterface; class AbstractRequest { @@ -11,6 +12,8 @@ class AbstractRequest protected AccessToken $accessToken; + protected ?array $errors = null; + public function __construct( AccessToken &$accessToken ) { $this->baseUrl = config('pax8.url.api'); @@ -18,10 +21,10 @@ public function __construct( AccessToken &$accessToken ) $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 @@ -29,5 +32,51 @@ protected function getRequest( $path, $query = [] ) '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; } } diff --git a/src/Requests/CompanyRequest.php b/src/Requests/CompanyRequest.php index b002d8e..9476bef 100644 --- a/src/Requests/CompanyRequest.php +++ b/src/Requests/CompanyRequest.php @@ -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; } } diff --git a/src/Responses/Company.php b/src/Responses/Company.php index de6160c..ba2999f 100644 --- a/src/Responses/Company.php +++ b/src/Responses/Company.php @@ -3,6 +3,7 @@ namespace Mvdgeijn\Pax8\Responses; use Illuminate\Support\Collection; +use stdClass; class Company { @@ -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 ) { @@ -43,7 +50,9 @@ 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) @@ -51,9 +60,31 @@ public static function parseCompany( object $data ): Company ->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() + ]; } /** @@ -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; + } + }