Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
Separated common parts into AbstractFinisher class
  • Loading branch information
Felix Gradinaru committed Apr 24, 2018
1 parent 0d8c0cc commit 01a2d5b
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 71 deletions.
55 changes: 55 additions & 0 deletions Classes/AbstractFinisher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace GradinaruFelix\Pipedrive;

use Neos\Flow\Annotations as Flow;
use Neos\Form\Core\Model\AbstractFinisher as NeosFormAbstractFinisher;
use Neos\Form\Exception\FinisherException;
use GradinaruFelix\Pipedrive\Utility\Api as PipedriveApi;

abstract class AbstractFinisher extends NeosFormAbstractFinisher
{

const OMITTED_OPTIONS = array(
'testMode',
'identifier',
);



protected function getIdentifier() {
$scope = $this->scope;
$customIdentifier = $this->parseOption('identifier');

if($customIdentifier) {
$identifier = "Pipedrive." . $customIdentifier;
} else {
$identifier = "Pipedrive." . $scope . "Finisher";
}

return $identifier;
}

/**
* Runs the API request for the given data
*
* @return bool
* @throws FinisherException
*/
protected function callAPI($data) {
$apiUtility = new PipedriveApi($this->apiEndpoint);
$apiUtility->setData($data);

$formState = $this->finisherContext->getFormRuntime()->getFormState();

$response = $apiUtility->execute();

if($response->data->id) {
$formState->setFormValue($this->getIdentifier() . ".ID", $response->data->id);
return true;
} else {
throw new FinisherException("Something went wrong while calling the API!");
return false;
}
}

}
32 changes: 17 additions & 15 deletions Classes/Finishers/ActivityFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,43 @@
namespace GradinaruFelix\Pipedrive\Finishers;

use Neos\Flow\Annotations as Flow;
use Neos\Form\Core\Model\AbstractFinisher;
use Neos\Form\Exception\FinisherException;
use GradinaruFelix\Pipedrive\AbstractFinisher;
use GradinaruFelix\Pipedrive\Utility\Api as PipedriveApi;

/**
* This finisher creates a activity in Pipedrive
* This finisher creates an activity in Pipedrive
*/

class ActivityFinisher extends AbstractFinisher
{

const OMITTED_OPTIONS = array(
'testMode',
);

/**
* Default activity type
* @Flow\InjectConfiguration(path="Form.Finisher.Activity.defaultValue")
* @var String
*/
protected $defaultType = '';

/**
* API endpoint name
* @var String
*/
protected $apiEndpoint = 'activities';

/**
* Name of the scope
* @var String
*/
protected $scope = 'Activity';

/**
* @var array
*/
protected $defaultOptions = array(
'subject' => '',
'done' => true,
'identifier' => '',
'testMode' => false,
);

Expand All @@ -54,17 +63,10 @@ protected function executeInternal()
}
}

$apiUtility = new PipedriveApi('activities');
$apiUtility->setData($data);

$formState = $this->finisherContext->getFormRuntime()->getFormState();

if ($testMode === true) {

\Neos\Flow\var_dump($data);
} else {
$response = $apiUtility->execute();
$formState->setFormValue("Pipedrive.ActivityFinisher.ID", $response->data->id);
print_r($formState->getFormValues());
$this->callAPI($data);
}
}
}
29 changes: 15 additions & 14 deletions Classes/Finishers/DealFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@
namespace GradinaruFelix\Pipedrive\Finishers;

use Neos\Flow\Annotations as Flow;
use Neos\Form\Core\Model\AbstractFinisher;
use Neos\Form\Exception\FinisherException;
use GradinaruFelix\Pipedrive\AbstractFinisher;
use GradinaruFelix\Pipedrive\Utility\Api as PipedriveApi;

/**
* This finisher sends an email to one or more recipients
* This finisher creates a deal in Pipedrive
*/

class DealFinisher extends AbstractFinisher
{

const OMITTED_OPTIONS = array(
'testMode',
);
/**
* API endpoint name
* @var String
*/
protected $apiEndpoint = 'deals';

/**
* Name of the scope
* @var String
*/
protected $scope = 'Deal';

/**
* @var array
Expand Down Expand Up @@ -44,17 +52,10 @@ protected function executeInternal()
}
}

$apiUtility = new PipedriveApi('deals');
$apiUtility->setData($data);

$formState = $this->finisherContext->getFormRuntime()->getFormState();

if ($testMode === true) {

\Neos\Flow\var_dump($data);
} else {
$response = $apiUtility->execute();
$formState->setFormValue("Pipedrive.DealFinisher.ID", $response->data->id);
print_r($formState->getFormValues());
$this->callAPI($data);
}
}
}
29 changes: 15 additions & 14 deletions Classes/Finishers/NoteFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@
namespace GradinaruFelix\Pipedrive\Finishers;

use Neos\Flow\Annotations as Flow;
use Neos\Form\Core\Model\AbstractFinisher;
use Neos\Form\Exception\FinisherException;
use GradinaruFelix\Pipedrive\AbstractFinisher;
use GradinaruFelix\Pipedrive\Utility\Api as PipedriveApi;

/**
* This finisher sends an email to one or more recipients
* This finisher creates a note in Pipedrive
*/

class NoteFinisher extends AbstractFinisher
{

const OMITTED_OPTIONS = array(
'testMode',
);
/**
* API endpoint name
* @var String
*/
protected $apiEndpoint = 'notes';

/**
* Name of the scope
* @var String
*/
protected $scope = 'Note';

/**
* @var array
Expand Down Expand Up @@ -43,17 +51,10 @@ protected function executeInternal()
}
}

$apiUtility = new PipedriveApi('notes');
$apiUtility->setData($data);

$formState = $this->finisherContext->getFormRuntime()->getFormState();

if ($testMode === true) {

\Neos\Flow\var_dump($data);
} else {
$response = $apiUtility->execute();
$formState->setFormValue("Pipedrive.ActivityFinisher.ID", $response->data->id);
print_r($formState->getFormValues());
$this->callAPI($data);
}
}
}
29 changes: 15 additions & 14 deletions Classes/Finishers/OrganizationFinisher.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
<?php
namespace GradinaruFelix\Pipedrive\Finishers;

use Neos\Form\Core\Model\AbstractFinisher;
use Neos\Form\Exception\FinisherException;
use GradinaruFelix\Pipedrive\AbstractFinisher;
use GradinaruFelix\Pipedrive\Utility\Api as PipedriveApi;

/**
* This finisher sends an email to one or more recipients
* This finisher creates an organization in Pipedrive
*/

class OrganizationFinisher extends AbstractFinisher
{

const OMITTED_OPTIONS = array(
'testMode',
);
/**
* API endpoint name
* @var String
*/
protected $apiEndpoint = 'organizations';

/**
* Name of the scope
* @var String
*/
protected $scope = 'Organization';

/**
* @var array
Expand Down Expand Up @@ -42,17 +50,10 @@ protected function executeInternal()
}
}

$apiUtility = new PipedriveApi('organizations');
$apiUtility->setData($data);

$formState = $this->finisherContext->getFormRuntime()->getFormState();

if ($testMode === true) {

\Neos\Flow\var_dump($data);
} else {
$response = $apiUtility->execute();
$formState->setFormValue("Pipedrive.OrganizationFinisher.ID", $response->data->id);
print_r($formState->getFormValues());
$this->callAPI($data);
}
}
}
27 changes: 14 additions & 13 deletions Classes/Finishers/PersonFinisher.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace GradinaruFelix\Pipedrive\Finishers;

use Neos\Form\Core\Model\AbstractFinisher;
use Neos\Form\Exception\FinisherException;
use GradinaruFelix\Pipedrive\AbstractFinisher;
use GradinaruFelix\Pipedrive\Utility\Api as PipedriveApi;

/**
Expand All @@ -12,9 +12,17 @@
class PersonFinisher extends AbstractFinisher
{

const OMITTED_OPTIONS = array(
'testMode',
);
/**
* API endpoint name
* @var String
*/
protected $apiEndpoint = 'persons';

/**
* Name of the scope
* @var String
*/
protected $scope = 'Person';

/**
* @var array
Expand Down Expand Up @@ -42,17 +50,10 @@ protected function executeInternal()
}
}

$apiUtility = new PipedriveApi('persons');
$apiUtility->setData($data);

$formState = $this->finisherContext->getFormRuntime()->getFormState();

if ($testMode === true) {

\Neos\Flow\var_dump($data);
} else {
$response = $apiUtility->execute();
$formState->setFormValue("Pipedrive.PersonFinisher.ID", $response->data->id);
print_r($formState->getFormValues());
$this->callAPI($data);
}
}
}
1 change: 0 additions & 1 deletion Classes/Utility/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public function execute()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getURI());
var_dump($this->getURI());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->data);
Expand Down

0 comments on commit 01a2d5b

Please sign in to comment.