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
68 changes: 41 additions & 27 deletions src/Bitrix24Accounts/Entity/Bitrix24Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,21 @@ class Bitrix24Account extends AggregateRoot implements Bitrix24AccountInterface
private ?string $comment = null;

public function __construct(
private readonly Uuid $id,
private readonly int $bitrix24UserId,
private readonly bool $isBitrix24UserAdmin,
private readonly Uuid $id,
private readonly int $bitrix24UserId,
private readonly bool $isBitrix24UserAdmin,
/** bitrix24 portal unique id */
private readonly string $memberId,
private string $domainUrl,
private Bitrix24AccountStatus $status,
private AuthToken $authToken,
private readonly string $memberId,
private string $domainUrl,
private Bitrix24AccountStatus $status,
private AuthToken $authToken,
private readonly CarbonImmutable $createdAt,
private CarbonImmutable $updatedAt,
private int $applicationVersion,
private Scope $applicationScope,
bool $isEmitBitrix24AccountCreatedEvent = false,
) {
private CarbonImmutable $updatedAt,
private int $applicationVersion,
private Scope $applicationScope,
bool $isEmitBitrix24AccountCreatedEvent = false,
)
{
$this->addAccountCreatedEventIfNeeded($isEmitBitrix24AccountCreatedEvent);
}

Expand Down Expand Up @@ -194,37 +195,50 @@ public function applicationInstalled(string $applicationToken): void
);
}

/**
* @throws InvalidArgumentException
*/
#[\Override]
public function applicationUninstalled(string $applicationToken): void
private function guardEmpty($applicationToken)
{
if ('' === $applicationToken) {
throw new InvalidArgumentException('application token cannot be empty');
}
}

if (Bitrix24AccountStatus::active !== $this->status) {
private function guardTokenMismatch($applicationToken)
{
if ($this->applicationToken !== $applicationToken) {
throw new InvalidArgumentException(
sprintf(
'for uninstall account must be in status «active», current status - «%s»',
$this->status->name
'application token «%s» mismatch with application token «%s» for bitrix24 account %s for domain %s',
$applicationToken,
$this->applicationToken,
$this->id->toRfc4122(),
$this->domainUrl
)
);
}
}

/*if ($this->applicationToken !== $applicationToken) {
private function guardStatusIsActive()
{
if (Bitrix24AccountStatus::active !== $this->status) {
throw new InvalidArgumentException(
sprintf(
'application token «%s» mismatch with application token «%s» for bitrix24 account %s for domain %s',
$applicationToken,
$this->applicationToken,
$this->id->toRfc4122(),
$this->domainUrl
'for uninstall account must be in status «active», current status - «%s»',
$this->status->name
)
);
}*/
}
}

/**
* @throws InvalidArgumentException
*/
#[\Override]
public function applicationUninstalled(string $applicationToken): void
{

$this->guardEmpty($applicationToken);
$this->guardTokenMismatch($applicationToken);
$this->guardStatusIsActive();
$this->status = Bitrix24AccountStatus::deleted;
$this->updatedAt = new CarbonImmutable();
$this->events[] = new Bitrix24AccountApplicationUninstalledEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface;
use Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface;
use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Uid\Uuid;
Expand All @@ -19,7 +20,8 @@ class Bitrix24AccountRepository extends EntityRepository implements Bitrix24Acco
{
public function __construct(
EntityManagerInterface $entityManager
) {
)
{
parent::__construct($entityManager, $entityManager->getClassMetadata(Bitrix24Account::class));
}

Expand All @@ -31,7 +33,16 @@ public function __construct(
#[\Override]
public function getById(Uuid $uuid): Bitrix24AccountInterface
{
$account = $this->getEntityManager()->getRepository(Bitrix24Account::class)->find($uuid);

$account = $this->getEntityManager()->getRepository(Bitrix24Account::class)
->createQueryBuilder('b24')
->where('b24.id = :id')
->andWhere('b24.status != :status')
->setParameter('id', $uuid)
->setParameter('status', Bitrix24AccountStatus::deleted)
->getQuery()
->getOneOrNullResult();
// $account = $this->getEntityManager()->getRepository(Bitrix24Account::class)->find($uuid);
if (null === $account) {
throw new Bitrix24AccountNotFoundException(
sprintf('bitrix24 account not found by id %s', $uuid->toRfc4122())
Expand All @@ -43,8 +54,16 @@ public function getById(Uuid $uuid): Bitrix24AccountInterface

public function existsById(Uuid $uuid): bool
{
if ($this->getEntityManager()->getRepository(Bitrix24Account::class)->find($uuid))
{
$account = $this->getEntityManager()->getRepository(Bitrix24Account::class)
->createQueryBuilder('b24')
->where('b24.id = :id')
->andWhere('b24.status != :status')
->setParameter('id', $uuid)
->setParameter('status', Bitrix24AccountStatus::deleted)
->getQuery()
->getOneOrNullResult();

if ($account) {
return true;
}

Expand All @@ -64,11 +83,12 @@ public function save(Bitrix24AccountInterface $bitrix24Account): void
*/
#[\Override]
public function findByMemberId(
string $memberId,
string $memberId,
?Bitrix24AccountStatus $bitrix24AccountStatus = null,
?int $bitrix24UserId = null,
?bool $isAdmin = null
): array {
?int $bitrix24UserId = null,
?bool $isAdmin = null
): array
{
if ('' === trim($memberId)) {
throw new InvalidArgumentException('memberId cannot be empty');
}
Expand Down Expand Up @@ -178,10 +198,11 @@ public function findOneAdminByMemberId(string $memberId): ?Bitrix24AccountInterf
*/
#[\Override]
public function findByDomain(
string $domainUrl,
string $domainUrl,
?Bitrix24AccountStatus $bitrix24AccountStatus = null,
?bool $isAdmin = null
): array {
?bool $isAdmin = null
): array
{
if ('' === trim($domainUrl)) {
throw new InvalidArgumentException('domainUrl cannot be an empty string');
}
Expand Down
4 changes: 0 additions & 4 deletions src/Bitrix24Accounts/UseCase/InstallFinish/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ private function validate(): void
if (empty($this->memberId)) {
throw new InvalidArgumentException('Member ID 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.');
}
Expand Down
4 changes: 0 additions & 4 deletions src/Bitrix24Accounts/UseCase/InstallStart/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,5 @@ private function validate(): void
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.');
}
}
}
33 changes: 25 additions & 8 deletions src/Bitrix24Accounts/UseCase/InstallStart/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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\Exceptions\Bitrix24AccountNotFoundException;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface;
use Carbon\CarbonImmutable;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -44,15 +45,31 @@
$command->applicationScope,
true
);
$this->bitrix24AccountRepository->save($bitrix24Account);
$this->flusher->flush($bitrix24Account);

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

Check failure on line 49 in src/Bitrix24Accounts/UseCase/InstallStart/Handler.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, highest, ubuntu-latest)

Call to an undefined method Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface::existsById().

if (!$isAccountExists) {
$this->bitrix24AccountRepository->save($bitrix24Account);
$this->flusher->flush($bitrix24Account);

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

throw new Bitrix24AccountNotFoundException(
sprintf('bitrix24account with uuid "%s" already exists', $command->uuid->toRfc4122())
);
}
}
}
4 changes: 2 additions & 2 deletions src/Bitrix24Accounts/UseCase/RenewAuthToken/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\RenewAuthToken;

use Bitrix24\SDK\Core\Response\DTO\RenewedAuthToken;

readonly class Command
{
public function __construct(
public RenewedAuthToken $renewedAuthToken,
public ?int $bitrix24UserId = null,
) {}
) {
}
}
14 changes: 13 additions & 1 deletion src/Bitrix24Accounts/UseCase/Uninstall/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@

namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\Uninstall;

use InvalidArgumentException;
use Symfony\Component\Uid\Uuid;

readonly class Command
{
public function __construct(
/**
* @var non-empty-string $applicationToken
*/
public string $applicationToken
) {}
) {
$this->validate();
}

private function validate(): void
{
if (empty($this->applicationToken) || !Uuid::isValid($this->applicationToken)) {
throw new InvalidArgumentException('Empty application token or invalid application token.');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class Bitrix24AccountBuilder

private readonly int $applicationVersion;

private readonly Scope $applicationScope;
private Scope $applicationScope;

private ?string $applicationToken = null;

public function __construct()
{
Expand Down Expand Up @@ -73,6 +75,18 @@ public function withDomainUrl(string $domainUrl): self
return $this;
}

public function withApplicationScope(Scope $applicationScope): self
{
$this->applicationScope = $applicationScope;
return $this;
}

public function withApplicationToken(string $applicationToken): self
{
$this->applicationToken = $applicationToken;
return $this;
}

public function withStatus(Bitrix24AccountStatus $bitrix24AccountStatus): self
{
$this->status = $bitrix24AccountStatus;
Expand All @@ -81,7 +95,7 @@ public function withStatus(Bitrix24AccountStatus $bitrix24AccountStatus): self

public function build(): AggregateRootEventsEmitterInterface&Bitrix24AccountInterface
{
return new Bitrix24Account(
$account = new Bitrix24Account(
$this->id,
$this->bitrix24UserId,
$this->isBitrix24UserAdmin,
Expand All @@ -94,5 +108,11 @@ public function build(): AggregateRootEventsEmitterInterface&Bitrix24AccountInte
$this->applicationVersion,
$this->applicationScope
);

if (isset($this->applicationToken) && $this->status == Bitrix24AccountStatus::new) {
$account->applicationInstalled($this->applicationToken);
}

return $account;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testFinishInstallationWithHappyPath(): void
);

$updated = $this->repository->getById($bitrix24Account->getId());
$this->assertEquals('active', $updated->getStatus()->value,'expected status is active');
$this->assertEquals(Bitrix24AccountStatus::active, $updated->getStatus(),'expected status is active');
$this->assertTrue(
$updated->isApplicationTokenValid($applicationToken),
sprintf('failed application token «%s» validation for bitrix24 account with id «%s»',
Expand Down
Loading
Loading