Skip to content

Commit

Permalink
Implement send sales mail
Browse files Browse the repository at this point in the history
  • Loading branch information
shimonhaga committed Dec 15, 2022
1 parent dee83e5 commit cfd6016
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 41 deletions.
7 changes: 5 additions & 2 deletions client
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ namespace Shimoning\ColorMeShopApi;
require_once __DIR__ . '/vendor/autoload.php';

echo __NAMESPACE__ . " shell\n";
echo "-----\nexample:\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([]));";

// load .env
if (\file_exists(__DIR__ . \DIRECTORY_SEPARATOR . '.env')) {
Expand All @@ -28,7 +31,7 @@ $options = new Entities\OAuth\Options(
$_ENV['CLIENT_SECRET'],
$_ENV['REDIRECT_URI'] ?? Constants\AuthRedirectUri::NO_REDIRECT,
);
$scopes = new Values\Scopes(Constants\AuthScope::all());
$scopes = new Values\Scopes(Constants\AuthScope::cases());

$sh->setScopeVariables([
'options' => $options,
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.9",
"version": "0.0.10",
"license": "MIT",
"authors": [
{
Expand Down
5 changes: 4 additions & 1 deletion src/Communicator/Errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Shimoning\ColorMeShopApi\Communicator;

use Shimoning\ColorMeShopApi\Entities\Collection;
use Shimoning\ColorMeShopApi\Entities\Error;

class Errors extends Collection
{
Expand All @@ -23,7 +24,9 @@ static public function build(Response $response): self
{
return new self(
$response,
$response->getParsedBody()['errors'] ?? [],
\array_map(function ($error) {
return new Error($error);
}, $response->getParsedBody()['errors'] ?? []),
);
}
}
13 changes: 7 additions & 6 deletions src/Communicator/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use GuzzleHttp\Client;

// TODO: support PSR-7
class Request
{
private Options $_options;
Expand Down Expand Up @@ -79,7 +78,7 @@ protected function sendRequest(string $method, string $uri, array $headers = [],
...$headers,
],
];
if (($method === 'POST' || $method === 'PUT') && !isset($headers['Content-Type'])) {
if (!isset($headers['Content-Type']) && ($method === 'POST' || $method === 'PUT')) {
if ($this->_options->isForm()) {
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
} else if ($this->_options->isJson()) {
Expand All @@ -89,8 +88,7 @@ protected function sendRequest(string $method, string $uri, array $headers = [],
if (!empty($data)) {
if ($this->_options->isForm()) {
$options['form_params'] = $data;
} else
if ($this->_options->isJson()) {
} else if ($this->_options->isJson()) {
$options['json'] = $data;
} else {
$options['body'] = $data;
Expand All @@ -101,11 +99,14 @@ protected function sendRequest(string $method, string $uri, array $headers = [],
$response = $this->_client->request(
$method,
$uri,
$options
$options,
);

// レスポンスを返す
return new Response($response);
return new Response(
$response,
new RequestMeta($method, $uri, $options),
);
}

/**
Expand Down
33 changes: 33 additions & 0 deletions src/Communicator/RequestMeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Shimoning\ColorMeShopApi\Communicator;

class RequestMeta
{
private string $_method;
private string $_uri;
private array $_options;

public function __construct(
string $method,
string $uri,
array $options,
) {
$this->_method = $method;
$this->_uri = $uri;
$this->_options = $options;
}

public function getMethod(): string
{
return $this->_method;
}
public function getUri(): string
{
return $this->_uri;
}
public function getOptions(): array
{
return $this->_options;
}
}
23 changes: 16 additions & 7 deletions src/Communicator/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@

class Response
{
private RequestMeta $_requestMeta;
private array $_rawHeader = [];
private string $_rawBody = '';
private array $_parsedBody;
private ?array $_parsedBody;
private int $_status;

/**
* @param \Psr\Http\Message\ResponseInterface $response
*/
public function __construct(ResponseInterface $response)
{
public function __construct(
ResponseInterface $response,
RequestMeta $requestMeta,
) {
$this->_requestMeta = $requestMeta;
$this->_rawHeader = $response->getHeaders();
$this->_status = $response->getStatusCode();

Expand All @@ -27,9 +31,9 @@ public function __construct(ResponseInterface $response)
/**
* ボディをパースする
* @param string $body
* @return array
* @return ?array
*/
private function parse(string $body): array
private function parse(string $body): ?array
{
// $utf8Body = \mb_convert_encoding($body, 'UTF-8', 'SJIS');
return \json_decode($body, true);
Expand All @@ -55,9 +59,9 @@ public function getRawBody(): string

/**
* パース後のボディ
* @return array
* @return array|null
*/
public function getParsedBody(): array
public function getParsedBody(): ?array
{
return $this->_parsedBody;
}
Expand All @@ -79,4 +83,9 @@ public function isSuccess(): bool
{
return 200 <= $this->_status && $this->_status < 300;
}

public function getRequestMeta(): RequestMeta
{
return $this->_requestMeta;
}
}
11 changes: 0 additions & 11 deletions src/Constants/AuthScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,4 @@ enum AuthScope: string
case READ_SALES = 'read_sales';
case WRITE_SALES = 'write_sales';
case READ_SHOP_COUPONS = 'read_shop_coupons';

static public function all()
{
return [
self::READ_PRODUCTS,
self::WRITE_PRODUCTS,
self::READ_SALES,
self::WRITE_SALES,
self::READ_SHOP_COUPONS,
];
}
}
19 changes: 19 additions & 0 deletions src/Constants/ErrorCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Shimoning\ColorMeShopApi\Constants;

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

static public function message()
{
return [
self::NOT_FOUND => 'レコードが見つかりませんでした。',
// self::VALIDATE_ERROR_STOCK => '',
self::VALIDATE_ERROR_FIELD => 'パラメータが指定されていません。',
];
}
}
19 changes: 19 additions & 0 deletions src/Constants/MailType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Shimoning\ColorMeShopApi\Constants;

enum MailType: string
{
case ACCEPTED = 'accepted';
case PAID = 'paid';
case DELIVERED = 'delivered';

public function name(): string
{
return match ($this) {
self::ACCEPTED => '受注メール',
self::PAID => '入金確認メール',
self::DELIVERED => '商品発送メール',
};
}
}
10 changes: 10 additions & 0 deletions src/Entities/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Error extends Entity
{
protected string $code;
protected string $message;
protected ?string $field;
protected int $status;

/**
Expand All @@ -26,6 +27,15 @@ public function getMessage(): string
return $this->message;
}

/**
* 対象フィールドを取得
* @return string|null
*/
public function getField(): ?string
{
return $this->field;
}

/**
* ステータスコードを取得
* @return string
Expand Down
62 changes: 49 additions & 13 deletions src/Services/Sales.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
use Shimoning\ColorMeShopApi\Communicator\Request;
use Shimoning\ColorMeShopApi\Communicator\Options as RequestOption;
use Shimoning\ColorMeShopApi\Communicator\Errors;

use Shimoning\ColorMeShopApi\Entities\Sales\SearchParameters;
use Shimoning\ColorMeShopApi\Entities\Sales\Sale;
use Shimoning\ColorMeShopApi\Entities\Sales\Stat;
use Shimoning\ColorMeShopApi\Entities\Sales\SaleUpdater;
use Shimoning\ColorMeShopApi\Entities\Collection;
use Shimoning\ColorMeShopApi\Entities\Pagination;
use Shimoning\ColorMeShopApi\Entities\Page;
use Shimoning\ColorMeShopApi\Entities\Sales\SaleUpdater;
use Shimoning\ColorMeShopApi\Constants\MailType;

class Sales
{
Expand Down Expand Up @@ -53,11 +53,11 @@ public function page(

/**
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/sale/operation/getSale
* @param string $id
* @param int|string $id
* @param string|null $accessToken
* @return Sale|Errors
*/
public function one(string $id, ?string $accessToken = null): Sale|Errors
public function one(int|string $id, ?string $accessToken = null): Sale|Errors
{
$response = (new Request(new RequestOption([
'authorization' => $accessToken ?? $this->_accessToken,
Expand All @@ -75,10 +75,13 @@ public function one(string $id, ?string $accessToken = null): Sale|Errors
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/sale/operation/statSale
* @param DateTimeInterface $dateTime
* @param string|null $accessToken
* @param string|null $accessToken
* @return Stat|Errors
*/
public function stat(DateTimeInterface $dateTime, ?string $accessToken = null): Stat|Errors
{
public function stat(
DateTimeInterface $dateTime,
?string $accessToken = null,
): Stat|Errors {
$response = (new Request(new RequestOption([
'authorization' => $accessToken ?? $this->_accessToken,
])))->get(
Expand All @@ -97,10 +100,13 @@ public function stat(DateTimeInterface $dateTime, ?string $accessToken = null):
/**
* @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 update(SaleUpdater $updater): Sale|Errors
{
public function update(
SaleUpdater $updater,
?string $accessToken = null,
): Sale|Errors {
$response = (new Request(new RequestOption([
'authorization' => $accessToken ?? $this->_accessToken,
'json' => true,
Expand All @@ -119,12 +125,16 @@ public function update(SaleUpdater $updater): Sale|Errors

/**
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/sale/operation/cancelSale
* @param string $id
* @param int|string $id
* @param bool|null $restock
* @param string|null $accessToken
* @return Sale|Errors
*/
public function cancel(string $id, ?bool $restock = false): Sale|Errors
{
public function cancel(
int|string $id,
?bool $restock = false,
?string $accessToken = null,
): Sale|Errors {
$response = (new Request(new RequestOption([
'authorization' => $accessToken ?? $this->_accessToken,
'json' => true,
Expand All @@ -141,6 +151,32 @@ public function cancel(string $id, ?bool $restock = false): Sale|Errors
return new Sale($data['sale'] ?? []);
}

// TODO: implement
public function sendMail() {}
/**
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/sale/operation/sendSalesMail
* @param int|string $id
* @param MailType $mailType
* @param string|null $accessToken
* @return true|Errors
*/
public function sendMail(
int|string $id,
MailType $mailType,
?string $accessToken = null,
): bool|Errors {
$response = (new Request(new RequestOption([
'authorization' => $accessToken ?? $this->_accessToken,
'json' => true,
])))->post(
'https://api.shop-pro.jp/v1/sales/' . $id . '/mails',
[
'mail' => [
'type' => $mailType->value,
],
],
);
if (! $response->isSuccess()) {
return Errors::build($response);
}
return true;
}
}

0 comments on commit cfd6016

Please sign in to comment.