Skip to content

Commit

Permalink
[update] 実装修正 for v3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
chanshige committed Jan 26, 2024
1 parent ecc7d88 commit 97e7b30
Show file tree
Hide file tree
Showing 26 changed files with 225 additions and 214 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.idea/
.phpcs.xml
.phpcs-cache
.phpunit.cache
build/
docker/
vendor/
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ $ composer require chanshige/sesame

Usage
--

```injectablephp
<?php
// initialize
Expand Down
204 changes: 102 additions & 102 deletions composer.lock

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions src/Action/AbstractAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Chanshige\SmartLock\Sesame\Action;

use Chanshige\SmartLock\Sesame\Extend\Signature;
use Chanshige\SmartLock\Sesame\Interface\ActionInterface;
use Chanshige\SmartLock\Sesame\Interface\DeviceInterface;
use Chanshige\SmartLock\Sesame\Interface\NowInterface;
use Koriym\HttpConstants\Method;

abstract class AbstractAction implements ActionInterface
Expand All @@ -23,9 +25,9 @@ public function uuid(): string
return $this->device->uuid();
}

public function sign(callable|null $generate = null): string
public function secretKey(): string
{
return $this->device->sign($generate);
return $this->device->secretKey();
}

abstract public function method(): string;
Expand All @@ -34,4 +36,10 @@ abstract public function path(): string;

/** @return array<string, string|int>|array{} */
abstract public function payload(): array;

/** @SuppressWarnings(PHPMD.StaticAccess) */
protected function sign(NowInterface $now): string
{
return Signature::generate($this->secretKey(), $now);
}
}
13 changes: 0 additions & 13 deletions src/Action/Cmd.php

This file was deleted.

12 changes: 12 additions & 0 deletions src/Action/CmdCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Chanshige\SmartLock\Sesame\Action;

final class CmdCode
{
public const TOGGLE = 88;
public const LOCK = 82;
public const UNLOCK = 83;
}
8 changes: 0 additions & 8 deletions src/Action/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Chanshige\SmartLock\Sesame\Action;

use BadFunctionCallException;
use Chanshige\SmartLock\Sesame\Interface\DeviceInterface;

use function assert;
Expand All @@ -31,13 +30,6 @@ public function path(): string
return '/history';
}

public function sign(callable|null $generate = null): string
{
assert($generate === null);

throw new BadFunctionCallException('This method is not supported.');
}

/** {@inheritdoc} */
public function payload(): array
{
Expand Down
10 changes: 8 additions & 2 deletions src/Action/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@

namespace Chanshige\SmartLock\Sesame\Action;

use Chanshige\SmartLock\Sesame\Extend\Now;
use Chanshige\SmartLock\Sesame\Interface\DeviceInterface;
use Chanshige\SmartLock\Sesame\Interface\NowInterface;

use function assert;
use function base64_encode;

final class Lock extends AbstractAction
{
public function __construct(
private readonly DeviceInterface $device,
private readonly string $comment = 'WebAPI',
private readonly NowInterface $now = new Now(),
) {
assert($this->device instanceof DeviceInterface);

parent::__construct($device);
}

Expand All @@ -31,9 +37,9 @@ public function path(): string
public function payload(): array
{
return [
'cmd' => Cmd::LOCK->value,
'cmd' => CmdCode::LOCK,
'history' => base64_encode($this->comment),
'sign' => $this->device->sign(),
'sign' => $this->sign($this->now),
];
}
}
11 changes: 0 additions & 11 deletions src/Action/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

namespace Chanshige\SmartLock\Sesame\Action;

use BadFunctionCallException;

use function assert;

final class Status extends AbstractAction
{
public function method(): string
Expand All @@ -20,13 +16,6 @@ public function path(): string
return '';
}

public function sign(callable|null $generate = null): string
{
assert($generate === null);

throw new BadFunctionCallException('This method is not supported.');
}

/** @return array{} */
public function payload(): array
{
Expand Down
10 changes: 8 additions & 2 deletions src/Action/Toggle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@

namespace Chanshige\SmartLock\Sesame\Action;

use Chanshige\SmartLock\Sesame\Extend\Now;
use Chanshige\SmartLock\Sesame\Interface\DeviceInterface;
use Chanshige\SmartLock\Sesame\Interface\NowInterface;

use function assert;
use function base64_encode;

final class Toggle extends AbstractAction
{
public function __construct(
private readonly DeviceInterface $device,
private readonly string $comment = 'WebAPI',
private readonly NowInterface $now = new Now(),
) {
assert($this->device instanceof DeviceInterface);

parent::__construct($device);
}

Expand All @@ -31,9 +37,9 @@ public function path(): string
public function payload(): array
{
return [
'cmd' => Cmd::TOGGLE->value,
'cmd' => CmdCode::TOGGLE,
'history' => base64_encode($this->comment),
'sign' => $this->device->sign(),
'sign' => $this->sign($this->now),
];
}
}
10 changes: 8 additions & 2 deletions src/Action/Unlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@

namespace Chanshige\SmartLock\Sesame\Action;

use Chanshige\SmartLock\Sesame\Extend\Now;
use Chanshige\SmartLock\Sesame\Interface\DeviceInterface;
use Chanshige\SmartLock\Sesame\Interface\NowInterface;

use function assert;
use function base64_encode;

final class Unlock extends AbstractAction
{
public function __construct(
private readonly DeviceInterface $device,
private readonly string $comment = 'WebAPI',
private readonly NowInterface $now = new Now(),
) {
assert($this->device instanceof DeviceInterface);

parent::__construct($device);
}

Expand All @@ -31,9 +37,9 @@ public function path(): string
public function payload(): array
{
return [
'cmd' => Cmd::UNLOCK->value,
'cmd' => CmdCode::UNLOCK,
'history' => base64_encode($this->comment),
'sign' => $this->device->sign(),
'sign' => $this->sign($this->now),
];
}
}
28 changes: 1 addition & 27 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
use Chanshige\SmartLock\Sesame\Exception\ClientException;
use Chanshige\SmartLock\Sesame\Exception\SesameException;
use Chanshige\SmartLock\Sesame\Http\GuzzleHttpFactory;
use Chanshige\SmartLock\Sesame\Http\ResponseInterface;
use Chanshige\SmartLock\Sesame\Interface\ActionInterface;
use Chanshige\SmartLock\Sesame\Interface\ClientInterface;
use Chanshige\SmartLock\Sesame\Interface\DeviceInterface;
use Chanshige\SmartLock\Sesame\Interface\HttpInterface;
use Chanshige\SmartLock\Sesame\Interface\ResponseInterface;

use function sprintf;

Expand All @@ -35,31 +34,6 @@ public function __invoke(ActionInterface $action): ResponseInterface
}
}

public function status(DeviceInterface $device): ResponseInterface
{
return $this(new Action\Status($device));
}

public function history(DeviceInterface $device, int $page = 0, int $lg = 50): ResponseInterface
{
return $this(new Action\History($device, $page, $lg));
}

public function lock(DeviceInterface $device, string $note = 'WebAPI'): ResponseInterface
{
return $this(new Action\Lock($device, $note));
}

public function unLock(DeviceInterface $device, string $note = 'WebAPI'): ResponseInterface
{
return $this(new Action\Unlock($device, $note));
}

public function toggle(DeviceInterface $device, string $note = 'WebAPI'): ResponseInterface
{
return $this(new Action\Toggle($device, $note));
}

/**
* @param array<string, mixed> $options
*
Expand Down
17 changes: 3 additions & 14 deletions src/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@

namespace Chanshige\SmartLock\Sesame;

use Chanshige\SmartLock\Sesame\Extend\Now;
use Chanshige\SmartLock\Sesame\Extend\Signature;
use Chanshige\SmartLock\Sesame\Interface\DeviceInterface;
use Chanshige\SmartLock\Sesame\Interface\NowInterface;

use function is_callable;

final readonly class Device implements DeviceInterface
readonly class Device implements DeviceInterface
{
public function __construct(
private string $uuid,
private string $secretKey,
private NowInterface|null $now = new Now(),
) {
}

Expand All @@ -25,13 +19,8 @@ public function uuid(): string
return $this->uuid;
}

/** @SuppressWarnings(PHPMD.StaticAccess) */
public function sign(callable|null $generate = null): string
public function secretKey(): string
{
if (! is_callable($generate)) {
return $this->sign(Signature::generate(...));
}

return $generate($this->secretKey, $this->now);
return $this->secretKey;
}
}
36 changes: 36 additions & 0 deletions src/Enum/Status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Chanshige\SmartLock\Sesame\Enum;

enum Status: string
{
case LOCKED = 'locked';
case UNLOCKED = 'unlocked';

/** @note サムターンが施錠・解錠判定範囲外の場合。半ロックとか? */
case MOVED = 'moved';

public function message(): string
{
return match ($this) {
self::LOCKED => '施錠',
self::UNLOCKED => '解錠',
self::MOVED => 'サムターン位置確認',
};
}

public function equals(Status $status): bool
{
return $this === $status;
}

public function next(): self
{
return match ($this) {
self::LOCKED => self::UNLOCKED,
self::UNLOCKED, self::MOVED => self::LOCKED,
};
}
}
6 changes: 6 additions & 0 deletions src/Extend/AesCmac.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
use Crypto\CMAC;
use Crypto\MACException;
use InvalidArgumentException;
use LogicException;

use function extension_loaded;
use function hex2bin;

if (! extension_loaded('crypto')) {
throw new LogicException('You cannot use the "Chanshige\SmartLock\Sesame\Extend\AesCmac" as the "crypto" extension is not installed.');
}

final class AesCmac
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/Http/GuzzleHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Chanshige\SmartLock\Sesame\Exception\ClientException;
use Chanshige\SmartLock\Sesame\Interface\HttpInterface;
use Chanshige\SmartLock\Sesame\Interface\ResponseFactoryInterface;
use Chanshige\SmartLock\Sesame\Interface\ResponseInterface;
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
Expand Down
Loading

0 comments on commit 97e7b30

Please sign in to comment.