Skip to content

Commit

Permalink
Refactor Handler.php files for code clarity and maintainability
Browse files Browse the repository at this point in the history
Adjusted import statements, formatted constructor and exception handling, and added PHPDoc comments for improved readability and maintenance. These changes enhance code consistency and readability across the `Uninstall`, `InstallStart`, and `InstallFinish` handlers.

Signed-off-by: mesilov <[email protected]>
  • Loading branch information
mesilov committed Nov 16, 2024
1 parent 469e563 commit 57153ae
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
31 changes: 20 additions & 11 deletions src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Exceptions\MultipleBitrix24AccountsFoundException;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface;
use Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface;
use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

Expand All @@ -25,6 +26,8 @@ public function __construct(

/**
* @throws Bitrix24AccountNotFoundException
* @throws MultipleBitrix24AccountsFoundException
* @throws InvalidArgumentException
*/
public function handle(Command $command): void
{
Expand All @@ -41,23 +44,29 @@ public function handle(Command $command): void
$command->bitrix24UserId
);
if ([] === $accounts) {
throw new Bitrix24AccountNotFoundException(sprintf(
'bitrix24 account for domain %s with member id %s in status «new» not found',
$command->domainUrl,
$command->memberId
));
throw new Bitrix24AccountNotFoundException(
sprintf(
'bitrix24 account for domain %s with member id %s in status «new» not found',
$command->domainUrl,
$command->memberId
)
);
}

if (count($accounts) > 1) {
throw new MultipleBitrix24AccountsFoundException(sprintf(
'multiple bitrix24 accounts for domain %s with member id %s in status «new» found',
$command->domainUrl,
$command->memberId
));
throw new MultipleBitrix24AccountsFoundException(
sprintf(
'multiple bitrix24 accounts for domain %s with member id %s in status «new» found',
$command->domainUrl,
$command->memberId
)
);
}

/**
* @var AggregateRootEventsEmitterInterface|Bitrix24AccountInterface $targetAccount
*/
$targetAccount = $accounts[0];
// @var Bitrix24AccountInterface|AggregateRootEventsEmitterInterface $targetAccount
$targetAccount->applicationInstalled($command->applicationToken);

$this->bitrix24AccountRepository->save($targetAccount);
Expand Down
10 changes: 4 additions & 6 deletions src/Bitrix24Accounts/UseCase/InstallStart/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\InstallStart;

use Bitrix24\Lib\Bitrix24Accounts\Entity\Bitrix24Account;
use Bitrix24\Lib\Services\Flusher;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Events\Bitrix24AccountCreatedEvent;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface;
use Bitrix24\Lib\Bitrix24Accounts\Entity\Bitrix24Account;
use Carbon\CarbonImmutable;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
Expand All @@ -20,15 +19,14 @@ public function __construct(
private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository,
private Flusher $flusher,
private LoggerInterface $logger
) {
}
) {}

public function handle(Command $command): void
{
$this->logger->debug('Bitrix24Accounts.InstallStart.start', [
'id' => $command->uuid->toRfc4122(),
'domain_url' => $command->domainUrl,
'member_id' => $command->memberId
'member_id' => $command->memberId,
]);

$newAccount = new Bitrix24Account(
Expand All @@ -53,4 +51,4 @@ public function handle(Command $command): void

$this->logger->debug('Bitrix24Accounts.InstallStart.Finish');
}
}
}
18 changes: 9 additions & 9 deletions src/Bitrix24Accounts/UseCase/Uninstall/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,21 @@

use Bitrix24\Lib\Services\Flusher;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Exceptions\Bitrix24AccountNotFoundException;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface;
use Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface;
use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException;
use Bitrix24\Lib\Bitrix24Accounts\UseCase\Uninstall\Command;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

readonly class Handler
{
public function __construct(
private EventDispatcherInterface $eventDispatcher,
private EventDispatcherInterface $eventDispatcher,
private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository,
private Flusher $flusher,
private LoggerInterface $logger
)
{
}
private Flusher $flusher,
private LoggerInterface $logger
) {}

/**
* @throws Bitrix24AccountNotFoundException
Expand All @@ -35,6 +31,10 @@ public function handle(Command $command): void
$this->logger->debug('Bitrix24Accounts.Uninstall.start', [
'b24_application_token' => $command->applicationToken,
]);

/**
* @var AggregateRootEventsEmitterInterface[]|Bitrix24AccountInterface[] $accounts
*/
$accounts = $this->bitrix24AccountRepository->findByApplicationToken($command->applicationToken);

foreach ($accounts as $account) {
Expand All @@ -48,4 +48,4 @@ public function handle(Command $command): void

$this->logger->debug('Bitrix24Accounts.Uninstall.Finish');
}
}
}

0 comments on commit 57153ae

Please sign in to comment.