Skip to content

Commit

Permalink
Merge pull request #8 from continuousphp/master
Browse files Browse the repository at this point in the history
fixed problem with closed curl session by moving curl completely into…
  • Loading branch information
jguittard committed Jun 29, 2015
2 parents dcc10cf + 545a52e commit d330dae
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 46 deletions.
8 changes: 1 addition & 7 deletions src/Factory/ResourceAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,7 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $
$zohoConfig = $config['zoho'];
$resourceConfig = $config['zoho']['resources'][$requestedName];

$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Authorization: Zoho-authtoken ' . $zohoConfig['auth_token'],
'X-com-zoho-subscriptions-organizationid: ' . $zohoConfig['organization_id'],
]);
$resource = new Resource($curl);
$resource = new Resource($zohoConfig['auth_token'], $zohoConfig['organization_id']);
$resource->setPath($resourceConfig['path']);
$resource->setCollectionName($resourceConfig['collectionName']);

Expand Down
2 changes: 1 addition & 1 deletion src/Hydrator/CustomerHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Zoho\Subscriptions\Hydrator;

use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
use Zend\Stdlib\Hydrator\Strategy\DateTimeFormatterStrategy;
use Zoho\Subscriptions\Hydrator\Strategy\DateTimeFormatterStrategy;
use Zoho\Subscriptions\Hydrator\Strategy\AddressStrategy;

class CustomerHydrator extends ClassMethodsHydrator
Expand Down
82 changes: 82 additions & 0 deletions src/Hydrator/Strategy/DateTimeFormatterStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2015 Julien Guittard (http://julienguittard.com)
*/

namespace Zoho\Subscriptions\Hydrator\Strategy;

use DateTime;
use DateTimeZone;
use Zend\Stdlib\Hydrator\Strategy\StrategyInterface;

class DateTimeFormatterStrategy implements StrategyInterface
{
/**
* @var string
*/
protected $format;

/**
* @var DateTimeZone|null
*/
protected $timezone;

/**
* Constructor
*
* @param string $format
* @param DateTimeZone|null $timezone
*/
public function __construct($format = DateTime::RFC3339, DateTimeZone $timezone = null)
{
$this->format = (string) $format;
$this->timezone = $timezone;
}

/**
* {@inheritDoc}
*
* Converts to date time string
*
* @param mixed|DateTime $value
*
* @return mixed|string
*/
public function extract($value)
{
if ($value instanceof DateTime) {
return $value->format($this->format);
}

return $value;
}

/**
* Converts date time string to DateTime instance for injecting to object
*
* {@inheritDoc}
*
* @param mixed|string $value
*
* @return mixed|DateTime
*/
public function hydrate($value)
{
if ($value === '' || $value === null) {
return;
}

if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
$this->format = 'Y-m-d';
}

if ($this->timezone) {
$hydrated = DateTime::createFromFormat($this->format, $value, $this->timezone);
} else {
$hydrated = DateTime::createFromFormat($this->format, $value);
}

return $hydrated ?: $value;
}
}
121 changes: 83 additions & 38 deletions src/Service/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ class Resource implements InputFilterAwareInterface
*/
protected $curl;

/**
* @var array
*/
protected $responseInfo;

/**
* @var string
*/
protected $authToken;

/**
* @var string
*/
protected $organizationId;

/**
* @var string
*/
Expand Down Expand Up @@ -167,27 +182,80 @@ public function setHydrator(HydratorInterface $hydrator)
return $this;
}

/**
* @return string
*/
public function getAuthToken()
{
return $this->authToken;
}

/**
* @return string
*/
public function getOrganizationId()
{
return $this->organizationId;
}

/**
* Constructor
*
* @param $curl
*/
public function __construct($curl)
public function __construct($authToken, $organizationId)
{
$this->authToken = $authToken;
$this->organizationId = $organizationId;
}

protected function request($url, $method = 'GET', $body = null)
{
$this->curl = $curl;
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, [
'Authorization: Zoho-authtoken ' . $this->getAuthToken(),
'X-com-zoho-subscriptions-organizationid: ' . $this->getOrganizationId(),
]);

curl_setopt($this->curl, CURLOPT_URL, $url);

switch ($method) {
case 'POST':
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
break;
case 'PUT':
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
break;
case 'DELETE':
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
}

$result = curl_exec($this->curl);
$this->responseInfo = curl_getinfo($this->curl);
$result = json_decode($result, true);
curl_close($this->curl);

return $result;
}

protected function getLastResponseHttpCode()
{
return $this->responseInfo['http_code'];
}

/**
* @return array
*/
public function fetchAll()
{
curl_setopt($this->curl, CURLOPT_URL, self::ZOHO_API_ENDPOINT . $this->getPath());
$result = curl_exec($this->curl);
$result = json_decode($result);
curl_close($this->curl);
$result = $this->request(self::ZOHO_API_ENDPOINT . $this->getPath());

$collectionName = $this->getCollectionName();
return $result->$collectionName;
return $result[$collectionName];
}

/**
Expand All @@ -196,13 +264,7 @@ public function fetchAll()
*/
public function fetch($id)
{
curl_setopt($this->curl, CURLOPT_URL, self::ZOHO_API_ENDPOINT . $this->getPath() . '/' . $id);
$result = curl_exec($this->curl);

$api_response_info = curl_getinfo($this->curl);

$result = json_decode($result, true);
curl_close($this->curl);
$result = $this->request(self::ZOHO_API_ENDPOINT . $this->getPath() . '/' . $id);

$entityClass = $this->getEntityClass();
$entityName = $this->getEntityName();
Expand Down Expand Up @@ -231,26 +293,17 @@ public function create($data)
throw new DomainException("Unprocessable entity", 422);
}

curl_setopt($this->curl, CURLOPT_URL, self::ZOHO_API_ENDPOINT . $this->getPath());
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $json);
$result = $this->request(self::ZOHO_API_ENDPOINT . $this->getPath(), 'POST', $json);


$response = curl_exec($this->curl);
$result = json_decode($response, true);
$api_response_info = curl_getinfo($this->curl);
curl_close($this->curl);

if ($api_response_info['http_code'] == 201) {
//print_r($result);exit;
if ($this->getLastResponseHttpCode() == 201) {
$entityClass = $this->getEntityClass();
$entityName = $this->getEntityName();
$entityName = $this->getEntityName();
$entity = new $entityClass;
$data = $result[$entityName];
$entity = $this->getHydrator()->hydrate($data, $entity);
return $entity;
}
throw new DomainException($result->message, $api_response_info['http_code']);
throw new DomainException($result->message, $this->getLastResponseHttpCode());

}

Expand All @@ -272,16 +325,10 @@ public function update($id, $data)
}
$fields = http_build_query($data);

curl_setopt($this->curl, CURLOPT_URL, self::ZOHO_API_ENDPOINT . $this->getPath() . '/' . $id);
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $fields);

$result = curl_exec($this->curl);
$result = json_decode($result);
curl_close($this->curl);
$result = $this->request(self::ZOHO_API_ENDPOINT . $this->getPath() . '/' . $id, 'PUT', $fields);

$entityName = $this->getEntityName();
return $result->$entityName;
return $result[$entityName];
}

/**
Expand All @@ -290,9 +337,7 @@ public function update($id, $data)
*/
public function delete($id)
{
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($this->curl, CURLOPT_URL, self::ZOHO_API_ENDPOINT . $this->getPath() . '/' . $id);
curl_exec($this->curl);
$this->request(self::ZOHO_API_ENDPOINT . $this->getPath() . '/' . $id, 'DELETE');
return true;
}
}

0 comments on commit d330dae

Please sign in to comment.