diff --git a/README.md b/README.md index c1a3ac4..5e9a3d7 100644 --- a/README.md +++ b/README.md @@ -80,16 +80,16 @@ During the development of this package I ran into some pitfalls. Here a tip to h ## Supported resources -| Component | Fetch all | Fetch single | Create | Update | delete | -|---------------|:---------------------:|:---------------------:|:---------------------:|:---------------------:|:---------------------:| -| AccessToken | :white_check_mark: | :no_entry: | :no_entry: | :no_entry: | :no_entry: | -| Companies | :white_check_mark: | :white_check_mark: | :white_check_mark: | :no_entry: | :no_entry: | -| Contacts | :white_check_mark: | :white_check_mark: | :black_square_button: | :black_square_button: | :black_square_button: | -| Products | :white_check_mark: | :white_check_mark: | :no_entry: | :no_entry: | :no_entry: | -| Orders | :white_check_mark: | :white_check_mark: | :white_check_mark: | :no_entry: | :no_entry: | -| Subscriptions | :white_check_mark: | :white_check_mark: | :no_entry: | :black_square_button: | :black_square_button: | -| Invoices | :white_check_mark: | :white_check_mark: | :no_entry: | :no_entry: | :no_entry: | -| Usage | :black_square_button: | :black_square_button: | :no_entry: | :no_entry: | :no_entry: | +| Component | Fetch all | Fetch single | Create | Update | delete | +|---------------|:---------------------:|:---------------------:|:-------------------:|:---------------------:|:---------------------:| +| AccessToken | :white_check_mark: | :no_entry: | :no_entry: | :no_entry: | :no_entry: | +| Companies | :white_check_mark: | :white_check_mark: | :white_check_mark: | :no_entry: | :no_entry: | +| Contacts | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :black_square_button: | +| Products | :white_check_mark: | :white_check_mark: | :no_entry: | :no_entry: | :no_entry: | +| Orders | :white_check_mark: | :white_check_mark: | :white_check_mark: | :no_entry: | :no_entry: | +| Subscriptions | :white_check_mark: | :white_check_mark: | :no_entry: | :black_square_button: | :black_square_button: | +| Invoices | :white_check_mark: | :white_check_mark: | :no_entry: | :no_entry: | :no_entry: | +| Usage | :black_square_button: | :black_square_button: | :no_entry: | :no_entry: | :no_entry: | ## Links diff --git a/src/Requests/AbstractRequest.php b/src/Requests/AbstractRequest.php index 23cdeb4..178b7a2 100644 --- a/src/Requests/AbstractRequest.php +++ b/src/Requests/AbstractRequest.php @@ -78,6 +78,33 @@ protected function getRequest($path, $query = [] ): ?ResponseInterface * @throws \GuzzleHttp\Exception\GuzzleException */ protected function postRequest($path, \stdClass $data ): ?ResponseInterface + { + return $this->doRequest( 'POST', $path, $data ); + } + + /** + * Do a PUT request on the Pax8 API + * + * @param $path + * @param \stdClass $data + * @return ResponseInterface|null + * @throws \GuzzleHttp\Exception\GuzzleException + */ + protected function putRequest($path, \stdClass $data ): ?ResponseInterface + { + return $this->doRequest( 'PUT', $path, $data ); + } + + /** + * Handle PUT or POST request on the Pax8 API + * + * @param string $method + * @param $path + * @param \stdClass $data + * @return ResponseInterface|null + * @throws \GuzzleHttp\Exception\GuzzleException + */ + protected function doRequest(string $method, $path, \stdClass $data ): ?ResponseInterface { if( $this->pax8->isExpired() ) $this->pax8->renew(); @@ -86,7 +113,7 @@ protected function postRequest($path, \stdClass $data ): ?ResponseInterface $retries = 2; while( $retries > 0 ) { - $response = $client->request('POST', $path, [ + $response = $client->request($method, $path, [ 'headers' => [ 'content-type' => 'application/json', 'Authorization' => 'Bearer ' . $this->pax8->accessToken @@ -101,6 +128,7 @@ protected function postRequest($path, \stdClass $data ): ?ResponseInterface $this->pax8->renew(); $retries--; } + return $this->handleErrors( $response ); } diff --git a/src/Requests/ContactRequest.php b/src/Requests/ContactRequest.php index 1b76f84..15a2648 100644 --- a/src/Requests/ContactRequest.php +++ b/src/Requests/ContactRequest.php @@ -68,4 +68,25 @@ public function create(Contact $contact): ?Contact return null; } + + /** + * Updates an existing contact. Returns a new Contact object when successfully updated, + * or null if contact not updated or invalid contact information is passed + * + * @param string $companyId + * @param Contact $contact + * @return Contact|null + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function update(string $companyId, Contact $contact): ?Contact + { + $contactId = $contact->getId(); + $response = $this->putRequest("/v1/companies/$companyId/contacts/$contactId", $contact->toObject() ); + + if ($response->getStatusCode() == 200) + return Contact::parse(json_decode( $response->getBody() ) ); + else + return null; + } + } diff --git a/src/Responses/Contact.php b/src/Responses/Contact.php index f55ea55..8769a06 100644 --- a/src/Responses/Contact.php +++ b/src/Responses/Contact.php @@ -32,16 +32,37 @@ public function createContact() { return [ 'firstName' => $this->getFirstName(), - 'lastName' => $this->getLastName(), - 'email' => $this->getEmail(), - 'phone' => $this->getPhone(), - 'types' => $this->getTypes() + 'lastName' => $this->getLastName(), + 'email' => $this->getEmail(), + 'phone' => $this->getPhone(), + 'types' => $this->getTypes() ]; } + /** + * @return \stdClass + */ public function toObject(): \stdClass { - return (object)$this->createContact(); + $contact = new \stdClass; + + $contact->firstName = $this->firstName; + $contact->lastName = $this->lastName; + $contact->email = $this->email; + $contact->phone = $this->phone; + + $types = $this->types->getTypes(); + + foreach( $types as $type ) { + $newType = new \stdClass; + + $newType->type = $type["type"]; + $newType->primary = $type["primary"]; + + $contact->types[] = $newType; + } + + return $contact; } /** diff --git a/src/Responses/ContactType.php b/src/Responses/ContactType.php index 24a9ab3..c9a2744 100644 --- a/src/Responses/ContactType.php +++ b/src/Responses/ContactType.php @@ -6,7 +6,7 @@ { const CONTACTTYPE_ADMIN = "Admin"; - const CONTACTTYPE_TECH = "Tech"; + const CONTACTTYPE_TECH = "Technical"; const CONTACTTYPE_BILLING = "Billing"; @@ -72,30 +72,30 @@ public function isPrimaryAdmin(): bool return isset( $this->types[self::CONTACTTYPE_ADMIN] ) && $this->types[self::CONTACTTYPE_ADMIN] == true; } - public function enableTech( bool $primary = false ) + public function enableTechnical( bool $primary = false ) { $this->types[self::CONTACTTYPE_TECH] = $primary; } - public function disableTech( ) + public function disableTechnical( ) { if( isset( $this->types[self::CONTACTTYPE_TECH] ) ) unset( $this->types[self::CONTACTTYPE_TECH]); } - public function isTech(): bool + public function isTechnical(): bool { return isset( $this->types[self::CONTACTTYPE_TECH] ); } - public function isPrimaryTech(): bool + public function isPrimaryTechnical(): bool { return isset( $this->types[self::CONTACTTYPE_TECH] ) && $this->types[self::CONTACTTYPE_TECH] == true; } public function enableBilling( bool $primary = false ) { - $this->types[self::CONTACTTYPE_TECH] = $primary; + $this->types[self::CONTACTTYPE_BILLING] = $primary; } public function disableBilling( )