class Redmine { private $service_url = 'http://meine-domain.de:3000/'; private $key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; public function createIssue( redmineIssueModel $issueModel ) { $issue = array('issue' => $issueModel ); $curl_post_data = json_encode( $issue ); $curl = curl_init( $this->service_url . 'issues.json?key=' . $this->key ); curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $curl, CURLOPT_POST, true ); curl_setopt( $curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json') ); curl_setopt( $curl, CURLOPT_POSTFIELDS, $curl_post_data ); $result = $this->execute( $curl ); return $result->issue->id; } /** * @param integer $id * @return models\redmineIssueModel * @throws Exception */ public function getIssue( $id ) { $curl = curl_init( $this->service_url . 'issues/' . $id . '.json?key=' . $this->key ); curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json') ); curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, "GET"); $result = $this->execute( $curl ); if( $result === null ) { return null; } $issueModel = new models\redmineIssueModel(); $issueModel->subject = $result->issue->subject; $issueModel->description = $result->issue->description; $issueModel->done_ratio = $result->done_ratio; $issueModel->assigned_to_id = $result->issue->assigned_to->id; $issueModel->priority_id = $result->issue->priority->id; $issueModel->status_id = $result->issue->status->id; $issueModel->tracker_id = $result->issue->tracker->id; return $issueModel; } /** * @param redmineIssueModel $issueModel * @param integer $id * @return bool * @throws Exception */ public function updateIssue( redmineIssueModel $issueModel, $id ) { $issue = array('issue' => $issueModel ); $curl_post_data = json_encode( $issue ); $curl = curl_init( $this->service_url . 'issues/' . $id . '.json?key=' . $this->key ); curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt( $curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json') ); curl_setopt( $curl, CURLOPT_POSTFIELDS, $curl_post_data ); $result = $this->execute( $curl ); if( $result === '' ) { return true; } return false; } /** * @param integer $id * @return bool * @throws Exception */ public function deleteIssue( $id ) { $curl = curl_init( $this->service_url . 'issues/' . $id . '.json?key=' . $this->key ); curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json') ); curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, "DELETE"); $result = $this->execute( $curl ); if( $result === '' ) { return true; } return false; } /** * @param $curl * @return mixed * @throws Exception */ private function execute( $curl ) { $curl_response = curl_exec( $curl ); if( $curl_response === false ) { curl_close( $curl ); throw new \Exception( 'Service Unavailable', 503 ); } curl_close( $curl ); $decoded = json_decode( $curl_response ); if( isset( $decoded->error ) ) { throw new \Exception( $decoded->error, $decoded->status ); } return $decoded; } }