diff --git a/src/CRM.php b/src/CRM.php index 06dcb85..9a2af91 100644 --- a/src/CRM.php +++ b/src/CRM.php @@ -3,6 +3,8 @@ use Doctrine\Common\Inflector\Inflector; use GuzzleHttp\Client; use ProsperWorks\Endpoints\Endpoint; +use GuzzleHttp\HandlerStack; +use GuzzleHttp\Middleware; /** * CRM API entry-point. @@ -71,10 +73,20 @@ abstract class CRM const RES_TASK = 'Task'; const RES_PROJECT = 'Project'; const RES_ACTIVITY = 'Activity'; - + + static $container; + public static function client() { static $client; + + self::$container = []; + $history = Middleware::history(self::$container); + + $stack = HandlerStack::create(); + // Add the history middleware to the handler stack. + $stack->push($history); + if (!$client) { $client = new Client([ 'base_uri' => 'https://api.prosperworks.com/developer_api/v1/', @@ -83,7 +95,8 @@ public static function client() 'X-PW-UserEmail' => Config::email(), 'X-PW-AccessToken' => Config::token(), 'Content-Type' => 'application/json' - ] + ], + 'handler' => $stack ]); } return $client; diff --git a/src/Endpoints/BaseEndpoint.php b/src/Endpoints/BaseEndpoint.php index 2296994..3798e5a 100644 --- a/src/Endpoints/BaseEndpoint.php +++ b/src/Endpoints/BaseEndpoint.php @@ -10,6 +10,8 @@ use ProsperWorks\RateLimit; use ProsperWorks\Resources\BareResource; use Psr\Http\Message\ResponseInterface; +use Phalcon\Logger; +use Phalcon\Logger\Adapter\File as FileLogger; /** * Provides normalization and HTTP request methods to real resources. @@ -88,6 +90,8 @@ public function getUri() { return $this->uri; } */ protected function request(string $method, $path = '', array $options = []) { + $config = $this->di->get('config'); + //TODO: replace with is_iterable() at PHP 7.1 if (is_array($path) || $path instanceof \Traversable) { return $this->requestMany($method, $path); @@ -97,7 +101,22 @@ protected function request(string $method, $path = '', array $options = []) } elseif (Config::debugLevel() >= Config::DEBUG_BASIC) { echo strtoupper($method) . " $this->uri/$path\n"; } - $response = $this->client->$method("$this->uri/$path", $options); + + try { + $response = $this->client->$method("$this->uri/$path", $options); + } catch (\RuntimeException $e) { + // Log the error and the last request info + $logger = new FileLogger($config['application']['loggingDir'] . date("d_m_y") . "-pwintegration.log"); + $error = "Exception " . $e->getMessage() . "\n"; + + $transaction = end(CRM::$container); + $error .= ( (string) $transaction['request']->getBody() ); // Hello World + foreach ($transaction['request']->getHeaders() as $header) { + $error .= print_r($header, true); + } + $logger->log("Sync exception: " . $error, Logger::INFO); + } + RateLimit::do()->pushRequest(); return $this->processResponse($response); } @@ -204,4 +223,4 @@ protected function processError(ClientException $error) { return $this->processResponse($error->getResponse()); } -} \ No newline at end of file +} diff --git a/src/Resources/Opportunity.php b/src/Resources/Opportunity.php new file mode 100644 index 0000000..4dbdbf9 --- /dev/null +++ b/src/Resources/Opportunity.php @@ -0,0 +1,16 @@ +options = $field->options ?? []; //validating $resource and options, if available - if ($resource && !in_array($resource, $this->resources)) { - throw new InvalidArg("You requested a field ($idOrName) that is not available for ($resource)."); - } - if (is_array($value) && $this->type != "MultiSelect") { throw new InvalidArg("Invalid multiple values for field $name that is not a MultiSelect field."); } + if ($resource && !in_array($resource, $this->resources)) { + throw new InvalidArg("You requested a field ($idOrName) that is not available for ($resource)."); + } + if ($this->options && $value) { if (!is_array($value)) $value = [$value]; foreach ($value as $val) { if (!is_numeric($val)) { - $valueName = $val; - $val = array_flip($this->options)[$val] ?? false; + if ($val == "") { + $valueName = ""; + $id = null; + } else { + $valueName = $val; + $id = array_flip($this->options)[$val] ?? false; + } } else { $valueName = $this->options[$val] ?? false; + $id = $val; } - if (!$val || !$valueName) { + if ( $valueName !== "" && (!$id || !$valueName) ) { $name = ($resource ? "$resource." : '') . $idOrName; $options = implode(', ', $this->options); - throw new InvalidArg("Invalid value ($value) for field $name. Valid options are: $options"); + + throw new InvalidArg("Invalid value ($val) for field $name. Valid options are: $options"); } else { - $values[] = $val; + $values[] = $id; } }