Skip to content
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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ test-run-functional: debug-print-env

# Запустить один функциональный тест с дебагером
run-one-functional-test: debug-print-env
docker-compose run --rm php-cli php -dxdebug.start_with_request=yes vendor/bin/phpunit --filter 'testRenewAuthTokenWithoutBitrix24UserId' tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php
docker-compose run --rm php-cli php -dxdebug.start_with_request=yes vendor/bin/phpunit --filter 'testUninstallWithHappyPath' tests/Functional/Bitrix24Accounts/UseCase/Uninstall/HandlerTest.php

schema-drop:
docker-compose run --rm php-cli php bin/doctrine orm:schema-tool:drop --force
Expand Down
6 changes: 3 additions & 3 deletions src/Bitrix24Accounts/Entity/Bitrix24Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(
private CarbonImmutable $updatedAt,
private int $applicationVersion,
private Scope $applicationScope,
bool $isEmitBitrix24AccountCreatedEvent = false
bool $isEmitBitrix24AccountCreatedEvent = false,
) {
$this->addAccountCreatedEventIfNeeded($isEmitBitrix24AccountCreatedEvent);
}
Expand Down Expand Up @@ -213,7 +213,7 @@ public function applicationUninstalled(string $applicationToken): void
);
}

if ($this->applicationToken !== $applicationToken) {
/*if ($this->applicationToken !== $applicationToken) {
throw new InvalidArgumentException(
sprintf(
'application token «%s» mismatch with application token «%s» for bitrix24 account %s for domain %s',
Expand All @@ -223,7 +223,7 @@ public function applicationUninstalled(string $applicationToken): void
$this->domainUrl
)
);
}
}*/

$this->status = Bitrix24AccountStatus::deleted;
$this->updatedAt = new CarbonImmutable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
public function getById(Uuid $uuid): Bitrix24AccountInterface
{
$account = $this->getEntityManager()->getRepository(Bitrix24Account::class)->find($uuid);
if (null === $account || Bitrix24AccountStatus::deleted === $account->getStatus()) {
if (null === $account) {
throw new Bitrix24AccountNotFoundException(
sprintf('bitrix24 account not found by id %s', $uuid->toRfc4122())
);
Expand All @@ -41,6 +41,16 @@ public function getById(Uuid $uuid): Bitrix24AccountInterface
return $account;
}

public function existsById(Uuid $uuid): bool
{
if ($this->getEntityManager()->getRepository(Bitrix24Account::class)->find($uuid))
{
return true;
}

return false;
}

#[\Override]
public function save(Bitrix24AccountInterface $bitrix24Account): void
{
Expand Down
15 changes: 14 additions & 1 deletion src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\ChangeDomainUrl;

use InvalidArgumentException;
readonly class Command
{
public function __construct(
Expand All @@ -15,5 +16,17 @@ public function __construct(
* @var non-empty-string $newDomainUrlHost
*/
public string $newDomainUrlHost
) {}
)
{

$this->validateDomain($oldDomainUrlHost, 'oldDomainUrlHost');
$this->validateDomain($newDomainUrlHost, 'newDomainUrlHost');
}

private function validateDomain(string $domain, string $parameterName): void
{
if (empty($domain) || !filter_var($domain, FILTER_VALIDATE_URL)) {
throw new InvalidArgumentException(sprintf('Invalid value for %s: %s', $parameterName, $domain));
}
}
}
3 changes: 3 additions & 0 deletions src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\ChangeDomainUrl;

use Bitrix24\Lib\Services\Flusher;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface;
use Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

Expand All @@ -24,6 +26,7 @@ public function handle(Command $command): void
'b24_domain_url_new' => $command->newDomainUrlHost,
]);

/** @var Bitrix24AccountInterface[]|AggregateRootEventsEmitterInterface[] $accounts */
$accounts = $this->bitrix24AccountRepository->findByDomain($command->oldDomainUrlHost);
foreach ($accounts as $account) {
$account->changeDomainUrl($command->newDomainUrlHost);
Expand Down
11 changes: 7 additions & 4 deletions src/Bitrix24Accounts/UseCase/InstallFinish/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
declare(strict_types=1);

namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\InstallFinish;
use InvalidArgumentException;

use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException;
readonly class Command
{
public function __construct(
Expand All @@ -25,9 +25,12 @@ private function validate(): void
if (empty($this->memberId)) {
throw new InvalidArgumentException('Member ID cannot be empty.');
}

if (empty($this->domainUrl)) {
throw new InvalidArgumentException('Domain URL cannot be empty.');
/*$pattern = '/^(https?:\/\/)?([a-z0-9-]+\.[a-z]{2,})(\/[^\s]*)?$/i';
if (!preg_match($pattern, $this->domainUrl)) {
throw new InvalidArgumentException('Domain URL is not valid.');
}*/
if (!filter_var($this->domainUrl, FILTER_VALIDATE_URL)) {
throw new InvalidArgumentException('Domain URL is not valid.');
}
}
}
7 changes: 4 additions & 3 deletions src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public function handle(Command $command): void
'b24_user_id' => $command->bitrix24UserId,
]);

$bitrix24Account = $this->getSingleAccountByMemberId($command->domainUrl, $command->memberId, Bitrix24AccountStatus::new, $command->bitrix24UserId);
/** @var Bitrix24AccountInterface|AggregateRootEventsEmitterInterface $bitrix24Account */
$bitrix24Account = $this->getSingleAccountByMemberId($command->domainUrl, $command->memberId, $command->bitrix24UserId);

$bitrix24Account->applicationInstalled($command->applicationToken);

Expand All @@ -54,11 +55,11 @@ public function handle(Command $command): void
]);
}

public function getSingleAccountByMemberId(string $domainUrl, string $memberId, Bitrix24AccountStatus $bitrix24AccountStatus, ?int $bitrix24UserId): Bitrix24AccountInterface
private function getSingleAccountByMemberId(string $domainUrl, string $memberId, ?int $bitrix24UserId): Bitrix24AccountInterface
{
$accounts = $this->bitrix24AccountRepository->findByMemberId(
$memberId,
$bitrix24AccountStatus,
Bitrix24AccountStatus::new,
$bitrix24UserId
);

Expand Down
47 changes: 39 additions & 8 deletions src/Bitrix24Accounts/UseCase/InstallStart/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,48 @@
use Bitrix24\SDK\Core\Credentials\AuthToken;
use Bitrix24\SDK\Core\Credentials\Scope;
use Symfony\Component\Uid\Uuid;
use InvalidArgumentException;

readonly class Command
{
public function __construct(
public Uuid $uuid,
public int $bitrix24UserId,
public bool $isBitrix24UserAdmin,
public string $memberId,
public string $domainUrl,
public Uuid $uuid,
public int $bitrix24UserId,
public bool $isBitrix24UserAdmin,
public string $memberId,
public string $domainUrl,
public AuthToken $authToken,
public int $applicationVersion,
public Scope $applicationScope
) {}
public int $applicationVersion,
public Scope $applicationScope
)
{
$this->validate();
}

private function validate(): void
{
if (empty($this->uuid) || !Uuid::isValid($this->uuid->toString())) {
throw new InvalidArgumentException('Empty uuid or invalid UUID provided.');
}

if ($this->bitrix24UserId <= 0) {
throw new InvalidArgumentException('Bitrix24 User ID must be a positive integer.');
}

if (!is_string($this->memberId) || empty($this->memberId)) {
throw new InvalidArgumentException('Member ID must be a non-empty string.');
}

if (!filter_var($this->domainUrl, FILTER_VALIDATE_URL)) {
throw new InvalidArgumentException('Domain URL is not valid.');
}

if ($this->applicationVersion <= 0) {
throw new InvalidArgumentException('Application version must be a positive integer.');
}

if (!is_string($this->authToken->accessToken)) {
throw new InvalidArgumentException('accessToken must be a string.');
}
}
}
19 changes: 11 additions & 8 deletions src/Bitrix24Accounts/UseCase/InstallStart/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
{
public function __construct(
private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository,
private Flusher $flusher,
private LoggerInterface $logger
) {}
private Flusher $flusher,
private LoggerInterface $logger
)
{
}

public function handle(Command $command): void
{
Expand Down Expand Up @@ -46,10 +48,11 @@ public function handle(Command $command): void
$this->flusher->flush($bitrix24Account);

$this->logger->info('Bitrix24Accounts.InstallStart.Finish',
[
'id' => $command->uuid->toRfc4122(),
'domain_url' => $command->domainUrl,
'member_id' => $command->memberId,
]);
[
'id' => $command->uuid->toRfc4122(),
'domain_url' => $command->domainUrl,
'member_id' => $command->memberId,
]);

}
}
30 changes: 14 additions & 16 deletions src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
{
public function __construct(
private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository,
private Flusher $flusher,
private LoggerInterface $logger
) {}
private Flusher $flusher,
private LoggerInterface $logger
)
{
}

/**
* @throws MultipleBitrix24AccountsFoundException
Expand All @@ -31,10 +33,10 @@ public function handle(Command $command): void
'bitrix24_user_id' => $command->bitrix24UserId,
]);

/** @var Bitrix24AccountInterface|AggregateRootEventsEmitterInterface $bitrix24Account */
$bitrix24Account = $this->getSingleAccountByMemberId(
$command->renewedAuthToken->domain,
$command->renewedAuthToken->memberId,
Bitrix24AccountStatus::active,
$command->bitrix24UserId
);

Expand All @@ -46,25 +48,21 @@ public function handle(Command $command): void
$this->flusher->flush($bitrix24Account);

$this->logger->info('Bitrix24Accounts.RenewAuthToken.finish',
[
'domain_url' => $command->renewedAuthToken->domain,
'member_id' => $command->renewedAuthToken->memberId,
'bitrix24_user_id' => $command->bitrix24UserId,
]);
[
'domain_url' => $command->renewedAuthToken->domain,
'member_id' => $command->renewedAuthToken->memberId,
'bitrix24_user_id' => $command->bitrix24UserId,
]);
}

/**
* @throws MultipleBitrix24AccountsFoundException
*/
public function getSingleAccountByMemberId(
string $domainUrl,
string $memberId,
Bitrix24AccountStatus $bitrix24AccountStatus,
?int $bitrix24UserId
): Bitrix24AccountInterface {
private function getSingleAccountByMemberId(string $domainUrl, string $memberId, ?int $bitrix24UserId): Bitrix24AccountInterface
{
$accounts = $this->bitrix24AccountRepository->findByMemberId(
$memberId,
$bitrix24AccountStatus,
Bitrix24AccountStatus::active,
$bitrix24UserId
);

Expand Down
1 change: 1 addition & 0 deletions src/Bitrix24Accounts/UseCase/Uninstall/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function handle(Command $command): void
'b24_application_token' => $command->applicationToken,
]);

/** @var Bitrix24AccountInterface[]|AggregateRootEventsEmitterInterface[] $accounts */
$accounts = $this->bitrix24AccountRepository->findByApplicationToken($command->applicationToken);
$accountsCount = count($accounts);
foreach ($accounts as $account) {
Expand Down
Loading
Loading