refactor
This commit is contained in:
50
src/Client.php
Normal file
50
src/Client.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
class Client{
|
||||
|
||||
public function __construct($url,$user,$pass,$debug=false){
|
||||
$this->url=$url;
|
||||
$this->user=$user;
|
||||
$this->pass=$pass;
|
||||
$this->debug=$debug;
|
||||
}
|
||||
|
||||
public function getRepo($user,$repo){
|
||||
return new Repo($this,$user,$repo);
|
||||
}
|
||||
|
||||
public function request($url, $type='GET', $postFields = null){
|
||||
if(substr($url,0,4)!='http'){
|
||||
$url=$this->url.$url;
|
||||
}
|
||||
if($this->debug){
|
||||
echo ">> $type $url ".json_encode($postFields)." <<\n";
|
||||
}
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, 'james/GiteaBot');
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||
|
||||
if($type!='GET'){
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
|
||||
}
|
||||
|
||||
if (!is_null($postFields)) {
|
||||
$postFields = json_encode($postFields);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json',
|
||||
'Content-Length: '.strlen($postFields), ]);
|
||||
}
|
||||
if($this->user) {
|
||||
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
||||
curl_setopt($ch, CURLOPT_USERPWD, $this->user.':'.$this->pass);
|
||||
}
|
||||
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$result = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
return json_decode($result);
|
||||
}
|
||||
}
|
49
src/Issue.php
Normal file
49
src/Issue.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
class Issue{
|
||||
|
||||
public function __construct(Repo $repo,$data){
|
||||
$this->repo=$repo;
|
||||
$this->data=$data;
|
||||
}
|
||||
|
||||
public function request($url, $type='GET', $postFields = null){
|
||||
return $this->repo->request($url, $type, $postFields);
|
||||
}
|
||||
|
||||
public function getData(){
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function getRepo(){
|
||||
return $this->repo;
|
||||
}
|
||||
|
||||
public function save(){
|
||||
return $this->request($this->data->url,'PATCH',$this->data);
|
||||
}
|
||||
|
||||
public function addLabel($label){
|
||||
$args=['labels'=>[$label->getData()->id]];
|
||||
return $this->request($this->data->url.'/labels','POST',$args);
|
||||
}
|
||||
|
||||
public function removeLabel($label){
|
||||
$id=$label->getData()->id;
|
||||
return $this->request($this->data->url."/labels/$id",'DELETE');
|
||||
}
|
||||
|
||||
public function addComment($body){
|
||||
$args=['body'=>$body];
|
||||
return $this->request($this->data->url.'/comments','POST',$args);
|
||||
}
|
||||
|
||||
public function hasLabel($label){
|
||||
$id=$label->getData()->id;
|
||||
foreach($this->data->labels as $label){
|
||||
if($label->id==$id){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
17
src/Label.php
Normal file
17
src/Label.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
class Label{
|
||||
|
||||
public function __construct(Repo $repo,$data){
|
||||
$this->repo=$repo;
|
||||
$this->data=$data;
|
||||
}
|
||||
|
||||
public function getData(){
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function getRepo(){
|
||||
return $this->repo;
|
||||
}
|
||||
|
||||
}
|
42
src/Repo.php
Normal file
42
src/Repo.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
class Repo{
|
||||
|
||||
public function __construct(Client $client,$user,$repo){
|
||||
$this->client=$client;
|
||||
$this->user=$user;
|
||||
$this->repo=$repo;
|
||||
}
|
||||
|
||||
public function request($url, $type='GET', $postFields = null){
|
||||
return $this->client->request($url, $type, $postFields);
|
||||
}
|
||||
|
||||
public function forIssues($callback,$args=null){
|
||||
$page=1;
|
||||
while(1){
|
||||
$url="repos/{$this->user}/{$this->repo}/issues?page=$page";
|
||||
if($args){
|
||||
$url.='&'.http_build_query($args);
|
||||
}
|
||||
$issues=$this->client->request($url);
|
||||
if(!$issues) break;
|
||||
foreach($issues as $data){
|
||||
$issue=new Issue($this,$data);
|
||||
call_user_func($callback,$issue);
|
||||
}
|
||||
$page++;
|
||||
}
|
||||
}
|
||||
|
||||
public function getLabelByName($name){
|
||||
$url="repos/{$this->user}/{$this->repo}/labels";
|
||||
$data=$this->client->request($url);
|
||||
foreach($data as $datum){
|
||||
if($datum->name==$name){
|
||||
return new Label($this,$datum);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user