Skip to content

Commit

Permalink
Added Contact Update method and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdgeijn committed Feb 26, 2022
1 parent ce24940 commit 33ccfee
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 22 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 29 additions & 1 deletion src/Requests/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -101,6 +128,7 @@ protected function postRequest($path, \stdClass $data ): ?ResponseInterface
$this->pax8->renew();
$retries--;
}

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

Expand Down
21 changes: 21 additions & 0 deletions src/Requests/ContactRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
31 changes: 26 additions & 5 deletions src/Responses/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Responses/ContactType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{
const CONTACTTYPE_ADMIN = "Admin";

const CONTACTTYPE_TECH = "Tech";
const CONTACTTYPE_TECH = "Technical";

const CONTACTTYPE_BILLING = "Billing";

Expand Down Expand Up @@ -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( )
Expand Down

0 comments on commit 33ccfee

Please sign in to comment.