src/Lms/Modules/IdentityAccess/Infrastructure/Read/DbalUserQuery.php line 151

Open in your IDE?
  1. <?php
  2. namespace CodersLab\Lms\Modules\IdentityAccess\Infrastructure\Read;
  3. use CodersLab\Lms\Modules\IdentityAccess\Application\Query\Model\User;
  4. use CodersLab\Lms\Modules\IdentityAccess\Application\Query\UserQuery;
  5. use CodersLab\Lms\SharedKernel\Application\TenantLanguages;
  6. use CodersLab\Lms\SharedKernel\Common\Exception\NotFoundException;
  7. use CodersLab\Lms\SharedKernel\Domain\Identity\Uuid;
  8. use CodersLab\Lms\SharedKernel\Domain\Role;
  9. use DateTime;
  10. use Doctrine\DBAL\Connection;
  11. final class DbalUserQuery implements UserQuery
  12. {
  13.     private Connection $connection;
  14.     private TenantLanguages $tenantLanguages;
  15.     public function __construct(Connection $connectionTenantLanguages $tenantLanguages)
  16.     {
  17.         $this->connection $connection;
  18.         $this->tenantLanguages $tenantLanguages;
  19.     }
  20.     public function getById(int $id): User
  21.     {
  22.         return $this->getBy('id', (string)$id);
  23.     }
  24.     public function getByEmail(string $email): User
  25.     {
  26.         return $this->getBy('email'$email);
  27.     }
  28.     public function getRoles(int $id): array
  29.     {
  30.         $roles $this->connection->fetchOne(
  31.             ' SELECT roles FROM fos_user WHERE id = :uid',
  32.             [
  33.                 'uid' => $id,
  34.             ]
  35.         );
  36.         $roles unserialize($roles) ?? [];
  37.         return array_map(fn (string $role) => new Role($role), $roles);
  38.     }
  39.     public function hasCourseWithRepositories(int $id): bool
  40.     {
  41.         $sql = <<<SQL
  42. SELECT COUNT(i.id) FROM ia_user_access ua
  43. JOIN instance i on ua.instance_id = i.id
  44. JOIN materials_course mc on i.course_id = mc.id
  45. WHERE mc.repositories = 1 AND ua.user_id = :id
  46. SQL;
  47.         $stmt $this->connection->executeQuery($sql, [
  48.             'id' => $id,
  49.         ]);
  50.         return (int)$stmt->fetchOne() > 0;
  51.     }
  52.     public function getByGithubLogin(string $githubLogin): User
  53.     {
  54.         return $this->getBy('github'$githubLogin);
  55.     }
  56.     public function lastSeenMaterial(string $id): ?User\LastSeenMaterial
  57.     {
  58.         $sql = <<<SQL
  59. SELECT iaua.instance_id, 
  60.        iaua.last_seen_material_id material_id,
  61.        iaua.last_seen_material_date date,
  62.        mct.chapter_id
  63. FROM ia_user_access iaua
  64. JOIN fos_user fu on iaua.user_id = fu.id
  65. JOIN instance i on i.id = iaua.instance_id
  66. JOIN materials_course_tree mct on mct.course_id = i.course_id and mct.material_id = iaua.last_seen_material_id
  67. WHERE iaua.user_id = :id 
  68.   AND iaua.last_seen_material_id IS NOT NULL 
  69.   AND fu.show_welcome_page = 1
  70. ORDER BY iaua.last_seen_material_date DESC
  71. LIMIT 1
  72. SQL;
  73.         $stmt $this->connection->executeQuery($sql, [
  74.             'id' => $id,
  75.         ]);
  76.         $res $stmt->fetchAssociative();
  77.         if ($res !== false) {
  78.             return new User\LastSeenMaterial(
  79.                 new Uuid($res['material_id']),
  80.                 $res['instance_id'],
  81.                 new DateTime($res['date']),
  82.                 new Uuid($res['chapter_id'])
  83.             );
  84.         }
  85.         return null;
  86.     }
  87.     /**
  88.      * @return array<User>
  89.      */
  90.     public function findUsersWithoutActivation(\DateTimeImmutable $registerDate): array
  91.     {
  92.         $qb $this->connection->createQueryBuilder();
  93.         $qb->select([
  94.             'u.id',
  95.             'u.name',
  96.             'u.surname',
  97.             'u.email',
  98.             'u.roles',
  99.             'u.github',
  100.             'u.github_id',
  101.             'u.language',
  102.             'u.created',
  103.         ])->from('fos_user''u')
  104.             ->where('u.enabled = 0')
  105.             ->andWhere('CAST(u.created as DATE) = :date')
  106.             ->setParameter('date'$registerDate->format('Y-m-d'));
  107.         $data $qb->execute()->fetchAllAssociative();
  108.         return array_map(function (array $row) {
  109.             $roles unserialize($row['roles']);
  110.             $roles array_filter($roles);
  111.             $roles = empty($roles) ? [Role::USER] : $roles;
  112.             return new User(
  113.                 (int)$row['id'],
  114.                 $row['name'],
  115.                 $row['surname'],
  116.                 $row['email'],
  117.                 $row['github'],
  118.                 $row['github_id'],
  119.                 'https://www.gravatar.com/avatar/' md5($row['email']),
  120.                 $roles,
  121.                 new User\Redirect(User\RedirectType::LIST(), []),
  122.                 $row['lang'] ?? $this->tenantLanguages->getDefaultLocale(),
  123.                 $this->tenantLanguages->getUserLanguages(),
  124.                 false,
  125.                 new \DateTimeImmutable($row['created'])
  126.             );
  127.         }, $data);
  128.     }
  129.     private function getBy(string $parameterstring $value): User
  130.     {
  131.         $qb $this->connection->createQueryBuilder();
  132.         $qb->select([
  133.             'u.id',
  134.             'u.name',
  135.             'u.surname',
  136.             'u.email',
  137.             'u.roles',
  138.             'u.github',
  139.             'u.github_id',
  140.             'u.language',
  141.             'u.created',
  142.         ])->from('fos_user''u')
  143.             ->where('u.' $parameter '  = :parameter')
  144.             ->setParameter('parameter'$value);
  145.         $data $qb->execute()->fetchAssociative();
  146.         if ($data === false) {
  147.             throw NotFoundException::entityForCriteria('User', [$parameter => $value]);
  148.         }
  149.         $roles unserialize($data['roles']);
  150.         $roles array_filter($roles);
  151.         $roles = empty($roles) ? [Role::USER] : $roles;
  152.         $lastSeen $this->lastSeenMaterial($data['id']);
  153.         if ($lastSeen instanceof User\LastSeenMaterial) {
  154.             $redirect = new User\Redirect(User\RedirectType::LAST_MATERIAL(), [
  155.                 'materialId' => (string)$lastSeen->getId(),
  156.                 'instanceId' => $lastSeen->getInstanceId(),
  157.                 'chapterId' => (string)$lastSeen->getChapterId(),
  158.             ]);
  159.         } else {
  160.             $redirect = new User\Redirect(User\RedirectType::LIST(), []);
  161.         }
  162.         return new User(
  163.             (int)$data['id'],
  164.             $data['name'],
  165.             $data['surname'],
  166.             $data['email'],
  167.             $data['github'],
  168.             $data['github_id'],
  169.             'https://www.gravatar.com/avatar/' md5($data['email']),
  170.             $roles,
  171.             $redirect,
  172.             $data['lang'] ?? $this->tenantLanguages->getDefaultLocale(),
  173.             $this->tenantLanguages->getUserLanguages(),
  174.             false,
  175.             new \DateTimeImmutable($data['created'])
  176.         );
  177.     }
  178. }