Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
arnapou committed Nov 28, 2023
1 parent f6b1ce9 commit f388602
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@
use Core\Application\Configuration\User\Repository\ReadUserRepositoryInterface;
use Core\Domain\Configuration\User\Model\User;

/**
* @phpstan-type _UserRecord array{
* contact_id: int|string,
* contact_alias: string,
* contact_name: string,
* contact_email: string,
* contact_admin: string,
* contact_theme: string,
* user_interface_density: string,
* user_can_reach_frontend: string,
* }
*/
class DbReadUserRepository extends AbstractRepositoryDRB implements ReadUserRepositoryInterface
{
use LoggerTrait;
Expand Down Expand Up @@ -111,9 +123,7 @@ public function findAllUsers(): array
$users = [];

while ($result = $statement->fetch(\PDO::FETCH_ASSOC)) {
/**
* @var array<string, string> $result
*/
/** @var _UserRecord $result */
$users[] = DbUserFactory::createFromRecord($result);
}

Expand All @@ -125,7 +135,6 @@ public function findAllUsers(): array
*/
public function findByContactGroups(ContactInterface $contact): array
{

$request = <<<'SQL'
SELECT SQL_CALC_FOUND_ROWS DISTINCT
contact_id,
Expand All @@ -134,7 +143,8 @@ public function findByContactGroups(ContactInterface $contact): array
contact_email,
contact_admin,
contact_theme,
user_interface_density
user_interface_density,
contact_oreon AS `user_can_reach_frontend`
FROM `:db`.contact
INNER JOIN `:db`.contactgroup_contact_relation AS cg
ON cg.contact_contact_id = contact.contact_id
Expand Down Expand Up @@ -178,9 +188,7 @@ public function findByContactGroups(ContactInterface $contact): array
$users = [];

while ($result = $statement->fetch(\PDO::FETCH_ASSOC)) {
/**
* @var array<string, string> $result
*/
/** @var _UserRecord $result */
$users[] = DbUserFactory::createFromRecord($result);
}

Expand All @@ -193,14 +201,26 @@ public function findByContactGroups(ContactInterface $contact): array
public function findById(int $userId): ?User
{
$statement = $this->db->prepare(
$this->translateDbName('SELECT * FROM `:db`.contact WHERE contact_id = :contact_id')
$this->translateDbName(
<<<'SQL'
SELECT
contact_id,
contact_alias,
contact_name,
contact_email,
contact_admin,
contact_theme,
user_interface_density,
contact_oreon AS `user_can_reach_frontend`
FROM `:db`.contact
WHERE contact_id = :contact_id
SQL
)
);
$statement->bindValue(':contact_id', $userId, \PDO::PARAM_INT);
$statement->execute();
if ($result = $statement->fetch(\PDO::FETCH_ASSOC)) {
/**
* @var array<string, string> $result
*/
/** @var _UserRecord $result */
return DbUserFactory::createFromRecord($result);
}

Expand All @@ -225,11 +245,14 @@ public function findUserIdsByAliases(array $userAliases): array
$bindValues[':' . $key] = $userAlias;
}

$bindFields = implode(',', array_keys($bindValues));
$statement = $this->db->prepare(
$this->translateDbName(
'SELECT contact_id
FROM `:db`.contact
WHERE contact_alias IN (' . implode(',', array_keys($bindValues)) . ')'
<<<SQL
SELECT contact_id
FROM `:db`.contact
WHERE contact_alias IN ({$bindFields})
SQL
)
);

Expand All @@ -255,8 +278,10 @@ public function findUserIdsByAliases(array $userAliases): array
public function findAvailableThemes(): array
{
$statement = $this->db->query(
'SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = \'contact\' AND COLUMN_NAME = \'contact_theme\''
<<<'SQL'
SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'contact' AND COLUMN_NAME = 'contact_theme'
SQL
);
if ($statement !== false && $result = $statement->fetch(\PDO::FETCH_ASSOC)) {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@

use Core\Domain\Configuration\User\Model\User;

/**
* @phpstan-import-type _UserRecord from DbReadUserRepository
*/
class DbUserFactory
{
/**
* @param array<string, string> $user
* @param _UserRecord $user
*
* @throws \Assert\AssertionFailedException
*
Expand Down

0 comments on commit f388602

Please sign in to comment.