Skip to content

Commit 2260214

Browse files
committed
Subscribe to laravel events
1 parent c9401d9 commit 2260214

23 files changed

+322
-109
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"brick/math": "~0.8",
2929
"doctrine/dbal": "^2.8|^3.0",
3030
"illuminate/contracts": "^9.0",
31-
"illuminate/database": "^9.0",
31+
"illuminate/database": "^9.37",
3232
"ramsey/uuid": "^4.0"
3333
},
3434
"require-dev": {

config/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Bavix\Wallet\Internal\Repository\TransferRepository;
2020
use Bavix\Wallet\Internal\Repository\WalletRepository;
2121
use Bavix\Wallet\Internal\Service\ClockService;
22+
use Bavix\Wallet\Internal\Service\ConnectionService;
2223
use Bavix\Wallet\Internal\Service\DatabaseService;
2324
use Bavix\Wallet\Internal\Service\DispatcherService;
2425
use Bavix\Wallet\Internal\Service\JsonService;
@@ -82,6 +83,7 @@
8283
*/
8384
'internal' => [
8485
'clock' => ClockService::class,
86+
'connection' => ConnectionService::class,
8587
'database' => DatabaseService::class,
8688
'dispatcher' => DispatcherService::class,
8789
'json' => JsonService::class,

ecs.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
use Symplify\CodingStandard\Fixer\LineLength\DocBlockLineLengthFixer;
1010
use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer;
1111
use Symplify\EasyCodingStandard\Config\ECSConfig;
12-
use Symplify\EasyCodingStandard\ValueObject\Option;
1312
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;
1413

1514
return static function (ECSConfig $containerConfigurator): void {
15+
$containerConfigurator->parallel();
1616
$services = $containerConfigurator->services();
1717
$services->set(ArraySyntaxFixer::class)
1818
->call('configure', [[
@@ -22,9 +22,6 @@
2222
$services->set(DeclareStrictTypesFixer::class);
2323
$services->set(LineLengthFixer::class);
2424

25-
$parameters = $containerConfigurator->parameters();
26-
$parameters->set(Option::PARALLEL, true);
27-
2825
$containerConfigurator->paths([
2926
__DIR__ . '/config',
3027
__DIR__ . '/database',

rector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
use Rector\CodeQuality\Rector\PropertyFetch\ExplicitMethodCallOverMagicGetSetRector;
66
use Rector\Config\RectorConfig;
7+
use Rector\Laravel\Rector\Assign\CallOnAppArrayAccessToStandaloneAssignRector;
8+
use Rector\Laravel\Rector\ClassMethod\AddParentRegisterToEventServiceProviderRector;
79
use Rector\Laravel\Set\LaravelSetList;
810
use Rector\Php74\Rector\Property\TypedPropertyRector;
911
use Rector\PHPUnit\Set\PHPUnitSetList;
1012
use Rector\Set\ValueObject\SetList;
1113

1214
return static function (RectorConfig $containerConfigurator): void {
15+
$containerConfigurator->parallel();
1316
$containerConfigurator->paths([
1417
__DIR__ . '/src',
1518
__DIR__ . '/tests',
@@ -29,4 +32,6 @@
2932

3033
// register a single rule
3134
$services->set(TypedPropertyRector::class);
35+
$services->set(CallOnAppArrayAccessToStandaloneAssignRector::class);
36+
$services->set(AddParentRegisterToEventServiceProviderRector::class);
3237
};

src/Internal/Exceptions/ExceptionInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,4 @@ interface ExceptionInterface extends Throwable
3131
public const TRANSACTION_FAILED = 1 << 10;
3232

3333
public const MODEL_NOT_FOUND = 1 << 11;
34-
35-
public const TRANSACTION_START = 1 << 12;
3634
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Exceptions;
6+
7+
use InvalidArgumentException;
8+
9+
final class TransactionRollbackException extends InvalidArgumentException implements ExceptionInterface
10+
{
11+
public function __construct(
12+
private mixed $result
13+
) {
14+
parent::__construct();
15+
}
16+
17+
public function getResult(): mixed
18+
{
19+
return $this->result;
20+
}
21+
}

src/Internal/Exceptions/TransactionStartException.php

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Listeners;
6+
7+
use Bavix\Wallet\Internal\Service\ConnectionServiceInterface;
8+
use Bavix\Wallet\Services\RegulatorServiceInterface;
9+
10+
final class TransactionBeginningListener
11+
{
12+
public function __construct(
13+
private ConnectionServiceInterface $connectionService,
14+
private RegulatorServiceInterface $regulatorService
15+
) {
16+
}
17+
18+
public function __invoke(): void
19+
{
20+
if ($this->connectionService->get()->transactionLevel() === 1) {
21+
$this->regulatorService->purge();
22+
}
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Listeners;
6+
7+
use Bavix\Wallet\Internal\Service\ConnectionServiceInterface;
8+
use Bavix\Wallet\Services\RegulatorServiceInterface;
9+
10+
final class TransactionCommittedListener
11+
{
12+
public function __construct(
13+
private ConnectionServiceInterface $connectionService,
14+
private RegulatorServiceInterface $regulatorService
15+
) {
16+
}
17+
18+
public function __invoke(): void
19+
{
20+
if ($this->connectionService->get()->transactionLevel() === 0) {
21+
$this->regulatorService->committed();
22+
}
23+
}
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Listeners;
6+
7+
use Bavix\Wallet\Internal\Service\ConnectionServiceInterface;
8+
use Bavix\Wallet\Services\RegulatorServiceInterface;
9+
10+
final class TransactionCommittingListener
11+
{
12+
public function __construct(
13+
private ConnectionServiceInterface $connectionService,
14+
private RegulatorServiceInterface $regulatorService
15+
) {
16+
}
17+
18+
public function __invoke(): void
19+
{
20+
/**
21+
* In fact, this if is not needed here.
22+
* But in order to protect the code from changes in the framework, I added a check here.
23+
*/
24+
if ($this->connectionService->get()->transactionLevel() === 1) {
25+
$this->regulatorService->committing();
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)