Skip to content

Commit

Permalink
Add client class
Browse files Browse the repository at this point in the history
  • Loading branch information
shimonhaga committed Dec 15, 2022
1 parent 2f9fecc commit 4e6aeba
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 18 deletions.
14 changes: 6 additions & 8 deletions client
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ namespace Shimoning\ColorMeShopApi;
require_once __DIR__ . '/vendor/autoload.php';

echo __NAMESPACE__ . " shell\n";
echo "-----\n - example1: for OAuth\n";
echo "\$oAuth = new Services\OAuth(\$options);\n";
echo "\$uri = \$oAuth->getOAuthUrl(\$scopes);\n";
echo "\$accessToken = \$oAuth->exchangeCode2Token(\$code);\n";
echo "echo \$accessToken->getAccessToken();\n";
echo "-----\n - example2: for Sales\n";
echo "\$salesService = new Services\Sales(\$token);";
echo "\$salesPage = \$salesService->page(new Entities\Sales\SearchParameters([]));";
echo "-----\nexample:\n";
echo "\$client = new Client;\n\n";
echo "// get url for OAuth\n";
echo "\$oAuthUrl = \$client->getOAuthUrl(\$scopes);\n\n";
echo "// get sales\n";
echo "\$sales = \$client->getSales();\n";

// load .env
if (\file_exists(__DIR__ . \DIRECTORY_SEPARATOR . '.env')) {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"minimum-stability": "dev",
"prefer-stable": true,
"version": "0.0.11",
"version": "0.1.0",
"license": "MIT",
"authors": [
{
Expand Down
139 changes: 139 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

namespace Shimoning\ColorMeShopApi;

use Shimoning\ColorMeShopApi\Communicator\Errors;
use Shimoning\ColorMeShopApi\Constants\MailType;
use Shimoning\ColorMeShopApi\Exceptions\ParameterException;
use Shimoning\ColorMeShopApi\Entities\Page;

use Shimoning\ColorMeShopApi\Services\OAuth;
use Shimoning\ColorMeShopApi\Entities\OAuth\Options as OAuthOptions;
use Shimoning\ColorMeShopApi\Values\Scopes;

use Shimoning\ColorMeShopApi\Services\Sales;
use Shimoning\ColorMeShopApi\Entities\Sales\Sale;
use Shimoning\ColorMeShopApi\Entities\Sales\SearchParameters;
use Shimoning\ColorMeShopApi\Entities\Sales\SaleUpdater;
use Shimoning\ColorMeShopApi\Entities\Sales\Stat as SaleStat;

class Client
{
/**
* OAuthアプリケーションの登録のための URL を取得する
*
* @link https://developer.shop-pro.jp/docs/colorme-api#section/API/%E5%88%A9%E7%94%A8%E6%89%8B%E9%A0%86#oauth%E3%82%A2%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%81%AE%E7%99%BB%E9%8C%B2
* @param OAuthOptions $options
* @param Scopes $scopes
* @return string
*/
public function getOAuthUrl(OAuthOptions $options, Scopes $scopes): string
{
return (new OAuth($options))->getUrl($scopes);
}

/**
* 認可コードをアクセストークンに交換
*
* @link https://developer.shop-pro.jp/docs/colorme-api#section/API/%E5%88%A9%E7%94%A8%E6%89%8B%E9%A0%86#%E8%AA%8D%E5%8F%AF%E3%82%B3%E3%83%BC%E3%83%89%E3%82%92%E3%82%A2%E3%82%AF%E3%82%BB%E3%82%B9%E3%83%88%E3%83%BC%E3%82%AF%E3%83%B3%E3%81%AB%E4%BA%A4%E6%8F%9B
* @param OAuthOptions $options
* @param string $code
* @return AccessToken|Errors
*/
public function exchangeCode2Token(OAuthOptions $options, string $code): AccessToken|Errors
{
return (new OAuth($options))->exchangeCode2Token($code);
}

/**
* 受注データのリストを取得
*
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/sale/operation/getSales
* @param SearchParameters|null $searchParameters
* @param string|null $accessToken
* @return Page<Sale>|Errors
*/
public function getSales(?SearchParameters $searchParameters = null, ?string $accessToken = null): Page|Errors
{
return $this->salesService($accessToken)->page($searchParameters ?? new SearchParameters([]), $accessToken);
}

/**
* 売上集計の取得
*
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/sale/operation/statSale
* @param \DateTimeInterface $dateTime
* @param string|null $accessToken
* @return SaleStat|Errors
*/
public function statSales(\DateTimeInterface $dateTime, ?string $accessToken = null): SaleStat|Errors
{
return $this->salesService($accessToken)->stat($dateTime, $accessToken);
}

/**
* 受注データの取得
*
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/sale/operation/getSale
* @param integer|string $id
* @param string|null $accessToken
* @return Sale|Errors
*/
public function getSale(int|string $id, ?string $accessToken = null): Sale|Errors
{
return $this->salesService($accessToken)->one($id, $accessToken);
}

/**
* 受注データの更新
*
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/sale/operation/updateSale
* @param SaleUpdater $updater
* @param string|null $accessToken
* @return Sale|Errors
*/
public function updateSale(SaleUpdater $updater, ?string $accessToken = null): Sale|Errors
{
return $this->salesService($accessToken)->update($updater, $accessToken);
}

/**
* 受注のキャンセル
*
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/sale/operation/cancelSale
* @param integer|string $id
* @param boolean|null $restock
* @param string|null $accessToken
* @return Sale|Errors
*/
public function cancelSale(int|string $id, ?bool $restock = false, ?string $accessToken = null): Sale|Errors
{
return $this->salesService($accessToken)->cancel($id, $restock, $accessToken);
}

/**
* メールの送信
*
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/sale/operation/sendSalesMail
* @param integer|string $id
* @param MailType $mailType
* @param string|null $accessToken
* @return boolean|Errors
*/
public function sendSalesMail(int|string $id, MailType $mailType, ?string $accessToken = null): bool|Errors
{
return $this->salesService($accessToken)->sendMail($id, $mailType, $accessToken);
}

private function salesService(?string $accessToken = null): Sales
{
static $service;
if (!$service) {
if (empty($accessToken)) {
throw new ParameterException('アクセストークンは必ず指定してください');
}
$service = new Sales($accessToken);
}
return $service;
}
}
6 changes: 4 additions & 2 deletions src/Constants/ErrorCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

enum ErrorCode: string
{
case NOT_FOUND = 404100;
case UNAUTHORIZED = 401010;
case NOT_FOUND = 404100;
// case VALIDATE_ERROR_STOCK = 422022;
case VALIDATE_ERROR_FIELD = 422210;
case VALIDATE_ERROR_FIELD = 422210;

static public function message()
{
return [
self::UNAUTHORIZED => 'このリソースにアクセスできません。有効なアクセストークンが見つからないか、必要なスコープが付与されていません。',
self::NOT_FOUND => 'レコードが見つかりませんでした。',
// self::VALIDATE_ERROR_STOCK => '',
self::VALIDATE_ERROR_FIELD => 'パラメータが指定されていません。',
Expand Down
10 changes: 5 additions & 5 deletions src/Services/OAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Shimoning\ColorMeShopApi\Communicator\Request;
use Shimoning\ColorMeShopApi\Communicator\RequestOptions;
use Shimoning\ColorMeShopApi\Communicator\Errors;
use Shimoning\ColorMeShopApi\Entities\OAuth\Options as OAuthOptions;
use Shimoning\ColorMeShopApi\Entities\OAuth\AccessToken;
use Shimoning\ColorMeShopApi\Values\Scopes;
Expand All @@ -23,7 +24,7 @@ public function __construct(OAuthOptions $options)
* @param Scopes $scopes
* @return string
*/
public function getOAuthUrl(Scopes $scopes): string
public function getUrl(Scopes $scopes): string
{
return $this->_options->getEndpointUri() . '/authorize?' . \http_build_query([
'client_id' => $this->_options->getClientId(),
Expand All @@ -37,9 +38,9 @@ public function getOAuthUrl(Scopes $scopes): string
* 認可コードをアクセストークンに交換する
*
* @param string $code
* @return AccessToken|bool
* @return AccessToken|Errors
*/
public function exchangeCode2Token(string $code): AccessToken|bool
public function exchangeCode2Token(string $code): AccessToken|Errors
{
$response = (new Request(new RequestOptions(['form' => true])))->post(
$this->_options->getEndpointUri() . '/token',
Expand All @@ -52,8 +53,7 @@ public function exchangeCode2Token(string $code): AccessToken|bool
]
);
if (! $response->isSuccess()) {
// TODO: return error instance
return false;
return Errors::build($response);
}
return new AccessToken($response->getParsedBody());
}
Expand Down
3 changes: 1 addition & 2 deletions src/Services/Sales.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Shimoning\ColorMeShopApi\Services;

use DateTimeInterface;
use Shimoning\ColorMeShopApi\Communicator\Request;
use Shimoning\ColorMeShopApi\Communicator\RequestOptions;
use Shimoning\ColorMeShopApi\Communicator\Errors;
Expand Down Expand Up @@ -79,7 +78,7 @@ public function one(int|string $id, ?string $accessToken = null): Sale|Errors
* @return Stat|Errors
*/
public function stat(
DateTimeInterface $dateTime,
\DateTimeInterface $dateTime,
?string $accessToken = null,
): Stat|Errors {
$response = (new Request(new RequestOptions([
Expand Down

0 comments on commit 4e6aeba

Please sign in to comment.