Skip to content

Commit

Permalink
Add AuthorizationRequestTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
huntmori committed Dec 29, 2023
1 parent 92040a7 commit 2166b97
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src/Domain/Common/models/AuthorizationRequestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Domain\Common\models;
use Psr\Http\Message\ServerRequestInterface as Request;

trait AuthorizationRequestTrait
{
static string $tokenHeaderKey = "Authorization";

public function extractToken(Request $request) : ?string {
if($request->hasHeader(self::$tokenHeaderKey)) {
$request->getHeader(self::$tokenHeaderKey);
}
return null;
}
}
3 changes: 2 additions & 1 deletion src/Domain/Profile/controller/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function getUserProfile($userIdx) : array

public function createUserProfile(Request $request, Response $response, array $args) : Response
{
$body = new ProfileCreateRequest($request->getBody());
$requestDto = new ProfileCreateRequest($request);
$this->profileService->createUserProfileByRequestDto($requestDto);
return $this->respondWithData($response);
}
}
25 changes: 23 additions & 2 deletions src/Domain/Profile/models/ProfileCreateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,35 @@

namespace App\Domain\Profile\models;

use App\Domain\Common\models\AuthorizationRequestTrait;
use App\Domain\Common\models\BaseDto;
use Psr\Http\Message\RequestInterface;

class ProfileCreateRequest extends BaseDto
{
private ?string $nickname;
private ?string $token;
use AuthorizationRequestTrait;

public function __construct($params = null)
public function __construct(RequestInterface $request)
{
parent::__construct($params);
parent::__construct($request->getBody());
$this->token = $this->extractToken($request);
}

public function getNickname() : ?string {
return $this->nickname;
}

public function setNickname(string $nickname) : void {
$this->nickname = $nickname;
}

public function getToken() : ?string {
return $this->token;
}

public function setToken(?string $token) : void {
$this->token = $token;
}
}
2 changes: 1 addition & 1 deletion src/Domain/Profile/service/ProfileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public function createUserProfile(int $userIdx, string $nickName);

public function checkNickNameDuplicate(string $nickName): bool;

public function createUserProfileByToken(string $token, ProfileCreateRequest $requestBody): ?Profile;
public function createUserProfileByRequestDto(ProfileCreateRequest $requestBody): ?Profile;
}
4 changes: 2 additions & 2 deletions src/Domain/Profile/service/ProfileServiceImplement.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public function checkNickNameDuplicate(string $nickName): bool
return false;
}

public function createUserProfileByToken(string $token, ProfileCreateRequest $requestBody): ?Profile
public function createUserProfileByRequestDto(ProfileCreateRequest $requestBody): ?Profile
{
// TODO: Implement createUserProfileByToken() method.
$claims = $this->jwtHandler->decodeJwt($token);
$claims = $this->jwtHandler->decodeJwt($requestBody->getToken());
$userId = $this->jwtHandler->getUserIdFromClaims($claims);

// $user = $this->userService
Expand Down

0 comments on commit 2166b97

Please sign in to comment.