diff --git a/src/Client.php b/src/Client.php index eec03b9..d738ee5 100644 --- a/src/Client.php +++ b/src/Client.php @@ -40,6 +40,17 @@ */ class Client extends GuzzleClient { + /** + * @var array + */ + private $marketoObjects = array( + 'Leads' => 'leads', + 'Companies' => 'companies', + 'Opportunities' => 'opportunities', + 'Opportunities Roles' => 'opportunities/roles', + 'Sales Persons' => 'salespersons' + ); + /** * {@inheritdoc} */ @@ -192,6 +203,135 @@ private function createOrUpdateLeadsCommand($action, $leads, $lookupField, $args return $this->getResult('createOrUpdateLeads', $args, false, $returnRaw); } + /** + * Only update the given opportunities. + * + * @param array $opportunities Array of arrays. + * @param string $dedupeBy + * @param array $args + * @param bool|false $returnRaw + * @throws \Exception + * + * @link http://developers.marketo.com/rest-api/endpoint-reference/lead-database-endpoint-reference/#!/Opportunities/syncOpportunitiesUsingPOST + * + * @return GetLeadsResponse + */ + public function updateOpportunities($opportunities, $dedupeBy = 'dedupeFields', $args = array(), $returnRaw = false) { + return $this->createOrUpdateObjects('Opportunities', 'updateOnly', $opportunities, $dedupeBy, $args, $returnRaw); + } + + /** + * Only create the given opportunities. + * + * @param array $opportunities Array of arrays. + * @param string $dedupeBy + * @param array $args + * @param bool|false $returnRaw + * @throws \Exception + * + * @link http://developers.marketo.com/rest-api/endpoint-reference/lead-database-endpoint-reference/#!/Opportunities/syncOpportunitiesUsingPOST + * + * @return GetLeadsResponse + */ + public function createOpportunities($opportunities, $dedupeBy = 'dedupeFields', $args = array(), $returnRaw = false) { + return $this->createOrUpdateObjects('Opportunities', 'createOnly', $opportunities, $dedupeBy, $args, $returnRaw); + } + + /** + * Create or update the given opportunities. + * + * @param array $opportunities Array of arrays. + * @param string $dedupeBy + * @param array $args + * @param bool|false $returnRaw + * @throws \Exception + * + * @link http://developers.marketo.com/rest-api/endpoint-reference/lead-database-endpoint-reference/#!/Opportunities/syncOpportunitiesUsingPOST + * + * @return GetLeadsResponse + */ + public function createOrUpdateOpportunities($opportunities, $dedupeBy = 'dedupeFields', $args = array(), $returnRaw = false) { + return $this->createOrUpdateObjects('Opportunities', 'createOrUpdate', $opportunities, $dedupeBy, $args, $returnRaw); + } + + /** + * Only update the given companies. + * + * @param array $companies Array of arrays. + * @param string $dedupeBy + * @param array $args + * @param bool|false $returnRaw + * @throws \Exception + * + * @link http://developers.marketo.com/rest-api/endpoint-reference/lead-database-endpoint-reference/#!/Companies/syncCompaniesUsingPOST + * + * @return GetLeadsResponse + */ + public function updateCompanies($companies, $dedupeBy = 'dedupeFields', $args = array(), $returnRaw = false) { + return $this->createOrUpdateObjects('Companies', 'updateOnly', $companies, $dedupeBy, $args, $returnRaw); + } + + /** + * Only create the given companies. + * + * @param array $companies Array of arrays. + * @param string $dedupeBy + * @param array $args + * @param bool|false $returnRaw + * @throws \Exception + * + * @link http://developers.marketo.com/rest-api/endpoint-reference/lead-database-endpoint-reference/#!/Companies/syncCompaniesUsingPOST + * + * @return GetLeadsResponse + */ + public function createCompanies($companies, $dedupeBy = 'dedupeFields', $args = array(), $returnRaw = false) { + return $this->createOrUpdateObjects('Companies', 'createOnly', $companies, $dedupeBy, $args, $returnRaw); + } + + /** + * Create or update the given companies. + * + * @param array $companies Array of arrays. + * @param string $dedupeBy + * @param array $args + * @param bool|false $returnRaw + * @throws \Exception + * + * @link http://developers.marketo.com/rest-api/endpoint-reference/lead-database-endpoint-reference/#!/Companies/syncCompaniesUsingPOST + * + * @return GetLeadsResponse + */ + public function createOrUpdateCompanies($companies, $dedupeBy = 'dedupeFields', $args = array(), $returnRaw = false) { + return $this->createOrUpdateObjects('Companies', 'createOrUpdate', $companies, $dedupeBy, $args, $returnRaw); + } + + /** + * Generic method to create or update Marketo objects. + * + * @param string $objectName + * @param string $action Should be createOnly, updateOnly, or createOrUpdate. + * @param array $records Array of arrays. + * @param string $dedupeBy + * @param array $args + * @param bool|false $returnRaw + * @throws \Exception + * + * @return GetLeadsResponse + + */ + private function createOrUpdateObjects($objectName, $action, $records, $dedupeBy, $args = array(), $returnRaw = false) { + if (!isset($this->marketoObjects[$objectName])) { + throw new \Exception('createOrUpdate() Expected parameter $objectName, to be a valid Marketo object ' . "but $objectName provided"); + }; + + $args['objectName'] = $this->marketoObjects[$objectName]; + $args['action'] = $action; + $args['input'] = $records; + $args['dedupeBy'] = $dedupeBy; + + return $this->getResult('createOrUpdateObject', $args, false, $returnRaw); + } + /** * Create the given leads. * @@ -684,6 +824,93 @@ public function approveEmail($emailId, $args = array(), $returnRaw = false) return $this->getResult('approveEmailbyId', $args, false, $returnRaw); } + /** + * Describe the leads object + * + * @param bool|false $returnRaw + * @throws \Exception + * + * @link http://developers.marketo.com/rest-api/endpoint-reference/lead-database-endpoint-reference/#!/Leads/describeUsingGET_2 + * + * @return Response + */ + public function describeLeads($returnRaw = false) { + return $this->describeObject('Leads', $returnRaw); + } + + /** + * Describe the opportunities object + * + * @param bool|false $returnRaw + * @throws \Exception + * + * @link http://developers.marketo.com/rest-api/endpoint-reference/lead-database-endpoint-reference/#!/Opportunities/describeUsingGET_3 + * + * @return Response + */ + public function describeOpportunities($returnRaw = false) { + return $this->describeObject('Opportunities', $returnRaw); + } + + /** + * Describe the opportunities roles object. + * + * @param bool|false $returnRaw + * @throws \Exception + * + * @link http://developers.marketo.com/rest-api/endpoint-reference/lead-database-endpoint-reference/#!/Opportunities/describeOpportunityRoleUsingGET + * + * @return Response + */ + public function describeOpportunityRoles($returnRaw = false) { + return $this->describeObject('Opportunities Roles', $returnRaw); + } + + /** + * Describe the companies object. + * + * @param bool|false $returnRaw + * @throws \Exception + * + * @link http://developers.marketo.com/rest-api/endpoint-reference/lead-database-endpoint-reference/#!/Companies/describeUsingGET + * + * @return Response + */ + public function describeCompanies($returnRaw = false) { + return $this->describeObject('Companies', $returnRaw); + } + + /** + * Describe the Sales Persons object. + * + * @param bool|false $returnRaw + * @throws \Exception + * + * @link http://developers.marketo.com/rest-api/endpoint-reference/lead-database-endpoint-reference/#!/Sales_Persons/describeUsingGET_4 + * + * @return Response + */ + public function describeSalesPersons($returnRaw = false) { + return $this->describeObject('Sales Persons', $returnRaw); + } + + /** + * Generic method to describe a Marketo object. + * + * @param string $objectName + * @param bool|false $returnRaw + * @return Response + * @throws \Exception + */ + private function describeObject($objectName, $returnRaw = false) { + if (!isset($this->marketoObjects[$objectName])) { + throw new \Exception('describeObject() Expected parameter $objectName, to be a valid Marketo object ' . "but $objectName provided"); + }; + + $args['objectName'] = $this->marketoObjects[$objectName]; + return $this->getResult('describeObject', $args, false, $returnRaw); + } + /** * Internal helper method to actually perform command. * diff --git a/src/service.json b/src/service.json index 80d2709..a745e13 100644 --- a/src/service.json +++ b/src/service.json @@ -247,6 +247,25 @@ "id": {"location": "uri"} }, "responseClass": "CSD\\Marketo\\Response\\ApproveEmailResponse" + }, + "describeObject": { + "httpMethod": "GET", + "uri": "/rest/v1/{objectName}/describe.json", + "parameters": { + "objectName": {"location": "uri"} + }, + "responseClass": "CSD\\Marketo\\Response" + }, + "createOrUpdateObject": { + "httpMethod": "POST", + "uri": "/rest/v1/{objectName}.json", + "parameters": { + "objectName": {"location": "uri"}, + "action": {"location": "json"}, + "input": {"location": "json"}, + "dedupeBy": {"location": "json"} + }, + "responseClass": "CSD\\Marketo\\Response\\GetLeadsResponse" } } }