Skip to content

Commit

Permalink
Sync, blog-api, part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Sep 10, 2024
1 parent 7daaf5b commit 0b8a0d1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 28 deletions.
1 change: 0 additions & 1 deletion blog-api/config/environments/test/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
'enabled' => false,
],
'yiisoft/yii-cycle' => [
// DBAL config
'dbal' => [
'connections' => [
'sqlite' => new SQLiteDriverConfig(
Expand Down
9 changes: 2 additions & 7 deletions blog-api/src/Auth/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@
#[OA\SecurityScheme(securityScheme: 'ApiKey', type: 'apiKey', name: 'X-Api-Key', in: 'header')]
final class AuthController
{
private ResponseFactory $responseFactory;
private UserService $userService;

public function __construct(
ResponseFactory $responseFactory,
UserService $userService
private ResponseFactory $responseFactory,
private UserService $userService,
) {
$this->responseFactory = $responseFactory;
$this->userService = $userService;
}

#[OA\Post(
Expand Down
15 changes: 7 additions & 8 deletions blog-api/src/User/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@

use Cycle\ORM\ORMInterface;
use Cycle\ORM\Select;
use Cycle\ORM\Transaction;
use Yiisoft\Auth\IdentityInterface;
use Yiisoft\Auth\IdentityRepositoryInterface;
use Yiisoft\Auth\IdentityWithTokenRepositoryInterface;
use Yiisoft\Data\Cycle\Reader\EntityReader;
use Yiisoft\Data\Cycle\Writer\EntityWriter;
use Yiisoft\Data\Reader\Sort;

final class UserRepository extends Select\Repository implements IdentityWithTokenRepositoryInterface, IdentityRepositoryInterface
{
private ORMInterface $orm;

public function __construct(Select $select, ORMInterface $orm)
public function __construct(
Select $select,
private ORMInterface $orm,
private EntityWriter $entityWriter,
)
{
$this->orm = $orm;
parent::__construct($select);
}

Expand Down Expand Up @@ -52,9 +53,7 @@ public function findByLogin(string $login): ?IdentityInterface

public function save(IdentityInterface $user): void
{
$transaction = new Transaction($this->orm);
$transaction->persist($user);
$transaction->run();
$this->entityWriter->write([$user]);
}

private function findIdentityBy(string $field, string $value): ?IdentityInterface
Expand Down
17 changes: 6 additions & 11 deletions blog-api/src/User/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,11 @@

final class UserService
{
private IdentityRepositoryInterface $identityRepository;
private CurrentUser $currentUser;
private QueueFactoryInterface $queueFactory;

public function __construct(
CurrentUser $currentUser,
IdentityRepositoryInterface $identityRepository,
QueueFactoryInterface $queueFactory
private CurrentUser $currentUser,
private IdentityRepositoryInterface $identityRepository,
private QueueFactoryInterface $queueFactory,
) {
$this->currentUser = $currentUser;
$this->identityRepository = $identityRepository;
$this->queueFactory = $queueFactory;
}

/**
Expand All @@ -40,7 +33,9 @@ public function __construct(
*/
public function login(string $login, string $password): IdentityInterface
{
$identity = $this->identityRepository->findByLogin($login);
/** @var UserRepository $identityRepository */
$identityRepository = $this->identityRepository;
$identity = $identityRepository->findByLogin($login);
if ($identity === null) {
throw new BadRequestException('No such user.');
}
Expand Down
1 change: 0 additions & 1 deletion blog-api/tests/Acceptance/AuthCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ final class AuthCest
{
public function testAuth(AcceptanceTester $I): void
{
$I->haveHttpHeader('Content-Type', 'application/json');
$I->sendPOST(
'/auth/',
[
Expand Down

0 comments on commit 0b8a0d1

Please sign in to comment.