diff --git a/README.md b/README.md index 4e66329..d1c6825 100644 --- a/README.md +++ b/README.md @@ -320,6 +320,60 @@ $new_customer = $customer->createContact([ ]); ``` +**List Customer Notes from a customer** +```php +$customer = ChartMogul\Customer::retrieve($uuid); +$customer_notes = $customer->notes([ + 'cursor' => 'aabbccdd...' +]); +``` + +**Create a Customer Note from a customer** +```php +$customer = ChartMogul\Customer::retrieve($uuid); +$new_customer_note = $customer->createNote([ + 'type' => 'note', + 'text' => 'This is a note' +]); +``` + +### Customer Notes + +**List Customer Notes** +```php +$customer_notes = ChartMogul\CustomerNote::all([ + 'customer_uuid' => $uuid, + 'cursor' => 'aabbccdd...' +]) +``` + +**Create a Customer Note** +```php +$customer_note = ChartMogul\CustomerNote::create([ + 'customer_uuid': $uuid, + 'type' => 'note', + 'text' => 'This is a note' +]) +``` + +**Get a Customer Note** +```php +$customer_note = ChartMogul\CustomerNote::retrieve($note_uuid) +``` + +**Update a Customer Note** +```php +$updated_customer_note = ChartMogul\CustomerNote::update($note_uuid, [ + 'text' => 'This is a new note' +]); +``` + +**Delete a Customer Note** +```php +$customer_note = ChartMogul\CustomerNote::retrieve($note_uuid) +$customer_note->destroy(); +``` + ### Contacts **List Contacts** diff --git a/src/Contact.php b/src/Contact.php index ba82b0c..f554830 100644 --- a/src/Contact.php +++ b/src/Contact.php @@ -3,13 +3,13 @@ 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; +use ChartMogul\Service\FromArrayTrait; /** * @property-read string $uuid @@ -34,6 +34,7 @@ class Contact extends AbstractResource use GetTrait; use DestroyTrait; use UpdateTrait; + use FromArrayTrait; /** * @ignore @@ -77,37 +78,4 @@ public static function merge($into, $from, ClientInterface $client = null) 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); - } } diff --git a/src/Customer.php b/src/Customer.php index 09872d7..9ff317d 100644 --- a/src/Customer.php +++ b/src/Customer.php @@ -354,4 +354,32 @@ public function createContact(array $data = []) return new Contact($result, $client); } + + /** + * Find all customer notes in a customer + * + * @param array $options + * @return CollectionWithCursor + */ + public function notes(array $options = []) + { + $client = $this->getClient(); + $result = $client->send("/v1/customer_notes", "GET", [$options, "customer_uuid" => $this->uuid]); + + return CustomerNote::fromArray($result, $client); + } + + /** + * Creates a customer note from the customer. + * + * @param array $data + * @return CustomerNote + */ + public function createNote(array $data = []) + { + $client = $this->getClient(); + $result = $client->send("/v1/customer_notes", "POST", [$data, "customer_uuid" => $this->uuid]); + + return new CustomerNote($result, $client); + } } diff --git a/src/CustomerNote.php b/src/CustomerNote.php new file mode 100644 index 0000000..da5effc --- /dev/null +++ b/src/CustomerNote.php @@ -0,0 +1,51 @@ += $maxAttempts && ! is_null($ex)) { + if ($attempt >= $maxAttempts && !is_null($ex)) { throw $ex; } diff --git a/src/Service/FromArrayTrait.php b/src/Service/FromArrayTrait.php new file mode 100644 index 0000000..dd2dfd9 --- /dev/null +++ b/src/Service/FromArrayTrait.php @@ -0,0 +1,45 @@ +cursor = $data['cursor']; + } + + if (isset($data['has_more'])) { + $array->has_more = $data['has_more']; + } + + return $array; + } + + return new static($data, $client); + } +} diff --git a/tests/Unit/CustomerNoteTest.php b/tests/Unit/CustomerNoteTest.php new file mode 100644 index 0000000..56769ff --- /dev/null +++ b/tests/Unit/CustomerNoteTest.php @@ -0,0 +1,152 @@ +getMockClient(0, [200], $stream); + + $uuid = "cus_00000000-0000-0000-0000-000000000000"; + + $result = CustomerNote::all(["customer_uuid" => $uuid], $cmClient); + $request = $mockClient->getRequests()[0]; + + $this->assertEquals("GET", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("/v1/customer_notes", $uri->getPath()); + + $this->assertTrue($result[0] instanceof CustomerNote); + $this->assertEquals("cursor==", $result->cursor); + $this->assertEquals(true, $result->has_more); + } + + public function testCreateNote() + { + $stream = Psr7\stream_for(CustomerNoteTest::NOTE_JSON); + list($cmClient, $mockClient) = $this->getMockClient(0, [200], $stream); + + $uuid = "cus_00000000-0000-0000-0000-000000000000"; + + $result = CustomerNote::create( + [ + "customer_uuid" => $uuid, + "type" => "note", + "author_email" => "john@example.com", + "text" => "This is a note", + ] + , $cmClient + ); + $request = $mockClient->getRequests()[0]; + + $this->assertEquals("POST", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("/v1/customer_notes", $uri->getPath()); + + $this->assertTrue($result instanceof CustomerNote); + $this->assertEquals("note_00000000-0000-0000-0000-000000000000", $result->uuid); + } + + public function testRetrieveNote() + { + $stream = Psr7\stream_for(CustomerNoteTest::NOTE_JSON); + list($cmClient, $mockClient) = $this->getMockClient(0, [200], $stream); + + $uuid = "con_00000000-0000-0000-0000-000000000000"; + + $result = CustomerNote::retrieve($uuid, $cmClient); + $request = $mockClient->getRequests()[0]; + + $this->assertEquals("GET", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("/v1/customer_notes/".$uuid, $uri->getPath()); + + $this->assertTrue($result instanceof CustomerNote); + $this->assertEquals("note_00000000-0000-0000-0000-000000000000", $result->uuid); + } + + public function testUpdateNote() + { + $stream = Psr7\stream_for(CustomerNoteTest::UPDATED_NOTE_JSON); + list($cmClient, $mockClient) = $this->getMockClient(0, [200], $stream); + + $uuid = "note_00000000-0000-0000-0000-000000000000"; + + $result = CustomerNote::update( + [ + "note_uuid" => $uuid, + ], [ + "text" => "This is a new note", + ], $cmClient + ); + $request = $mockClient->getRequests()[0]; + + $this->assertEquals("PATCH", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("/v1/customer_notes/".$uuid, $uri->getPath()); + + $this->assertTrue($result instanceof CustomerNote); + $this->assertEquals("This is a new note", $result->text); + } + + public function testDeleteNote() + { + $stream = Psr7\stream_for("{}"); + list($cmClient, $mockClient) = $this->getMockClient(0, [204], $stream); + + $uuid = "note_00000000-0000-0000-0000-000000000000"; + + $result = (new CustomerNote(["uuid" => $uuid], $cmClient))->destroy(); + $request = $mockClient->getRequests()[0]; + + $this->assertEquals("DELETE", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("/v1/customer_notes/".$uuid, $uri->getPath()); + + $this->assertEquals("{}", $result); + } +} diff --git a/tests/Unit/CustomerTest.php b/tests/Unit/CustomerTest.php index 957f584..9d4aada 100644 --- a/tests/Unit/CustomerTest.php +++ b/tests/Unit/CustomerTest.php @@ -4,6 +4,7 @@ use ChartMogul\Http\Client; use ChartMogul\Customer; use ChartMogul\Contact; +use ChartMogul\CustomerNote; use ChartMogul\Resource\Collection; use ChartMogul\Exceptions\ChartMogulException; use GuzzleHttp\Psr7; @@ -202,6 +203,32 @@ class CustomerTest extends TestCase } }'; + const LIST_NOTES_JSON= '{ + "entries": [ + { + "uuid": "note_00000000-0000-0000-0000-000000000000", + "customer_uuid": "cus_00000000-0000-0000-0000-000000000000", + "type": "note", + "author": "John Doe (john@example.com)", + "text": "This is a note", + "created_at": "2015-06-09T13:16:00-04:00", + "updated_at": "2015-06-09T13:16:00-04:00" + } + ], + "cursor": "cursor==", + "has_more": true + }'; + + const NOTE_JSON= '{ + "uuid": "note_00000000-0000-0000-0000-000000000000", + "customer_uuid": "cus_00000000-0000-0000-0000-000000000000", + "type": "note", + "author": "John Doe (john@example.com)", + "text": "This is a note", + "created_at": "2015-06-09T13:16:00-04:00", + "updated_at": "2015-06-09T13:16:00-04:00" + }'; + public function testRetrieveCustomer() { $stream = Psr7\stream_for(CustomerTest::RETRIEVE_CUSTOMER_JSON); @@ -366,4 +393,49 @@ public function testCreateCustomersContact() $this->assertTrue($result instanceof Contact); $this->assertEquals("con_00000000-0000-0000-0000-000000000000", $result->uuid); } + + public function testListCustomerNotes() + { + $stream = Psr7\stream_for(CustomerTest::LIST_NOTES_JSON); + list($cmClient, $mockClient) = $this->getMockClient(0, [200], $stream); + + $uuid = "cus_00000000-0000-0000-0000-000000000000"; + + $result = (new Customer(["uuid" => $uuid], $cmClient))->notes(); + $request = $mockClient->getRequests()[0]; + + $this->assertEquals("GET", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("/v1/customer_notes", $uri->getPath()); + + $this->assertTrue($result[0] instanceof CustomerNote); + $this->assertEquals("cursor==", $result->cursor); + $this->assertEquals(true, $result->has_more); + } + + public function testCreateNote() + { + $stream = Psr7\stream_for(CustomerTest::NOTE_JSON); + list($cmClient, $mockClient) = $this->getMockClient(0, [200], $stream); + + $uuid = "cus_00000000-0000-0000-0000-000000000000"; + + $result = (new Customer(["uuid" => $uuid], $cmClient))->createNote( + [ + "customer_uuid" => $uuid, + ], [ + "type" => "note", + "author_email" => "john@example.com", + "text" => "This is a note", + ] + ); + $request = $mockClient->getRequests()[0]; + + $this->assertEquals("POST", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("/v1/customer_notes", $uri->getPath()); + + $this->assertTrue($result instanceof CustomerNote); + $this->assertEquals("note_00000000-0000-0000-0000-000000000000", $result->uuid); + } }