-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Create Loghy SDK * Create ci.yml (#1)
- Loading branch information
Showing
10 changed files
with
362 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name: CI | ||
|
||
on: ['push'] | ||
|
||
jobs: | ||
run-pest: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/[email protected] | ||
- uses: php-actions/composer@v5 | ||
- uses: NWBY/[email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
composer.lock | ||
vendor | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
# Loghy-PHP | ||
# Loghy-PHP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "incudata-loghy/loghy-php", | ||
"description": "Loghy PHP SDK.", | ||
"type": "library", | ||
"version": "1.0.0", | ||
"authors": [ | ||
{ | ||
"name": "INCUDATA", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Loghy\\SDK\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Loghy\\Tests\\": "tests/" | ||
} | ||
}, | ||
"require": { | ||
"guzzlehttp/guzzle": "^7.4" | ||
}, | ||
"require-dev": { | ||
"pestphp/pest": "^1.21" | ||
}, | ||
"scripts": { | ||
"test": [ | ||
"@php ./vendor/bin/pest --stop-on-failure" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
> | ||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory suffix="Test.php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">./app</directory> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</coverage> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Loghy\SDK\Contract; | ||
|
||
/** | ||
* Interface LoghyInterface | ||
*/ | ||
interface LoghyInterface | ||
{ | ||
/** | ||
* Get Loghy ID from a authentication code | ||
* | ||
* @param string $code | ||
* @return array<string,array|bool|int|string>|null | ||
*/ | ||
public function getLoghyId(string $code): ?array; | ||
|
||
/** | ||
* Get user information from a Loghy ID | ||
* | ||
* @param string $loghyId | ||
* @return array<string,array|bool|int|string>|null | ||
*/ | ||
public function getUserInfo(string $loghyId): ?array; | ||
|
||
/** | ||
* Set user ID by site to a Loghy ID | ||
* | ||
* @param string $loghyId | ||
* @param string $userId | ||
* @return array<string,bool|int|string>|null | ||
*/ | ||
public function putUserId(string $loghyId, string $userId): ?array; | ||
|
||
/** | ||
* Delete user information from a Loghy ID | ||
* | ||
* @param int|string $loghyId | ||
* @return array<string,bool|int|string>|null | ||
*/ | ||
public function deleteUserInfo(string $loghyId): ?array; | ||
|
||
/** | ||
* Delete Loghy ID | ||
* | ||
* @param int|string $loghyId | ||
* @return array<string,bool|int|string>|null | ||
*/ | ||
public function deleteLoghyId(string $loghyId): ?array; | ||
|
||
/** | ||
* Set Guzzle HTTP client | ||
* | ||
* @param \GuzzleHttp\Client $client | ||
*/ | ||
public function setHttpClient(\GuzzleHttp\Client $client): void; | ||
|
||
/** | ||
* Get Guzzle HTTP Client | ||
* | ||
* @return \GuzzleHttp\Client | ||
*/ | ||
public function httpClient(): \GuzzleHttp\Client; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Loghy\SDK; | ||
|
||
use GuzzleHttp\Client; | ||
use Loghy\SDK\Contract\LoghyInterface; | ||
|
||
/** | ||
* Class Loghy. | ||
*/ | ||
class Loghy implements LoghyInterface { | ||
|
||
/** | ||
* The Guzzle client instance. | ||
*/ | ||
protected ?Client $client; | ||
|
||
function __construct( | ||
private string $apiKey, | ||
private string $siteCode | ||
) { | ||
} | ||
|
||
/** | ||
* Get Loghy ID from a authentication code | ||
* | ||
* @param string $code | ||
* @return array<string,array|bool|int|string>|null | ||
*/ | ||
public function getLoghyId( | ||
string $code | ||
): ?array { | ||
$url = 'https://api001.sns-loghy.jp/api/' . 'loghyid'; | ||
$data = [ 'code' => $code ]; | ||
|
||
$response = $this->httpClient()->request('POST', $url, [ | ||
'form_params' => $data | ||
]); | ||
|
||
$body = (string) $response->getBody(); | ||
$content = json_decode($body, true); | ||
return $content; | ||
} | ||
|
||
/** | ||
* Get user information from a Loghy ID | ||
* | ||
* @param string $loghyId | ||
* @return array<string,array|bool|int|string>|null | ||
*/ | ||
public function getUserInfo( | ||
string $loghyId | ||
): ?array { | ||
return $this->requestApi('lgid2get', $loghyId); | ||
} | ||
|
||
/** | ||
* Set user ID by site to a Loghy ID | ||
* | ||
* @param string $loghyId | ||
* @param string $userId | ||
* @return array<string,bool|int|string>|null | ||
*/ | ||
public function putUserId( | ||
string $loghyId, | ||
string $userId | ||
): ?array { | ||
return $this->requestApi('lgid2set', $loghyId, $userId); | ||
} | ||
|
||
/** | ||
* Delete user information from a Loghy ID | ||
* | ||
* @param int|string $loghyId | ||
* @return array<string,bool|int|string>|null | ||
*/ | ||
public function deleteUserInfo( | ||
string $loghyId | ||
): ?array { | ||
return $this->requestApi('lgid2pdel', $loghyId); | ||
} | ||
|
||
/** | ||
* Delete Loghy ID | ||
* | ||
* @param int|string $loghyId | ||
* @return array<string,bool|int|string>|null | ||
*/ | ||
public function deleteLoghyId( | ||
string $loghyId | ||
): ?array { | ||
return $this->requestApi('lgid2del', $loghyId); | ||
} | ||
|
||
/** | ||
* Request API | ||
* | ||
*/ | ||
private function requestApi( | ||
string $command, | ||
string $id, | ||
string $mid = '' | ||
): ?array { | ||
$url = 'https://api001.sns-loghy.jp/api/' . $command; | ||
|
||
$atype = 'site'; | ||
$time = time(); | ||
$skey = hash( | ||
'sha256', | ||
$command . $atype . $this->siteCode . $id . $mid . $time . $this->apiKey | ||
); | ||
$data = [ | ||
'cmd' => $command, | ||
'atype' => $atype, | ||
'sid' => $this->siteCode, | ||
'id' => $id, | ||
'mid' => $mid, | ||
'time' => $time, | ||
'skey' => $skey, | ||
]; | ||
|
||
$response = $this->httpClient()->request('GET', $url, [ | ||
'query' => $data | ||
]); | ||
|
||
$body = (string) $response->getBody(); | ||
$content = json_decode($body, true); | ||
return $content; | ||
} | ||
|
||
/** | ||
* Set Guzzle HTTP client | ||
* | ||
* @param \GuzzleHttp\Client $client | ||
*/ | ||
public function setHttpClient( | ||
Client $client | ||
): void { | ||
$this->client = $client; | ||
} | ||
|
||
/** | ||
* Get Guzzle HTTP Client | ||
* | ||
* @return \GuzzleHttp\Client | ||
*/ | ||
public function httpClient(): Client | ||
{ | ||
return $this->client ?? new Client(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
dataset('loghy_id_response', function () { | ||
yield fn() => [ | ||
'result' => true, | ||
'data' => [ | ||
'lgid' => 43686, | ||
'site_id' => '1', | ||
'social_login' => 'google', | ||
'sid' => 'exampleCode', | ||
'geturl' => "https://api001.sns-loghy.jp/api/lgid2get?cmd=lgid2get&sid=examplecode&id=43686&time=1638169393&rkey=00745824&skey=6f3f43700bb2fb5ba527b719f" | ||
] | ||
]; | ||
}); | ||
|
||
dataset('ok_response', function() { | ||
yield fn() => [ | ||
'result' => true, | ||
]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
define('LOGHY_TESTS_DIR', dirname(__FILE__)); | ||
|
||
require_once join(DIRECTORY_SEPARATOR, [LOGHY_TESTS_DIR, '..', 'vendor', 'autoload.php']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
beforeEach(function(): void { | ||
$this->loghy = new Loghy\SDK\Loghy('__apiKey__', '__siteCode__'); | ||
}); | ||
|
||
test('httpClient() returns the same instance of the GuzzleHttp\Client class that was provided at setHttpClient()', function(): void { | ||
$client = new GuzzleHttp\Client(); | ||
$this->loghy->setHttpClient($client); | ||
|
||
expect($this->loghy->httpClient()) | ||
->toBeInstanceOf(GuzzleHttp\Client::class) | ||
->toEqual($client); | ||
}); | ||
|
||
test('getLoghyId() returns an array has LoghyID', function(array $responseData): void { | ||
$client = makeGuzzleJsonMockClient($responseData); | ||
$this->loghy->setHttpClient($client); | ||
|
||
expect($this->loghy->getLoghyId('__code__')) | ||
->toBeArray() | ||
->toEqual($responseData); | ||
})->with('loghy_id_response'); | ||
|
||
test('putUserId() returns an array has ok', function(array $responseData): void { | ||
$client = makeGuzzleJsonMockClient($responseData); | ||
$this->loghy->setHttpClient($client); | ||
|
||
expect($this->loghy->putUserId('__loghy_id__', '__user_id__')) | ||
->toBeArray() | ||
->toEqual($responseData); | ||
})->with('ok_response'); | ||
|
||
function makeGuzzleJsonMockClient( | ||
array $data | ||
): GuzzleHttp\Client { | ||
$mock = new GuzzleHttp\Handler\MockHandler([ | ||
new GuzzleHttp\Psr7\Response( | ||
200, | ||
['Content-Type' => 'application/json; charset=UTF-8'], | ||
json_encode($data) | ||
) | ||
]); | ||
|
||
$handlerStack = GuzzleHttp\HandlerStack::create($mock); | ||
return new GuzzleHttp\Client(['handler' => $handlerStack]); | ||
} |