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
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ include $(ENV)
-include $(ENV_LOCAL)


start-rector: vendor
vendor/bin/rector process tests --config=rector.php

coding-standards: vendor
vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --diff --verbose
Expand Down Expand Up @@ -46,6 +48,10 @@ init:
@echo "run application…"
docker-compose up -d


clear:
docker-compose run --rm php-cli composer clear-cache

up:
@echo "run application…"
docker-compose up --build -d
Expand Down Expand Up @@ -110,7 +116,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 'testCreateExistingAccount' tests/Functional/Bitrix24Accounts/UseCase/InstallStart/HandlerTest.php
docker-compose run --rm php-cli php -dxdebug.start_with_request=yes vendor/bin/phpunit --filter 'testChangeDomainUrlWithHappyPath' tests/Functional/Bitrix24Accounts/UseCase/ChangeDomainUrl/HandlerTest.php

schema-drop:
docker-compose run --rm php-cli php bin/doctrine orm:schema-tool:drop --force
Expand Down
2 changes: 1 addition & 1 deletion src/Bitrix24Accounts/Entity/Bitrix24Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public function getUpdatedAt(): CarbonImmutable
* @throws InvalidArgumentException
*/
#[\Override]
public function updateApplicationVersion(int $version, ?Scope $newScope): void
public function updateApplicationVersion(AuthToken $authToken, int $b24UserId, int $version, ?Scope $newScope): void
{
if (Bitrix24AccountStatus::active !== $this->status) {
throw new InvalidArgumentException(
Expand Down
38 changes: 10 additions & 28 deletions src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,19 @@

namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\ChangeDomainUrl;

use Bitrix24\Lib\Bitrix24Accounts\ValueObjects\Domain;

readonly class Command
{
public function __construct(
/**
* @var non-empty-string $oldDomain
*/
public string $oldDomain,
/**
* @var non-empty-string $newDomain
*/
public string $newDomain
) {
$this->validateDomain($oldDomain, 'oldDomainUrlHost');
$this->validateDomain($newDomain, 'newDomainUrlHost');
}
public string $oldDomain;

private function validateDomain(string $domain, string $parameterName): void
{
// Регулярное выражение для проверки допустимых символов (латиница и кириллица)
$patternValidChars = '/^((?!-)[A-Za-zА-Яа-яЁё0-9-]{1,63}(?<!-)\.)+[A-Za-zА-Яа-яЁё]{2,6}$/u';
public string $newDomain;

// Проверка общей длины (1-253 символа)
$patternLengthCheck = '/^.{1,253}$/';

// Проверка длины каждой метки (1-63 символа, включая кириллицу)
$patternLengthEachLabel = '/^[A-Za-zА-Яа-яЁё0-9-]{1,63}(\.[A-Za-zА-Яа-яЁё0-9-]{1,63})*$/u';
if (
in_array(preg_match($patternValidChars, $domain), [0, false], true)
|| in_array(preg_match($patternLengthCheck, $domain), [0, false], true)
|| in_array(preg_match($patternLengthEachLabel, $domain), [0, false], true)) {
throw new \InvalidArgumentException(sprintf('Invalid value for %s: %s', $parameterName, $domain));
}
public function __construct(
Domain $oldDomain,
Domain $newDomain
) {
$this->oldDomain = $oldDomain->getValue();
$this->newDomain = $newDomain->getValue();
}
}
29 changes: 7 additions & 22 deletions src/Bitrix24Accounts/UseCase/InstallFinish/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,34 @@

namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\InstallFinish;

use Bitrix24\Lib\Bitrix24Accounts\ValueObjects\Domain;

readonly class Command
{
public string $domain;

public function __construct(
public string $applicationToken,
public string $memberId,
public string $domainUrl,
Domain $domain,
public int $bitrix24UserId,
) {
$this->validate();
$this->domain = $domain->getValue();
}

private function validate(): void
{
if ('' === $this->applicationToken || '0' === $this->applicationToken) {
if ('' === $this->applicationToken) {
throw new \InvalidArgumentException('Application token cannot be empty.');
}

if ('' === $this->memberId || '0' === $this->memberId) {
throw new \InvalidArgumentException('Member ID cannot be empty.');
}

$this->validateDomain($this->domainUrl);

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

private function validateDomain(string $domain): void
{
// Регулярное выражение для проверки допустимых символов (латиница и кириллица)
$patternValidChars = '/^((?!-)[A-Za-zА-Яа-яЁё0-9-]{1,63}(?<!-)\.)+[A-Za-zА-Яа-яЁё]{2,6}$/u';

// Проверка общей длины (1-253 символа)
$patternLengthCheck = '/^.{1,253}$/';

// Проверка длины каждой метки (1-63 символа, включая кириллицу)
$patternLengthEachLabel = '/^[A-Za-zА-Яа-яЁё0-9-]{1,63}(\.[A-Za-zА-Яа-яЁё0-9-]{1,63})*$/u';
if (
in_array(preg_match($patternValidChars, $domain), [0, false], true)
|| in_array(preg_match($patternLengthCheck, $domain), [0, false], true)
|| in_array(preg_match($patternLengthEachLabel, $domain), [0, false], true)) {
throw new \InvalidArgumentException('Domain URL is not valid.');
}
}
}
6 changes: 3 additions & 3 deletions src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public function __construct(
public function handle(Command $command): void
{
$this->logger->info('Bitrix24Accounts.InstallFinish.start', [
'b24_domain_url' => $command->domainUrl,
'b24_domain_url' => $command->domain,
'b24_member_id' => $command->memberId,
'b24_application_id' => $command->applicationToken,
'b24_user_id' => $command->bitrix24UserId,
]);

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

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

Expand All @@ -47,7 +47,7 @@ public function handle(Command $command): void
$this->logger->info(
'Bitrix24Accounts.InstallFinish.Finish',
[
'b24_domain_url' => $command->domainUrl,
'b24_domain_url' => $command->domain,
'b24_member_id' => $command->memberId,
'b24_application_id' => $command->applicationToken,
'b24_user_id' => $command->bitrix24UserId,
Expand Down
32 changes: 6 additions & 26 deletions src/Bitrix24Accounts/UseCase/InstallStart/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,41 @@

namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\InstallStart;

use Bitrix24\Lib\Bitrix24Accounts\ValueObjects\Domain;
use Bitrix24\SDK\Core\Credentials\AuthToken;
use Bitrix24\SDK\Core\Credentials\Scope;
use Symfony\Component\Uid\Uuid;

readonly class Command
{
public string $domain;

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

private function validate(): void
{
if (empty($this->uuid)) {
throw new \InvalidArgumentException('Empty UUID provided.');
}

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

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

$this->validateDomain($this->domainUrl);

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

private function validateDomain(string $domain): void
{
// Регулярное выражение для проверки допустимых символов (латиница и кириллица)
$patternValidChars = '/^((?!-)[A-Za-zА-Яа-яЁё0-9-]{1,63}(?<!-)\.)+[A-Za-zА-Яа-яЁё]{2,6}$/u';

// Проверка общей длины (1-253 символа)
$patternLengthCheck = '/^.{1,253}$/';

// Проверка длины каждой метки (1-63 символа, включая кириллицу)
$patternLengthEachLabel = '/^[A-Za-zА-Яа-яЁё0-9-]{1,63}(\.[A-Za-zА-Яа-яЁё0-9-]{1,63})*$/u';
if (
in_array(preg_match($patternValidChars, $domain), [0, false], true)
|| in_array(preg_match($patternLengthCheck, $domain), [0, false], true)
|| in_array(preg_match($patternLengthEachLabel, $domain), [0, false], true)) {
throw new \InvalidArgumentException('Domain URL is not valid.');
}
}
}
8 changes: 4 additions & 4 deletions src/Bitrix24Accounts/UseCase/InstallStart/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{
$this->logger->info('Bitrix24Accounts.InstallStart.start', [
'id' => $command->uuid->toRfc4122(),
'domain_url' => $command->domainUrl,
'domain' => $command->domain,
'member_id' => $command->memberId,
]);

Expand All @@ -33,7 +33,7 @@
$command->bitrix24UserId,
$command->isBitrix24UserAdmin,
$command->memberId,
$command->domainUrl,
$command->domain,
Bitrix24AccountStatus::new,
$command->authToken,
new CarbonImmutable(),
Expand All @@ -43,7 +43,7 @@
true
);

$isAccountExists = $this->bitrix24AccountRepository->existsById($bitrix24Account->getId());

Check failure on line 46 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);
Expand All @@ -53,7 +53,7 @@
'Bitrix24Accounts.InstallStart.Finish',
[
'id' => $command->uuid->toRfc4122(),
'domain_url' => $command->domainUrl,
'domain_url' => $command->domain,
'member_id' => $command->memberId,
]
);
Expand All @@ -62,7 +62,7 @@
'Bitrix24Accounts.InstallStart.AlreadyExists',
[
'id' => $command->uuid->toRfc4122(),
'domain_url' => $command->domainUrl,
'domain' => $command->domain,
'member_id' => $command->memberId,
]
);
Expand Down
4 changes: 2 additions & 2 deletions src/Bitrix24Accounts/UseCase/Uninstall/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public function __construct(

private function validate(): void
{
if ('' === $this->applicationToken || '0' === $this->applicationToken) {
throw new \InvalidArgumentException('Empty application token application token.');
if ('' === $this->applicationToken) {
throw new \InvalidArgumentException('Application token must be a non-empty string.');
}
}
}
39 changes: 39 additions & 0 deletions src/Bitrix24Accounts/ValueObjects/Domain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Bitrix24\Lib\Bitrix24Accounts\ValueObjects;

readonly class Domain
{
private string $value;

public function __construct(string $domain)
{
$this->validate($domain);
$this->value = $domain;
}

public function getValue(): string
{
return $this->value;
}

private function validate(string $domain): void
{
// Регулярное выражение для проверки допустимых символов (латиница и кириллица)
$patternValidChars = '/^((?!-)[A-Za-zА-Яа-яЁё0-9-]{1,63}(?<!-)\.)+[A-Za-zА-Яа-яЁё]{2,6}$/u';

// Проверка общей длины (1-253 символа)
$patternLengthCheck = '/^.{1,253}$/';

// Проверка длины каждой метки (1-63 символа, включая кириллицу)
$patternLengthEachLabel = '/^[A-Za-zА-Яа-яЁё0-9-]{1,63}(\.[A-Za-zА-Яа-яЁё0-9-]{1,63})*$/u';
if (
in_array(preg_match($patternValidChars, $domain), [0, false], true)
|| in_array(preg_match($patternLengthCheck, $domain), [0, false], true)
|| in_array(preg_match($patternLengthEachLabel, $domain), [0, false], true)) {
throw new \InvalidArgumentException(sprintf('Invalid domain: %s', $domain));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function withStatus(Bitrix24AccountStatus $bitrix24AccountStatus): self
return $this;
}

public function build(): AggregateRootEventsEmitterInterface&Bitrix24AccountInterface
public function build(): Bitrix24Account
{
$bitrix24Account = new Bitrix24Account(
$this->id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Uid\Uuid;
use Bitrix24\Lib\Bitrix24Accounts\ValueObjects\Domain;

/**
* @internal
Expand Down Expand Up @@ -74,8 +75,8 @@ public function testChangeDomainUrlForAccount(): void

$this->handler->handle(
new Bitrix24Accounts\UseCase\ChangeDomainUrl\Command(
$oldDomainUrl,
$newDomainUrl
new Domain($oldDomainUrl),
new Domain($newDomainUrl)
)
);

Expand Down Expand Up @@ -126,8 +127,8 @@ public function testChangeDomainUrlForManyAccounts(): void

$this->handler->handle(
new Bitrix24Accounts\UseCase\ChangeDomainUrl\Command(
$oldDomainUrl,
$newDomainUrl
new Domain($oldDomainUrl),
new Domain($newDomainUrl)
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Uid\Uuid;
use Bitrix24\Lib\Bitrix24Accounts\ValueObjects\Domain;

/**
* @internal
Expand Down Expand Up @@ -78,7 +79,7 @@ public function testFinishInstallationApplication(): void
new Bitrix24Accounts\UseCase\InstallFinish\Command(
$applicationToken,
$bitrix24Account->getMemberId(),
$bitrix24Account->getDomainUrl(),
new Domain($bitrix24Account->getDomainUrl()),
$bitrix24Account->getBitrix24UserId()
)
);
Expand Down
Loading
Loading