Skip to content

Commit

Permalink
feat: Create Loghy SDK (#2)
Browse files Browse the repository at this point in the history
* feat: Create Loghy SDK

* Create ci.yml (#1)
  • Loading branch information
mkohei authored Dec 24, 2021
1 parent fde7060 commit cce3696
Show file tree
Hide file tree
Showing 10 changed files with 362 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
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]
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
vendor
.phpunit.result.cache
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Loghy-PHP
# Loghy-PHP
33 changes: 33 additions & 0 deletions composer.json
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"
]
}
}
18 changes: 18 additions & 0 deletions phpunit.xml
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>
66 changes: 66 additions & 0 deletions src/Contract/LoghyInterface.php
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;
}
153 changes: 153 additions & 0 deletions src/Loghy.php
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();
}
}
20 changes: 20 additions & 0 deletions tests/Datasets/LoghyResponses.php
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,
];
});
7 changes: 7 additions & 0 deletions tests/Pest.php
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']);
49 changes: 49 additions & 0 deletions tests/Unit/LoghyTest.php
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]);
}

0 comments on commit cce3696

Please sign in to comment.