Skip to content

Commit

Permalink
Support for Contacts (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
briwa committed Apr 14, 2023
1 parent 2e9bbee commit 1f36b82
Show file tree
Hide file tree
Showing 6 changed files with 542 additions and 1 deletion.
72 changes: 71 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,76 @@ $customer = ChartMogul\Customer::retrieve($cus->uuid);
$tags = $customer->removeCustomAttributes("age", "channel");
```

**List Contacts from a customer**

```php
$customer = ChartMogul\Customer::retrieve($uuid);
$contacts = $customer->contacts([
'cursor' => 'aabbccdd...'
]);
```

**Create a Contact from a customer**

```php
$customer = ChartMogul\Customer::retrieve($uuid);
$new_customer = $customer->createContact([
"data_source_uuid" => "ds_00000000-0000-0000-0000-000000000000",
"first_name" => "Adam",
"last_name" => "Smith",
]);
```

### Contacts

**List Contacts**

```php
$contacts = ChartMogul\Contacts::all([
'cursor' => 'aabbccdd...'
]);
```

**Create a Contact**

```php
$new_contact = ChartMogul\Contact::create([
"customer_uuid" => "cus_00000000-0000-0000-0000-000000000000",
"data_source_uuid" => "ds_00000000-0000-0000-0000-000000000000",
"first_name" => "Adam",
"last_name" => "Smith",
]);
```

**Get a Contact**

```php
$contact = ChartMogul\Contact::retrieve($uuid);
```

**Delete A Contact**

```php
$contact = ChartMogul\Contact::retrieve($uuid);
$contact->destroy();
```

**Update a Contact**

```php
$updated_contact = ChartMogul\Contact::update([
'contact_uuid' => $uuid
], [
'first_name' => 'New Name'
]);
```

**Merge Contacts**

```php
$merged_contact = ChartMogul\Contact::merge($into_contact_uuid, $from_contact_uuid);
```

### Plans

**Import a Plan**
Expand Down Expand Up @@ -747,7 +817,7 @@ You need `Docker` installed locally to use our `Makefile` workflow.
* Fork it.
* Create your feature branch (`git checkout -b my-new-feature`).
* Install dependencies: `make build`.
* Fix bugs or add features. Make sure the changes pass the coding guidelines by runing PHP CodeSniffer: `make cs`. You can also run `make cbf` to fix some errors automatically.
* Install Composer vendor dependencies with `make composer install`
* Write tests for your new features. Run tests and check test coverage with `make test`.
* To run any composer commands or php scripts, use `make composer <commands>` or `make php <script>`.
* If all tests are passed, push to the branch (`git push origin my-new-feature`).
Expand Down
108 changes: 108 additions & 0 deletions src/Contact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace ChartMogul;

use ChartMogul\Resource\AbstractResource;
use ChartMogul\Resource\CollectionWithCursor;
use ChartMogul\Http\ClientInterface;
use ChartMogul\Service\AllTrait;
use ChartMogul\Service\UpdateTrait;
use ChartMogul\Service\CreateTrait;
use ChartMogul\Service\DestroyTrait;
use ChartMogul\Service\GetTrait;

/**
* @property-read string $uuid
* @property-read string $customer_uuid
* @property-read string $data_source_uuid
* @property-read string $customer_external_id
* @property-read string $first_name
* @property-read string $last_name
* @property-read integer $position
* @property-read string $email
* @property-read string $title
* @property-read string $notes
* @property-read string $phone
* @property-read string $linked_in
* @property-read string $twitter
* @property-read string $custom
*/
class Contact extends AbstractResource
{
use CreateTrait;
use AllTrait;
use GetTrait;
use DestroyTrait;
use UpdateTrait;

/**
* @ignore
*/
public const RESOURCE_NAME = 'Contact';
/**
* @ignore
*/
public const RESOURCE_PATH = '/v1/contacts';
public const RESOURCE_ID = 'contact_uuid';
public const ROOT_KEY = 'entries';

protected $uuid;
protected $customer_uuid;
protected $data_source_uuid;
protected $customer_external_id;
protected $first_name;
protected $last_name;
protected $position;
protected $email;
protected $title;
protected $notes;
protected $phone;
protected $linked_in;
protected $twitter;
protected $custom;

/**
* Merge Contacts
* @param string $into
* @param string $from
* @param ClientInterface|null $client
* @return Contact
*/
public static function merge($into, $from, ClientInterface $client = null)
{
$result = (new static([], $client))
->getClient()
->send("/v1/contacts/".$into."/merge/".$from, "POST");

return new Contact($result, $client);
}

/**
* Overrides fromArray so that it will return a collection with cursor instead.
*
* @param array $data
* @param ClientInterface|null $client
* @return CollectionWithCursor|static
*/
public static function fromArray(array $data, ClientInterface $client = null)
{
if (isset($data[static::ROOT_KEY])) {
$array = new CollectionWithCursor(array_map(function ($data) use ($client) {
return static::fromArray($data, $client);
}, $data[static::ROOT_KEY]));

if (isset($data["cursor"])) {
$array->cursor = $data["cursor"];
}

if (isset($data["has_more"])) {
$array->has_more = $data["has_more"];
}

return $array;
}

return new static($data, $client);
}
}
27 changes: 27 additions & 0 deletions src/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ChartMogul\Resource\AbstractResource;
use ChartMogul\Resource\Collection;
use ChartMogul\Resource\CollectionWithCursor;
use ChartMogul\Http\ClientInterface;
use ChartMogul\Service\UpdateTrait;
use ChartMogul\Service\CreateTrait;
Expand Down Expand Up @@ -290,4 +291,30 @@ public function invoices(array $options = [])
}
return $this->invoices;
}

/**
* Find all contacts in a customer
* @param array $options
* @return CollectionWithCursor
*/
public function contacts(array $options = [])
{
$client = $this->getClient();
$result = $client->send("/v1/customers/".$this->uuid."/contacts", "GET", $options);

return Contact::fromArray($result, $client);
}

/**
* Creates a contact from the customer.
* @param array $data
* @return Contact
*/
public function createContact(array $data = [])
{
$client = $this->getClient();
$result = $client->send("/v1/customers/".$this->uuid."/contacts", "POST", $data);

return new Contact($result, $client);
}
}
17 changes: 17 additions & 0 deletions src/Resource/CollectionWithCursor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace ChartMogul\Resource;

use Doctrine\Common\Collections\ArrayCollection;

class CollectionWithCursor extends ArrayCollection
{
/**
* @var boolean
*/
public $has_more;
/**
* @var string
*/
public $cursor;
}
Loading

0 comments on commit 1f36b82

Please sign in to comment.