Skip to content

Commit 36bbd8f

Browse files
committed
Add middleware, method UserResetPassword, UserDelete, LockInitialize; change transport & configuration
1 parent 7bc1041 commit 36bbd8f

23 files changed

+580
-52
lines changed

src/Configuration.php

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace SunnyPHP\TTLock;
55

6+
use SunnyPHP\TTLock\Contract\Request\RequiredConfiguration;
67
use Webmozart\Assert\Assert;
78

89
/**
@@ -12,20 +13,27 @@ final class Configuration
1213
{
1314
private const ENDPOINT = 'https://euapi.ttlock.com';
1415
private string $clientId;
15-
private string $clientSecret;
16+
private ?string $clientSecret;
17+
private ?string $accessToken;
1618
private string $endpointHost;
19+
private static array $toArrayFilter = [
20+
'clientId' => RequiredConfiguration::CLIENT_ID,
21+
'clientSecret' => RequiredConfiguration::CLIENT_SECRET,
22+
'accessToken' => RequiredConfiguration::ACCESS_TOKEN,
23+
];
1724

1825
public function __construct(
1926
string $clientId,
20-
string $clientSecret,
27+
?string $clientSecret = null,
28+
?string $accessToken = null,
2129
string $endpointHost = self::ENDPOINT
2230
) {
2331
Assert::notEmpty($clientId, 'ClientId param should be filled');
24-
Assert::notEmpty($clientSecret, 'ClientSecret param should be filled');
2532
Assert::notEmpty($endpointHost, 'EndpointHost param should be filled, use default value: ' . self::ENDPOINT);
2633

2734
$this->clientId = $clientId;
2835
$this->clientSecret = $clientSecret;
36+
$this->accessToken = $accessToken;
2937
$this->endpointHost = $endpointHost;
3038
}
3139

@@ -34,28 +42,53 @@ public function getClientId(): string
3442
return $this->clientId;
3543
}
3644

37-
public function getClientSecret(): string
45+
public function getClientSecret(): ?string
3846
{
3947
return $this->clientSecret;
4048
}
4149

50+
public function getAccessToken(): ?string
51+
{
52+
return $this->accessToken;
53+
}
54+
4255
public function getEndpointHost(): string
4356
{
4457
return $this->endpointHost;
4558
}
4659

4760
public function withClientId(string $clientId): self
4861
{
49-
return new self($clientId, $this->getClientSecret(), $this->getEndpointHost());
62+
return new self($clientId, $this->getClientSecret(), $this->getAccessToken(), $this->getEndpointHost());
63+
}
64+
65+
public function withClientSecret(?string $clientSecret): self
66+
{
67+
return new self($this->getClientId(), $clientSecret, $this->getAccessToken(), $this->getEndpointHost());
5068
}
5169

52-
public function withClientSecret(string $clientSecret): self
70+
public function withAccessToken(?string $accessToken): self
5371
{
54-
return new self($this->getClientId(), $clientSecret, $this->getEndpointHost());
72+
return new self($this->getClientId(), $this->getClientSecret(), $accessToken, $this->getEndpointHost());
5573
}
5674

5775
public function withEndpointHost(string $endpointHost): self
5876
{
59-
return new self($this->getClientId(), $this->getClientSecret(), $endpointHost);
77+
return new self($this->getClientId(), $this->getClientSecret(), $this->getAccessToken(), $endpointHost);
78+
}
79+
80+
public function toArray(?int $bitmask = null): array
81+
{
82+
$params = [];
83+
84+
foreach (self::$toArrayFilter as $key => $filterBit) {
85+
if ($bitmask === null || ($bitmask & $filterBit) === $filterBit) {
86+
Assert::propertyExists($this, $key);
87+
88+
$params[$key] = $this->{$key};
89+
}
90+
}
91+
92+
return $params;
6093
}
6194
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SunnyPHP\TTLock\Contract\Middleware\BeforeResponse;
5+
6+
use SunnyPHP\TTLock\Contract\Middleware\MiddlewareInterface;
7+
8+
interface BeforeResponseInterface extends MiddlewareInterface
9+
{
10+
public function handle(array $response, ?MiddlewareInterface $next = null): array;
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SunnyPHP\TTLock\Contract\Middleware;
5+
6+
interface MiddlewareInterface
7+
{
8+
/**
9+
* @return class-string
10+
*/
11+
public function getType(): string;
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SunnyPHP\TTLock\Contract\Request\Lock;
5+
6+
use DateTimeImmutable;
7+
use SunnyPHP\TTLock\Contract\Request\RequestInterface;
8+
9+
interface InitializeInterface extends RequestInterface
10+
{
11+
public function getLockData(): string;
12+
13+
public function getLockAlias(): ?string;
14+
15+
public function getGroupId(): ?int;
16+
17+
public function getNbInitSuccess(): ?bool;
18+
19+
public function getCurrentTime(): DateTimeImmutable;
20+
}

src/Contract/Request/RequestInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
interface RequestInterface
77
{
88
/**
9-
* Returns True if client credentials required (clientId, clientSecret)
10-
* @return bool
9+
* Returns required configuration bitmask
10+
* @return int
1111
*/
12-
public function isClientCredentialsRequired(): bool;
12+
public function getRequiredConfiguration(): int;
1313

1414
/**
1515
* Endpoint request URL part
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SunnyPHP\TTLock\Contract\Request;
5+
6+
final class RequiredConfiguration
7+
{
8+
public const CLIENT_ID = 1 << 0;
9+
public const CLIENT_SECRET = 1 << 1;
10+
public const ACCESS_TOKEN = 1 << 2;
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SunnyPHP\TTLock\Contract\Request\User;
5+
6+
use DateTimeImmutable;
7+
use SunnyPHP\TTLock\Contract\Request\RequestInterface;
8+
9+
interface DeleteInterface extends RequestInterface
10+
{
11+
/**
12+
* Username for delete (only numbers and english letters)
13+
* @return string
14+
*/
15+
public function getUsername(): string;
16+
17+
/**
18+
* Current time
19+
* @return DateTimeImmutable
20+
*/
21+
public function getCurrentTime(): DateTimeImmutable;
22+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SunnyPHP\TTLock\Contract\Request\User;
5+
6+
use DateTimeImmutable;
7+
use SunnyPHP\TTLock\Contract\Request\RequestInterface;
8+
9+
interface ResetPasswordInterface extends RequestInterface
10+
{
11+
/**
12+
* Username (only numbers and english letters)
13+
* @return string
14+
*/
15+
public function getUsername(): string;
16+
17+
/**
18+
* New password (32 chars, low case, md5 encrypted)
19+
* @return string
20+
*/
21+
public function getPassword(): string;
22+
23+
/**
24+
* Current time
25+
* @return DateTimeImmutable
26+
*/
27+
public function getCurrentTime(): DateTimeImmutable;
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SunnyPHP\TTLock\Contract\Response\Common;
5+
6+
use SunnyPHP\TTLock\Contract\Response\ResponseInterface;
7+
8+
/**
9+
* No data, no error, just success execution
10+
*/
11+
interface SuccessResponseInterface extends ResponseInterface
12+
{
13+
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SunnyPHP\TTLock\Contract\Response\Lock;
5+
6+
use SunnyPHP\TTLock\Contract\Response\ResponseInterface;
7+
8+
interface InitializeInterface extends ResponseInterface
9+
{
10+
public function getLockId(): int;
11+
12+
public function getKeyId(): int;
13+
}

0 commit comments

Comments
 (0)