Skip to content

Commit

Permalink
Merge pull request #9 from Keanor/master
Browse files Browse the repository at this point in the history
fix #8
  • Loading branch information
lesstif committed Aug 15, 2015
2 parents 262c4be + ca8b057 commit 662efac
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 67 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ vendor/
# composer.lock


### Vim ###
### Editor (Vim, PhpStorm) ###
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
*.un~
Session.vim
.netrwhist
*~
.idea/

composer.lock
config.php
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lesstif/php-jira-rest-client",
"description": "JIRA REST API Client for PHP Users.",
"type": "library",
"type": "library",
"keywords": ["jira", "jira-php", "jira-rest"],
"require": {
"php": ">=5.4.0",
Expand Down
12 changes: 6 additions & 6 deletions src/Issue/IssueField.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function getIssueType()
/** @var object */
public $status;

/** @var object[] */
/** @var array */
public $labels;

/** @var \JiraRestApi\Project\Project */
Expand All @@ -185,7 +185,7 @@ public function getIssueType()
/** @var string */
public $environment;

/** @var string[] */
/** @var array */
public $components;

/** @var Comments */
Expand All @@ -197,7 +197,7 @@ public function getIssueType()
/** @var object */
public $resolution;

/** @var string[] */
/** @var array */
public $fixVersions;

/** @var Reporter */
Expand Down Expand Up @@ -230,13 +230,13 @@ public function getIssueType()
/** @var string */
public $resolutiondate;

/** @var DateTime */
/** @var \DateTime */
public $duedate;

/** @var object[] */
/** @var array */
public $issuelinks;

/** @var object[] */
/** @var array */
public $subtasks;

/** @var int */
Expand Down
208 changes: 149 additions & 59 deletions src/JiraClient.php
Original file line number Diff line number Diff line change
@@ -1,91 +1,115 @@
<?php

namespace JiraRestApi;

class JIRAException extends \Exception
{
}

use Monolog\Logger as Logger;
use Monolog\Handler\StreamHandler;

use Dotenv;

/**
* interact jira server with REST API.
* Interact jira server with REST API.
*/
class JiraClient
{
/* @var json mapper */
/**
* Json Mapper
*
* @var \JsonMapper
*/
protected $json_mapper;

/** @var HTTP response code */
/**
* HTTP response code
*
* @var string
*/
protected $http_response;

/** @var JIRA REST API URI */
/**
* JIRA REST API URI
*
* @var string
*/
private $api_uri = '/rest/api/2';

/** @var CURL instrance */
/**
* CURL instance
*
* @var resource
*/
protected $curl;

/** @var jira host */
/**
* Jira host
*
* @var string
*/
protected $host;
/** @var jira username */

/**
* Jira username
*
* @var string
*/
protected $username;
/** @var jira password */

/**
* Jira password
*
* @var string
*/
protected $password;

/** @var Monolog instance */
/**
* Monolog instance
*
* @var \Monolog\Logger
*/
protected $log;

// disable SSL Certification validation
/**
* Disable SSL Certification validation
*
* @var bool
*/
protected $CURLOPT_SSL_VERIFYHOST = false;
// FALSE to stop CURL from verifying the peer's certificate.

/**
* FALSE to stop CURL from verifying the peer's certificate.
*
* @var bool
*/
protected $CURLOPT_SSL_VERIFYPEER = false;

// debug curl
/**
* debug curl
*
* @var bool
*/
protected $CURLOPT_VERBOSE = false;

/**
* Log filename
*
* @var string
*/
protected $LOG_FILE;
protected $LOG_LEVEL;

private function convertLogLevel($log_level)
{
if ($log_level == 'DEBUG') {
return Logger::DEBUG;
} elseif ($log_level == 'INFO') {
return Logger::DEBUG;
} elseif ($log_level == 'WARNING') {
return Logger::WARNING;
} elseif ($log_level == 'ERROR') {
return Logger::ERROR;
} else {
return Logger::WARNING;
}
}

// serilize only not null field.
protected function filterNullVariable($haystack)
{
foreach ($haystack as $key => $value) {
if (is_array($value)) {
$haystack[$key] = $this->filterNullVariable($haystack[$key]);
} elseif (is_object($value)) {
$haystack[$key] = $this->filterNullVariable(get_class_vars(get_class($value)));
}

if (is_null($haystack[$key]) || empty($haystack[$key])) {
unset($haystack[$key]);
}
}

return $haystack;
}
/**
* Log level
*
* @var int
*/
protected $LOG_LEVEL;

/**
* Constructor
*
* @param string $path Path with dotenv file
*/
public function __construct($path = '.')
{
{
Dotenv::load($path);

// not available in dotenv 1.1
// $dotenv->required(['JIRA_HOST', 'JIRA_USER', 'JIRA_PASS']);

Expand All @@ -111,6 +135,61 @@ public function __construct($path = '.')
$this->http_response = 200;
}

/**
* Convert log level
*
* @param $log_level
*
* @return int
*/
private function convertLogLevel($log_level)
{
switch ($log_level) {
case 'DEBUG':
return Logger::DEBUG;
case 'INFO':
return Logger::INFO;
case 'ERROR':
return Logger::ERROR;
default:
return Logger::WARNING;
}
}

/**
* Serilize only not null field
*
* @param array $haystack
*
* @return array
*/
protected function filterNullVariable($haystack)
{
foreach ($haystack as $key => $value) {
if (is_array($value)) {
$haystack[$key] = $this->filterNullVariable($haystack[$key]);
} elseif (is_object($value)) {
$haystack[$key] = $this->filterNullVariable(get_class_vars(get_class($value)));
}

if (is_null($haystack[$key]) || empty($haystack[$key])) {
unset($haystack[$key]);
}
}

return $haystack;
}

/**
* Execute REST request
*
* @param string $context Rest API context (ex.:issue, search, etc..)
* @param null $post_data
* @param null $custom_request
*
* @return string
* @throws JIRAException
*/
public function exec($context, $post_data = null, $custom_request = null)
{
$url = $this->host.$this->api_uri.'/'.preg_replace('/\//', '', $context, 1);
Expand Down Expand Up @@ -163,7 +242,7 @@ public function exec($context, $post_data = null, $custom_request = null)

// HostNotFound, No route to Host, etc Network error
$this->log->addError('CURL Error: = '.$body);
throw new JIRAException('CURL Error: = '.$body);
throw new JiraException('CURL Error: = '.$body);
} else {
// if request was ok, parsing http response code.
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
Expand All @@ -172,7 +251,7 @@ public function exec($context, $post_data = null, $custom_request = null)

// don't check 301, 302 because setting CURLOPT_FOLLOWLOCATION
if ($this->http_response != 200 && $this->http_response != 201) {
throw new JIRAException('CURL HTTP Request Failed: Status Code : '
throw new JiraException('CURL HTTP Request Failed: Status Code : '
.$this->http_response.', URL:'.$url
."\nError Message : ".$response, $this->http_response);
}
Expand All @@ -181,6 +260,14 @@ public function exec($context, $post_data = null, $custom_request = null)
return $response;
}

/**
* Create upload handle
*
* @param string $url Request URL
* @param string $upload_file Filename
*
* @return resource
*/
private function createUploadHandle($url, $upload_file)
{
$ch = curl_init();
Expand Down Expand Up @@ -230,10 +317,13 @@ private function createUploadHandle($url, $upload_file)
}

/**
* file upload.
* File upload
*
* @param string $context url context
* @param array $filePathArray upload file path.
*
* @param context url context
* @param filePathArray upload file path.
* @return array
* @throws JiraException
*/
public function upload($context, $filePathArray)
{
Expand Down Expand Up @@ -291,7 +381,7 @@ public function upload($context, $filePathArray)
if ($this->http_response != 200 && $this->http_response != 201) {
$body = 'CURL HTTP Request Failed: Status Code : '
.$this->http_response.', URL:'.$url
."\nError Message : ".$response;
."\nError Message : ".$response; // @TODO undefined variable $response
$this->log->addError($body);
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/JiraException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Created by PhpStorm.
* User: keanor
* Date: 14.08.15
* Time: 20:53
*/

namespace JiraRestApi;

/**
* Class JiraException
*
* @package JiraRestApi
*/
class JiraException extends \Exception
{
}

0 comments on commit 662efac

Please sign in to comment.