Skip to content

Commit

Permalink
Subscribe to laravel events
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Oct 26, 2022
1 parent c9401d9 commit 3584c5a
Show file tree
Hide file tree
Showing 23 changed files with 322 additions and 109 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"brick/math": "~0.8",
"doctrine/dbal": "^2.8|^3.0",
"illuminate/contracts": "^9.0",
"illuminate/database": "^9.0",
"illuminate/database": "^9.37",
"ramsey/uuid": "^4.0"
},
"require-dev": {
Expand Down
2 changes: 2 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Bavix\Wallet\Internal\Repository\TransferRepository;
use Bavix\Wallet\Internal\Repository\WalletRepository;
use Bavix\Wallet\Internal\Service\ClockService;
use Bavix\Wallet\Internal\Service\ConnectionService;
use Bavix\Wallet\Internal\Service\DatabaseService;
use Bavix\Wallet\Internal\Service\DispatcherService;
use Bavix\Wallet\Internal\Service\JsonService;
Expand Down Expand Up @@ -82,6 +83,7 @@
*/
'internal' => [
'clock' => ClockService::class,
'connection' => ConnectionService::class,
'database' => DatabaseService::class,
'dispatcher' => DispatcherService::class,
'json' => JsonService::class,
Expand Down
5 changes: 1 addition & 4 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
use Symplify\CodingStandard\Fixer\LineLength\DocBlockLineLengthFixer;
use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Option;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

return static function (ECSConfig $containerConfigurator): void {
$containerConfigurator->parallel();
$services = $containerConfigurator->services();
$services->set(ArraySyntaxFixer::class)
->call('configure', [[
Expand All @@ -22,9 +22,6 @@
$services->set(DeclareStrictTypesFixer::class);
$services->set(LineLengthFixer::class);

$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PARALLEL, true);

$containerConfigurator->paths([
__DIR__ . '/config',
__DIR__ . '/database',
Expand Down
5 changes: 5 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

use Rector\CodeQuality\Rector\PropertyFetch\ExplicitMethodCallOverMagicGetSetRector;
use Rector\Config\RectorConfig;
use Rector\Laravel\Rector\Assign\CallOnAppArrayAccessToStandaloneAssignRector;
use Rector\Laravel\Rector\ClassMethod\AddParentRegisterToEventServiceProviderRector;
use Rector\Laravel\Set\LaravelSetList;
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $containerConfigurator): void {
$containerConfigurator->parallel();
$containerConfigurator->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
Expand All @@ -29,4 +32,6 @@

// register a single rule
$services->set(TypedPropertyRector::class);
$services->set(CallOnAppArrayAccessToStandaloneAssignRector::class);
$services->set(AddParentRegisterToEventServiceProviderRector::class);
};
2 changes: 0 additions & 2 deletions src/Internal/Exceptions/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,4 @@ interface ExceptionInterface extends Throwable
public const TRANSACTION_FAILED = 1 << 10;

public const MODEL_NOT_FOUND = 1 << 11;

public const TRANSACTION_START = 1 << 12;
}
21 changes: 21 additions & 0 deletions src/Internal/Exceptions/TransactionRollbackException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Internal\Exceptions;

use InvalidArgumentException;

final class TransactionRollbackException extends InvalidArgumentException implements ExceptionInterface
{
public function __construct(
private mixed $result
) {
parent::__construct();
}

public function getResult(): mixed
{
return $this->result;
}
}
11 changes: 0 additions & 11 deletions src/Internal/Exceptions/TransactionStartException.php

This file was deleted.

24 changes: 24 additions & 0 deletions src/Internal/Listeners/TransactionBeginningListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Internal\Listeners;

use Bavix\Wallet\Internal\Service\ConnectionServiceInterface;
use Bavix\Wallet\Services\RegulatorServiceInterface;

final class TransactionBeginningListener
{
public function __construct(
private ConnectionServiceInterface $connectionService,
private RegulatorServiceInterface $regulatorService
) {
}

public function __invoke(): void
{
if ($this->connectionService->get()->transactionLevel() === 1) {
$this->regulatorService->purge();
}
}
}
24 changes: 24 additions & 0 deletions src/Internal/Listeners/TransactionCommittedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Internal\Listeners;

use Bavix\Wallet\Internal\Service\ConnectionServiceInterface;
use Bavix\Wallet\Services\RegulatorServiceInterface;

final class TransactionCommittedListener
{
public function __construct(
private ConnectionServiceInterface $connectionService,
private RegulatorServiceInterface $regulatorService
) {
}

public function __invoke(): void
{
if ($this->connectionService->get()->transactionLevel() === 0) {
$this->regulatorService->committed();
}
}
}
28 changes: 28 additions & 0 deletions src/Internal/Listeners/TransactionCommittingListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Internal\Listeners;

use Bavix\Wallet\Internal\Service\ConnectionServiceInterface;
use Bavix\Wallet\Services\RegulatorServiceInterface;

final class TransactionCommittingListener
{
public function __construct(
private ConnectionServiceInterface $connectionService,
private RegulatorServiceInterface $regulatorService
) {
}

public function __invoke(): void
{
/**
* In fact, this if is not needed here.
* But in order to protect the code from changes in the framework, I added a check here.
*/
if ($this->connectionService->get()->transactionLevel() === 1) {
$this->regulatorService->committing();
}
}
}
24 changes: 24 additions & 0 deletions src/Internal/Listeners/TransactionRolledBackListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Internal\Listeners;

use Bavix\Wallet\Internal\Service\ConnectionServiceInterface;
use Bavix\Wallet\Services\RegulatorServiceInterface;

final class TransactionRolledBackListener
{
public function __construct(
private ConnectionServiceInterface $connectionService,
private RegulatorServiceInterface $regulatorService
) {
}

public function __invoke(): void
{
if ($this->connectionService->get()->transactionLevel() === 0) {
$this->regulatorService->purge();
}
}
}
26 changes: 26 additions & 0 deletions src/Internal/Service/ConnectionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Internal\Service;

use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\ConnectionResolverInterface;

/**
* @internal
*/
final class ConnectionService implements ConnectionServiceInterface
{
private ConnectionInterface $connection;

public function __construct(ConnectionResolverInterface $connectionResolver)
{
$this->connection = $connectionResolver->connection(config('wallet.database.connection'));
}

public function get(): ConnectionInterface
{
return $this->connection;
}
}
12 changes: 12 additions & 0 deletions src/Internal/Service/ConnectionServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Internal\Service;

use Illuminate\Database\ConnectionInterface;

interface ConnectionServiceInterface
{
public function get(): ConnectionInterface;
}
50 changes: 11 additions & 39 deletions src/Internal/Service/DatabaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,15 @@

use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
use Bavix\Wallet\Internal\Exceptions\TransactionStartException;
use Bavix\Wallet\Services\RegulatorServiceInterface;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\ConnectionResolverInterface;
use Bavix\Wallet\Internal\Exceptions\TransactionRollbackException;
use Illuminate\Database\RecordsNotFoundException;
use Throwable;

final class DatabaseService implements DatabaseServiceInterface
{
private ConnectionInterface $connection;

private bool $init = false;

public function __construct(
ConnectionResolverInterface $connectionResolver,
private RegulatorServiceInterface $regulatorService
private ConnectionServiceInterface $connectionService
) {
$this->connection = $connectionResolver->connection(config('wallet.database.connection'));
}

/**
Expand All @@ -33,39 +24,23 @@ public function __construct(
*/
public function transaction(callable $callback): mixed
{
$level = $this->connection->transactionLevel();
if ($level > 0 && ! $this->init) {
throw new TransactionStartException(
'Working inside an embedded transaction is not possible. https://bavix.github.io/laravel-wallet/#/transaction',
ExceptionInterface::TRANSACTION_START,
);
}

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

$this->init = true;

try {
$this->regulatorService->purge();
$connection = $this->connectionService->get();
if ($connection->transactionLevel() > 0) {
return $callback();
}

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

if ($result === false) {
return false;
if ($result === false || (is_countable($result) && count($result) === 0)) {
throw new TransactionRollbackException($result);
}

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

$this->regulatorService->approve();

return $result;
});
} catch (TransactionRollbackException $rollbackException) {
return $rollbackException->getResult();
} catch (RecordsNotFoundException|ExceptionInterface $exception) {
throw $exception;
} catch (Throwable $throwable) {
Expand All @@ -74,9 +49,6 @@ public function transaction(callable $callback): mixed
ExceptionInterface::TRANSACTION_FAILED,
$throwable
);
} finally {
$this->regulatorService->purge();
$this->init = false;
}
}
}
2 changes: 0 additions & 2 deletions src/Internal/Service/DatabaseServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
use Bavix\Wallet\Internal\Exceptions\TransactionStartException;
use Illuminate\Database\RecordsNotFoundException;

interface DatabaseServiceInterface
Expand All @@ -17,7 +16,6 @@ interface DatabaseServiceInterface
* @return T
*
* @throws RecordsNotFoundException
* @throws TransactionStartException
* @throws TransactionFailedException
* @throws ExceptionInterface
*/
Expand Down
Loading

0 comments on commit 3584c5a

Please sign in to comment.