Skip to content

[9.4] partial octane support #573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.14",
"symplify/easy-coding-standard": "^11.1",
"vimeo/psalm": "^4.26"
"vimeo/psalm": "^4.27"
},
"suggest": {
"bavix/laravel-wallet-swap": "Addition to the laravel-wallet library for quick setting of exchange rates",
Expand Down
20 changes: 20 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ parameters:
count: 1
path: src/Internal/Repository/WalletRepository.php

-
message: "#^Method Bavix\\\\Wallet\\\\Internal\\\\Service\\\\StateService\\:\\:get\\(\\) should return string\\|null but returns mixed\\.$#"
count: 2
path: src/Internal/Service/StateService.php

-
message: "#^Parameter \\#2 \\$callback of method Illuminate\\\\Contracts\\\\Cache\\\\Repository\\:\\:rememberForever\\(\\) expects Closure, mixed given\\.$#"
count: 1
path: src/Internal/Service/StateService.php

-
message: "#^Parameter \\#1 \\$number of method Bavix\\\\Wallet\\\\Internal\\\\Service\\\\MathServiceInterface\\:\\:round\\(\\) expects float\\|int\\|string, mixed given\\.$#"
count: 1
Expand Down Expand Up @@ -74,3 +84,13 @@ parameters:
message: "#^Parameter \\#2 \\$holderId of method Bavix\\\\Wallet\\\\Internal\\\\Repository\\\\WalletRepositoryInterface\\:\\:getBySlug\\(\\) expects int\\|string, mixed given\\.$#"
count: 1
path: src/Services/WalletService.php

-
message: "#^Cannot call method needs\\(\\) on mixed\\.$#"
count: 3
path: src/WalletServiceProvider.php

-
message: "#^Parameter \\#1 \\$abstract of method Illuminate\\\\Contracts\\\\Container\\\\Container\\:\\:make\\(\\) expects class\\-string\\<wallet\\.internal\\.storage\\>, string given\\.$#"
count: 2
path: src/WalletServiceProvider.php
29 changes: 15 additions & 14 deletions src/Internal/Service/DatabaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,42 @@ public function transaction(callable $callback): mixed
);
}

if ($level > 0) {
return $callback();
}

$this->init = true;

try {
if ($level > 0) {
return $callback();
}

$this->regulatorService->purge();

return $this->connection->transaction(function () use ($callback) {
$result = $callback();
$this->init = false;

if ($result === false || (is_countable($result) && count($result) === 0)) {
$this->regulatorService->purge();
} else {
$this->regulatorService->approve();
if ($result === false) {
return false;
}

if (is_countable($result) && count($result) === 0) {
return $result;
}

$this->regulatorService->approve();

return $result;
});
} catch (RecordsNotFoundException|ExceptionInterface $exception) {
$this->regulatorService->purge();
$this->init = false;

throw $exception;
} catch (Throwable $throwable) {
$this->regulatorService->purge();
$this->init = false;

throw new TransactionFailedException(
'Transaction failed. Message: ' . $throwable->getMessage(),
ExceptionInterface::TRANSACTION_FAILED,
$throwable
);
} finally {
$this->regulatorService->purge();
$this->init = false;
}
}
}
45 changes: 25 additions & 20 deletions src/Internal/Service/LockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@

final class LockService implements LockServiceInterface
{
private const PREFIX = 'wallet_lock::';
private const LOCK_KEY = 'wallet_lock::';

/**
* @var array<string, bool>
*/
private array $lockedKeys = [];
private const INNER_KEYS = 'inner_keys::';

private ?LockProvider $lockProvider = null;

private CacheRepository $lockedKeys;

private CacheRepository $cache;

private int $seconds;

public function __construct(CacheFactory $cacheFactory)
{
$this->seconds = (int) config('wallet.lock.seconds', 1);
$this->cache = $cacheFactory->store(config('wallet.lock.driver', 'array'));
$this->seconds = (int) config('wallet.lock.seconds', 1);
$this->lockedKeys = $cacheFactory->store('array');
}

/**
Expand All @@ -38,21 +40,20 @@ public function block(string $key, callable $callback): mixed
return $callback();
}

$this->lockedKeys[$key] = true;
$lock = $this->getLockProvider()
->lock(self::LOCK_KEY . $key, $this->seconds);
$this->lockedKeys->put(self::INNER_KEYS . $key, true, $this->seconds);

try {
return $this->getLockProvider()
->lock(self::PREFIX . $key)
->block($this->seconds, $callback)
;
return $lock->block($this->seconds, $callback);
} finally {
unset($this->lockedKeys[$key]);
$this->lockedKeys->delete(self::INNER_KEYS . $key);
}
}

public function isBlocked(string $key): bool
{
return $this->lockedKeys[$key] ?? false;
return $this->lockedKeys->get(self::INNER_KEYS . $key) === true;
}

/**
Expand All @@ -61,14 +62,18 @@ public function isBlocked(string $key): bool
*/
private function getLockProvider(): LockProvider
{
$store = $this->cache->getStore();
if ($store instanceof LockProvider) {
return $store;
if ($this->lockProvider === null) {
$store = $this->cache->getStore();
if (! ($store instanceof LockProvider)) {
throw new LockProviderNotFoundException(
'Lockable cache not found',
ExceptionInterface::LOCK_PROVIDER_NOT_FOUND
);
}

$this->lockProvider = $store;
}

throw new LockProviderNotFoundException(
'Lockable cache not found',
ExceptionInterface::LOCK_PROVIDER_NOT_FOUND
);
return $this->lockProvider;
}
}
40 changes: 24 additions & 16 deletions src/Internal/Service/StateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,45 @@

namespace Bavix\Wallet\Internal\Service;

use Illuminate\Contracts\Cache\Factory as CacheFactory;
use Illuminate\Contracts\Cache\Repository as CacheRepository;

final class StateService implements StateServiceInterface
{
/**
* @var array<string, string>
*/
private array $forks = [];
private const PREFIX_FORKS = 'wallet_forks::';

private const PREFIX_FORK_CALL = 'wallet_fork_call::';

private CacheRepository $forks;

private CacheRepository $forkCallables;

/**
* @var array<string, callable>
*/
private array $forkCallables = [];
public function __construct(CacheFactory $cacheFactory)
{
$this->forks = $cacheFactory->store('array');
$this->forkCallables = $cacheFactory->store('array');
}

public function fork(string $uuid, callable $value): void
{
$this->forkCallables[$uuid] ??= $value;
if (! $this->forks->has(self::PREFIX_FORKS . $uuid)) {
$this->forkCallables->put(self::PREFIX_FORK_CALL . $uuid, $value);
}
}

public function get(string $uuid): ?string
{
if ($this->forkCallables[$uuid] ?? null) {
$callable = $this->forkCallables[$uuid];
unset($this->forkCallables[$uuid]);

$this->forks[$uuid] = $callable();
$callable = $this->forkCallables->pull(self::PREFIX_FORK_CALL . $uuid);
if ($callable !== null) {
return $this->forks->rememberForever(self::PREFIX_FORKS . $uuid, $callable);
}

return $this->forks[$uuid] ?? null;
return $this->forks->get(self::PREFIX_FORKS . $uuid);
}

public function drop(string $uuid): void
{
unset($this->forks[$uuid], $this->forkCallables[$uuid]);
$this->forkCallables->forget(self::PREFIX_FORK_CALL . $uuid);
$this->forks->forget(self::PREFIX_FORKS . $uuid);
}
}
10 changes: 6 additions & 4 deletions src/Internal/Service/StorageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

final class StorageService implements StorageServiceInterface
{
private const PREFIX = 'wallet_sg::';

public function __construct(
private MathServiceInterface $mathService,
private CacheRepository $cacheRepository
Expand All @@ -23,15 +25,15 @@ public function flush(): bool

public function missing(string $uuid): bool
{
return $this->cacheRepository->forget($uuid);
return $this->cacheRepository->forget(self::PREFIX . $uuid);
}

/**
* @throws RecordNotFoundException
*/
public function get(string $uuid): string
{
$value = $this->cacheRepository->get($uuid);
$value = $this->cacheRepository->get(self::PREFIX . $uuid);
if ($value === null) {
throw new RecordNotFoundException(
'The repository did not find the object',
Expand All @@ -44,7 +46,7 @@ public function get(string $uuid): string

public function sync(string $uuid, float|int|string $value): bool
{
return $this->cacheRepository->set($uuid, $value);
return $this->cacheRepository->forever(self::PREFIX . $uuid, $this->mathService->round($value));
}

/**
Expand All @@ -53,7 +55,7 @@ public function sync(string $uuid, float|int|string $value): bool
public function increase(string $uuid, float|int|string $value): string
{
$result = $this->mathService->add($this->get($uuid), $value);
$this->sync($uuid, $result);
$this->sync($uuid, $this->mathService->round($result));

return $this->mathService->round($result);
}
Expand Down
44 changes: 23 additions & 21 deletions src/Services/RegulatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,31 @@ public function decrease(Wallet $wallet, float|int|string $value): string

public function approve(): void
{
foreach ($this->wallets as $wallet) {
$diffValue = $this->diff($wallet);
if ($this->mathService->compare($diffValue, 0) === 0) {
continue;
}

$balance = $this->bookkeeperService->increase($wallet, $diffValue);
$wallet->newQuery()
->whereKey($wallet->getKey())
->update([
try {
foreach ($this->wallets as $wallet) {
$diffValue = $this->diff($wallet);
if ($this->mathService->compare($diffValue, 0) === 0) {
continue;
}

$balance = $this->bookkeeperService->increase($wallet, $diffValue);
$wallet->newQuery()
->whereKey($wallet->getKey())
->update([
'balance' => $balance,
]) // ?qN
;
$wallet->fill([
'balance' => $balance,
]) // ?qN
;
$wallet->fill([
'balance' => $balance,
])->syncOriginalAttribute('balance');

$event = $this->balanceUpdatedEventAssembler->create($wallet);
$this->dispatcherService->dispatch($event);
}
])->syncOriginalAttribute('balance');

$this->dispatcherService->flush();
$this->purge();
$event = $this->balanceUpdatedEventAssembler->create($wallet);
$this->dispatcherService->dispatch($event);
}
$this->dispatcherService->flush();
} finally {
$this->purge();
}
}

public function purge(): void
Expand Down
Loading