-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from feat/base-boilerplate
Add base boilerplate
- Loading branch information
Showing
116 changed files
with
7,290 additions
and
971 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace App\Core\Date; | ||
|
||
enum DatetimeFormat: string | ||
{ | ||
case ISO_WITH_MILLIS = 'Y-m-d\TH:i:s.vp'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace App\Core\Formatter; | ||
|
||
enum ExceptionErrorCode: string | ||
{ | ||
case GENERIC = 'GENERIC'; | ||
case INVALID_VALIDATION = 'INVALID-STRUCTURE-VALIDATION'; | ||
case MODEL_NOT_FOUND = 'MODEL-NOT-FOUND'; | ||
case REQUIRE_AUTHORIZATION = 'REQUIRE-AUTH'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace App\Core\Formatter\ExceptionMessage; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
interface ExceptionMessage | ||
{ | ||
public function getJsonResponse(): Collection; | ||
public function getMessage(): string; | ||
} |
29 changes: 29 additions & 0 deletions
29
app/Core/Formatter/ExceptionMessage/ExceptionMessageGeneric.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace App\Core\Formatter\ExceptionMessage; | ||
|
||
use App\Core\Formatter\ExceptionErrorCode; | ||
use Illuminate\Support\Collection; | ||
|
||
class ExceptionMessageGeneric implements ExceptionMessage | ||
{ | ||
protected ExceptionMessage $exceptionMessage; | ||
|
||
public function __construct() | ||
{ | ||
$this->exceptionMessage = new ExceptionMessageStandard( | ||
'Something Wrong on Our Server', | ||
ExceptionErrorCode::GENERIC->value | ||
); | ||
} | ||
|
||
public function getJsonResponse(): Collection | ||
{ | ||
return $this->exceptionMessage->getJsonResponse(); | ||
} | ||
|
||
public function getMessage(): string | ||
{ | ||
return $this->exceptionMessage->getMessage(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
app/Core/Formatter/ExceptionMessage/ExceptionMessageStandard.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace App\Core\Formatter\ExceptionMessage; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
class ExceptionMessageStandard implements ExceptionMessage | ||
{ | ||
public function __construct( | ||
protected string $errorMessage, | ||
protected string $errorCode, | ||
protected ?array $meta = [], | ||
) { | ||
} | ||
|
||
public function getJsonResponse(): Collection | ||
{ | ||
return collect([ | ||
'message' => $this->errorMessage, | ||
'errorCode' => $this->errorCode, | ||
'meta' => $this->meta, | ||
]); | ||
} | ||
|
||
public function getMessage(): string | ||
{ | ||
return $this->errorMessage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace App\Core\Formatter\Randomizer; | ||
|
||
interface Randomizer | ||
{ | ||
public function getRandomizeString(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace App\Core\Formatter\Randomizer; | ||
|
||
use Illuminate\Support\Str; | ||
|
||
class RandomizerUUID implements Randomizer | ||
{ | ||
public function getRandomizeString(): string | ||
{ | ||
return Str::uuid(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace App\Core\Logger\MessageFormatter; | ||
|
||
interface LoggerMessageFormatter | ||
{ | ||
public function getMessage(): string; | ||
} |
22 changes: 22 additions & 0 deletions
22
app/Core/Logger/MessageFormatter/LoggerMessageFormatterFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace App\Core\Logger\MessageFormatter; | ||
|
||
class LoggerMessageFormatterFactory implements LoggerMessageFormatterFactoryContract | ||
{ | ||
public function makeGeneric( | ||
string $endpoint, | ||
string $requestID, | ||
ProcessingStatus $processingStatus, | ||
string $message, | ||
array $meta, | ||
): LoggerMessageFormatter { | ||
return new LoggerMessageFormatterGeneric( | ||
$endpoint, | ||
$requestID, | ||
$processingStatus, | ||
$message, | ||
$meta, | ||
); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
app/Core/Logger/MessageFormatter/LoggerMessageFormatterFactoryContract.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\Core\Logger\MessageFormatter; | ||
|
||
interface LoggerMessageFormatterFactoryContract | ||
{ | ||
public function makeGeneric( | ||
string $endpoint, | ||
string $requestID, | ||
ProcessingStatus $processingStatus, | ||
string $message, | ||
array $meta, | ||
): LoggerMessageFormatter; | ||
} |
27 changes: 27 additions & 0 deletions
27
app/Core/Logger/MessageFormatter/LoggerMessageFormatterGeneric.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace App\Core\Logger\MessageFormatter; | ||
|
||
class LoggerMessageFormatterGeneric implements LoggerMessageFormatter | ||
{ | ||
public function __construct( | ||
protected string $endpoint, | ||
protected string $requestID, | ||
protected ProcessingStatus $processingStatus, | ||
protected string $message, | ||
protected array $meta, | ||
) { | ||
$this->meta = $meta; | ||
} | ||
|
||
public function getMessage(): string | ||
{ | ||
return json_encode([ | ||
'endpoint' => $this->endpoint, | ||
'request-id' => $this->requestID, | ||
'processing-status' => $this->processingStatus->value, | ||
'message' => $this->message, | ||
'meta' => $this->meta, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace App\Core\Logger\MessageFormatter; | ||
|
||
enum ProcessingStatus: string | ||
{ | ||
case BEGIN = "BEGIN"; | ||
case ERROR = "ERROR"; | ||
case PROCESSING = "PROCESSING"; | ||
case SUCCESS = "SUCCESS"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace App\Core\Query; | ||
|
||
enum OrderDirection: string | ||
{ | ||
case ASCENDING = 'asc'; | ||
case DESCENDING = 'desc'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\Core\User\Query; | ||
|
||
enum UserOrderBy: string | ||
{ | ||
case NAME = 'name'; | ||
case EMAIL = 'email'; | ||
case CREATED_AT = 'created_at'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace App\Core\User; | ||
|
||
use App\Core\Formatter\ExceptionMessage\ExceptionMessageStandard; | ||
use App\Core\Query\OrderDirection; | ||
use App\Core\User\Query\UserOrderBy; | ||
use App\Exceptions\Core\User\UserEmailDuplicatedException; | ||
use App\Models\User\Enum\UserExceptionCode; | ||
use App\Models\User\User; | ||
use App\Port\Core\User\CreateUserPort; | ||
use App\Port\Core\User\DeleteUserPort; | ||
use App\Port\Core\User\GetAllUserPort; | ||
use App\Port\Core\User\GetUserPort; | ||
use App\Port\Core\User\UpdateUserPort; | ||
use Illuminate\Contracts\Pagination\LengthAwarePaginator; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Hash; | ||
|
||
class UserCore implements UserCoreContract | ||
{ | ||
public function create(CreateUserPort $request): User | ||
{ | ||
try { | ||
DB::beginTransaction(); | ||
|
||
$isEmailExist = User::query() | ||
->where('email', $request->getEmail()) | ||
->exists(); | ||
if ($isEmailExist) { | ||
throw new UserEmailDuplicatedException(new ExceptionMessageStandard( | ||
'Email is duplicated', | ||
UserExceptionCode::DUPLICATED->value, | ||
)); | ||
} | ||
|
||
$user = new User; | ||
$user->name = $request->getName(); | ||
$user->email = $request->getEmail(); | ||
$user->password = Hash::make($request->getUserPassword()); | ||
$user->save(); | ||
|
||
DB::commit(); | ||
|
||
return $user; | ||
} catch (\Exception $e) { | ||
DB::rollBack(); | ||
throw $e; | ||
} | ||
} | ||
|
||
public function delete(DeleteUserPort $request): void | ||
{ | ||
$user = $request->getUserModel(); | ||
$user->delete(); | ||
} | ||
|
||
public function get(GetUserPort $request): User | ||
{ | ||
return $request->getUserModel(); | ||
} | ||
|
||
public function getAll(GetAllUserPort $request): LengthAwarePaginator | ||
{ | ||
$page = $request->getPage() ?? 1; | ||
$perPage = $request->getPerPage() ?? 30; | ||
$orderDirection = $request->getOrderDirection() ?? OrderDirection::DESCENDING; | ||
$orderBy = $request->getOrderBy() ?? UserOrderBy::CREATED_AT; | ||
|
||
return User::query() | ||
->orderBy($orderBy->value, $orderDirection->value) | ||
->paginate($perPage, ['*'], 'page', $page); | ||
} | ||
|
||
public function update(UpdateUserPort $request): User | ||
{ | ||
try { | ||
DB::beginTransaction(); | ||
|
||
$user = $request->getUserModel(); | ||
$email = $request->getEmail(); | ||
|
||
$isEmailExist = User::query() | ||
->where('email', $email) | ||
->where('id', '<>', $user->id) | ||
->exists(); | ||
if ($isEmailExist) { | ||
throw new UserEmailDuplicatedException(new ExceptionMessageStandard( | ||
'Email is already in used', | ||
UserExceptionCode::DUPLICATED->value, | ||
)); | ||
} | ||
|
||
$user->name = $request->getName(); | ||
$user->email = $email; | ||
$user->password = Hash::make($request->getUserPassword()); | ||
$user->save(); | ||
|
||
DB::commit(); | ||
|
||
return $user; | ||
} catch (\Exception $e) { | ||
DB::rollBack(); | ||
throw $e; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App\Core\User; | ||
|
||
use App\Models\User\User; | ||
use App\Port\Core\User\CreateUserPort; | ||
use App\Port\Core\User\DeleteUserPort; | ||
use App\Port\Core\User\GetAllUserPort; | ||
use App\Port\Core\User\GetUserPort; | ||
use App\Port\Core\User\UpdateUserPort; | ||
use Illuminate\Contracts\Pagination\LengthAwarePaginator; | ||
|
||
interface UserCoreContract | ||
{ | ||
public function create(CreateUserPort $request): User; | ||
public function delete(DeleteUserPort $request): void; | ||
public function get(GetUserPort $request): User; | ||
public function getAll(GetAllUserPort $request): LengthAwarePaginator; | ||
public function update(UpdateUserPort $request): User; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App\Exceptions; | ||
|
||
use App\Core\Formatter\ExceptionMessage\ExceptionMessage; | ||
use Exception; | ||
|
||
abstract class BaseException extends Exception | ||
{ | ||
public function __construct( | ||
public readonly ExceptionMessage $exceptionMessage, | ||
?\Throwable $previousException = null | ||
) { | ||
parent::__construct( | ||
$this->exceptionMessage->getJsonResponse()->toJson(), | ||
0, | ||
$previousException | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace App\Exceptions\Core\User; | ||
|
||
use App\Exceptions\BaseException; | ||
|
||
class UserEmailDuplicatedException extends BaseException | ||
{ | ||
} |
Oops, something went wrong.