Skip to content

Commit

Permalink
Refactor repository and entity manager usage.
Browse files Browse the repository at this point in the history
Standardize usage of EntityManagerInterface and improve type inconsistencies. Apply `#[Override]` attribute in tests, and add null checks and validation for better robustness.

Signed-off-by: mesilov <[email protected]>
  • Loading branch information
mesilov committed Oct 6, 2024
1 parent 85ceeb4 commit e6c002b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/Bitrix24Accounts/Entity/Bitrix24Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ class Bitrix24Account implements Bitrix24AccountInterface
public function __construct(
#[ORM\Id]
#[ORM\Column(type: UuidType::NAME, unique: true)]
private readonly Uuid $id,
private readonly Uuid $id,
#[ORM\Column(name: 'b24_user_id', type: 'integer', nullable: false)]
#[SerializedName('b24_user_id')]
private readonly int $bitrix24UserId,
private readonly int $bitrix24UserId,
private readonly bool $isBitrix24UserAdmin,
/** bitrix24 portal unique id */
#[ORM\Column(name: 'member_id', type: 'string', nullable: false)]
#[SerializedName('member_id')]
private readonly string $memberId,
private readonly string $memberId,
private string $domainUrl,
private Bitrix24AccountStatus $accountStatus,
AuthToken $authToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface;
use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException;
use Bitrix24\SDK\Lib\Bitrix24Accounts\Entity\Bitrix24Account;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Override;
use Symfony\Component\Uid\Uuid;

class Bitrix24AccountRepository extends EntityRepository implements Bitrix24AccountRepositoryInterface
{
public function __construct(EntityManager $entityManager)
public function __construct(EntityManagerInterface $entityManager)
{
parent::__construct($entityManager, $entityManager->getClassMetadata(Bitrix24Account::class));
}
Expand Down Expand Up @@ -75,6 +75,7 @@ public function delete(Uuid $uuid, bool $flush = false): void
if ($bitrix24Account === null) {
throw new Bitrix24AccountNotFoundException(sprintf('bitrix24 account not found by id %s', $uuid->toRfc4122()));
}

$this->getEntityManager()->remove($bitrix24Account);

if ($flush) {
Expand All @@ -100,6 +101,7 @@ public function findOneAdminByMemberId(string $memberId): ?Bitrix24AccountInterf
if (trim($memberId) === '') {
throw new InvalidArgumentException('memberId cannot be an empty string');
}

return $this->getEntityManager()->getRepository(Bitrix24Account::class)->findOneBy(
[
'memberId' => $memberId,
Expand All @@ -122,9 +124,10 @@ public function findByDomain(string $domainUrl, ?Bitrix24AccountStatus $bitrix24
$criteria = [
'domainUrl' => $domainUrl,
];
if ($bitrix24AccountStatus !== null) {
if ($bitrix24AccountStatus instanceof \Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus) {
$criteria['status'] = $bitrix24AccountStatus->name;
}

if ($isAdmin !== null) {
$criteria['isBitrix24UserAdmin'] = $isAdmin;
}
Expand Down
14 changes: 4 additions & 10 deletions src/Bitrix24Accounts/ReadModel/Fetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,18 @@

class Fetcher
{
private EntityManagerInterface $em;
private PaginatorInterface $paginator;

public function __construct(
EntityManagerInterface $em,
PaginatorInterface $paginator
)
private readonly EntityManagerInterface $em,
private readonly PaginatorInterface $paginator)
{
$this->em = $em;
$this->paginator = $paginator;
}

public function list(
int $page,
int $size
): PaginationInterface
{
$qb = $this->em->getConnection()->createQueryBuilder()
$queryBuilder = $this->em->getConnection()->createQueryBuilder()
->select(
'b24account.id as id',
'b24account.status as status',
Expand All @@ -40,6 +34,6 @@ public function list(
->from('bitrix24account', 'b24account')
->orderBy('b24account.created_at_utc', 'DESC');

return $this->paginator->paginate($qb, $page, $size);
return $this->paginator->paginate($queryBuilder, $page, $size);
}
}
10 changes: 7 additions & 3 deletions tests/EntityManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ public static function get(): EntityManagerInterface
if (!array_key_exists('DATABASE_HOST', $_ENV)) {
throw new WrongConfigurationException('DATABASE_HOST not defined in $_ENV');
}

if (!array_key_exists('DATABASE_USER', $_ENV)) {
throw new WrongConfigurationException('DATABASE_USER not defined in $_ENV');
}

if (!array_key_exists('DATABASE_PASSWORD', $_ENV)) {
throw new WrongConfigurationException('DATABASE_PASSWORD not defined in $_ENV');
}

if (!array_key_exists('DATABASE_NAME', $_ENV)) {
throw new WrongConfigurationException('DATABASE_NAME not defined in $_ENV');
}
Expand All @@ -55,13 +58,14 @@ public static function get(): EntityManagerInterface
if (!Type::hasType(UuidType::NAME)) {
Type::addType(UuidType::NAME, UuidType::class);
}

if (!Type::hasType('carbon_immutable')) {
Type::addType('carbon_immutable', CarbonImmutableType::class);
}

$config = ORMSetup::createAttributeMetadataConfiguration($paths, $isDevMode);
$connection = DriverManager::getConnection($connectionParams, $config);
$entityManager = new EntityManager($connection, $config);
$configuration = ORMSetup::createAttributeMetadataConfiguration($paths, $isDevMode);
$connection = DriverManager::getConnection($connectionParams, $configuration);
$entityManager = new EntityManager($connection, $configuration);
// todo разобраться, почему так, без этого объекты оставались в кеше и при find мы получали старые значения
$entityManager->clear();
$entityManager->flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ protected function createBitrix24AccountImplementation(
);
}

#[Override]
protected function createBitrix24AccountRepositoryImplementation(): Bitrix24AccountRepositoryInterface
{
return new Bitrix24AccountRepository(EntityManagerFactory::get());
Expand Down

0 comments on commit e6c002b

Please sign in to comment.