Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,20 @@ composer require itigoppo/backlog-api
# Usage

```php
$backlog = new Backlog(new ApiKeyConnector('Your Backlog Space ID', 'Your API KEY'[, string $domain = 'jp']));
// api keyでのログイン
// .jpドメイン
$config = new BacklogJpConfigure([
'space_id' => 'Your Backlog Space ID',
'api_key' => 'Your API KEY',
]);
$backlog = (new Backlog($config))->newClient();

// .comドメイン
$config = new BacklogComConfigure([
'space_id' => 'Your Backlog Space ID',
'api_key' => 'Your API KEY',
]);
$backlog = (new Backlog($config))->newClient();
```

$domain部分、お使いのスペースに合わせて変更してください。
Expand Down
44 changes: 43 additions & 1 deletion src/Backlog/Backlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Itigoppo\BacklogApi\Backlog;

use Itigoppo\BacklogApi\Connector\ApiKeyConnector;
use Itigoppo\BacklogApi\Connector\Configure\Configure;
use Itigoppo\BacklogApi\Connector\Connector;
use Itigoppo\BacklogApi\Connector\OAuthConnector;
use Itigoppo\BacklogApi\Exception\BacklogException;

/**
Expand Down Expand Up @@ -52,11 +55,50 @@ class Backlog
/** @var null|Watchings */
protected $_watchings = null;

public function __construct($connector)
/**
* Backlog constructor.
* @param \Itigoppo\BacklogApi\Connector\Configure\Configure $config
* @throws BacklogException
*/
public function __construct(Configure $config)
{
if (empty($config->space_id)) {
throw new BacklogException('space_id must not be null');
}

$connector = null;
if (!empty($config->api_key)) {
$connector = new ApiKeyConnector($config);
} else {
$connector = new OAuthConnector($config);
}

$this->connector = $connector;
}

/**
* @return $this
* @throws BacklogException
*/
public function newClient()
{
$this->connector->setClient();

return $this;
}

/**
* @return string|null
*/
public function getOAuthAuthorizationURL()
{
if ($this->connector instanceof OAuthConnector) {
return $this->connector->getOAuthAuthorizationURL();
}

return null;
}

/**
* 状態一覧の取得
*
Expand Down
30 changes: 24 additions & 6 deletions src/Connector/ApiKeyConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,41 @@
namespace Itigoppo\BacklogApi\Connector;

use GuzzleHttp\Client;
use Itigoppo\BacklogApi\Connector\Configure\Configure;
use Itigoppo\BacklogApi\Exception\BacklogException;

class ApiKeyConnector extends Connector
{
/** @var Client */
protected $client;
private $client;

/** @var string */
protected $api_key;
private $base_uri;

public function __construct($space_id, $api_key, $domain = 'jp')
/** @var string */
private $api_key;

/**
* ApiKeyConnector constructor.
*
* @param Configure $config
* @throws BacklogException
*/
public function __construct(Configure $config)
{
if (empty($config->api_key)) {
throw new BacklogException('api_key must not be null');
}

$this->base_uri = $config->getBaseApiURL();
$this->api_key = $config->api_key;
}

public function setClient()
{
$this->client = new Client([
'base_uri' => sprintf(self::API_URL, $space_id, $domain)
'base_uri' => $this->base_uri,
]);

$this->api_key = $api_key;
}

public function get($path, $form_params = [], $query_params = [], $headers = [])
Expand Down
21 changes: 21 additions & 0 deletions src/Connector/Configure/BacklogComConfigure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Itigoppo\BacklogApi\Connector\Configure;

class BacklogComConfigure extends Configure
{
public function getBaseApiURL()
{
return sprintf('https://%1$s.backlog.com/api/v2/', $this->space_id);
}

public function getBaseOAuthAuthorizationURL()
{
return sprintf('https://%1$s.backlog.com/OAuth2AccessRequest.action', $this->space_id);
}

public function getBaseOAuthAccessTokenURL()
{
return sprintf('https://%1$s.backlog.com/api/v2/oauth2/token', $this->space_id);
}
}
21 changes: 21 additions & 0 deletions src/Connector/Configure/BacklogJpConfigure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Itigoppo\BacklogApi\Connector\Configure;

class BacklogJpConfigure extends Configure
{
public function getBaseApiURL()
{
return sprintf('https://%1$s.backlog.jp/api/v2/', $this->space_id);
}

public function getBaseOAuthAuthorizationURL()
{
return sprintf('https://%1$s.backlog.jp/OAuth2AccessRequest.action', $this->space_id);
}

public function getBaseOAuthAccessTokenURL()
{
return sprintf('https://%1$s.backlog.jp/api/v2/oauth2/token', $this->space_id);
}
}
58 changes: 58 additions & 0 deletions src/Connector/Configure/Configure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Itigoppo\BacklogApi\Connector\Configure;

use Itigoppo\BacklogApi\Exception\BacklogException;
use Itigoppo\BacklogApi\Traits\PrivateGetterTrait;

/**
* @property-read string $space_id
* @property-read string $api_key
* @property-read string $access_token
* @property-read string $client_id
* @property-read string $client_secret
* @property-read string $redirect_uri
* @property-read string $state
* @property-read string $authorization_code
* @property-read string $refresh_token
*/
abstract class Configure implements ConfigureInterface
{
use PrivateGetterTrait;

/** @var string */
private $space_id;
/** @var string */
private $api_key;
/** @var string */
private $access_token;
/** @var string */
private $client_id;
/** @var string */
private $client_secret;
/** @var string */
private $redirect_uri;
/** @var string */
private $state;
/** @var string */
private $authorization_code;
/** @var string */
private $refresh_token;

/**
* Configure constructor.
* @param array $options
*/
public function __construct($options)
{
$this->space_id = isset($options['space_id']) ? $options['space_id'] : null;
$this->api_key = isset($options['api_key']) ? $options['api_key'] : null;
$this->access_token = isset($options['access_token']) ? $options['access_token'] : null;
$this->client_id = isset($options['client_id']) ? $options['client_id'] : null;
$this->client_secret = isset($options['client_secret']) ? $options['client_secret'] : null;
$this->redirect_uri = isset($options['redirect_uri']) ? $options['redirect_uri'] : null;
$this->state = isset($options['state']) ? $options['state'] : null;
$this->authorization_code = isset($options['authorization_code']) ? $options['authorization_code'] : null;
$this->refresh_token = isset($options['refresh_token']) ? $options['refresh_token'] : null;
}
}
27 changes: 27 additions & 0 deletions src/Connector/Configure/ConfigureInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Itigoppo\BacklogApi\Connector\Configure;

interface ConfigureInterface
{
/**
* base api url
*
* @return string
*/
public function getBaseApiURL();

/**
* base OAuth authorization url
*
* @return string
*/
public function getBaseOAuthAuthorizationURL();

/**
* base OAuth access token url
*
* @return string
*/
public function getBaseOAuthAccessTokenURL();
}
1 change: 0 additions & 1 deletion src/Connector/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

abstract class Connector implements ConnectorInterface
{
const API_URL = 'https://%1$s.backlog.%2$s/api/v2/';
}
6 changes: 6 additions & 0 deletions src/Connector/ConnectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
*/
interface ConnectorInterface
{

/**
* @return void
*/
public function setClient();

/**
* Get Request
*
Expand Down
Loading