Skip to content

Commit

Permalink
Improve make action console command
Browse files Browse the repository at this point in the history
Improve code readability
  • Loading branch information
eliseekn committed Feb 23, 2023
1 parent ed23d38 commit b4ab072
Show file tree
Hide file tree
Showing 18 changed files with 227 additions and 130 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@
/vendor/
.env
.phpunit.result.cache
TODO
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM php:7.4-fpm
FROM php:8.0-fpm

RUN apt update -y && apt upgrade -y
RUN apt install -y git curl libpng-dev libonig-dev libxml2-dev zip unzip
Expand Down
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
test:
php console app:env test
php console db:delete
php console db:create
php console migrations:run
php console test

docker:
Expand Down
21 changes: 21 additions & 0 deletions app/Http/Actions/User/StoreAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* @copyright (2019 - 2022) - N'Guessan Kouadio Elisée ([email protected])
* @license MIT (https://opensource.org/licenses/MIT)
* @link https://github.com/eliseekn/tinymvc
*/

namespace App\Http\Actions\User;

use App\Database\Models\User;

class StoreAction
{
public function handle(array $data)
{
$data['password'] = hash_pwd($data['password']);

return User::create($data);
}
}
30 changes: 30 additions & 0 deletions app/Http/Actions/User/UpdateAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* @copyright (2019 - 2022) - N'Guessan Kouadio Elisée ([email protected])
* @license MIT (https://opensource.org/licenses/MIT)
* @link https://github.com/eliseekn/tinymvc
*/

namespace App\Http\Actions\User;

use App\Database\Models\User;

class UpdateAction
{
public function handle(array $data, string $email)
{
$user = User::findBy('email', $email);

if ($user === false) {
return false;
}

if (isset($data['password'])) {
$data['password'] = hash_pwd($data['password']);
}

$user->fill($data);
return $user->save();
}
}
6 changes: 3 additions & 3 deletions app/Http/Controllers/Auth/EmailVerificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use App\Database\Models\Token;
use App\Mails\VerificationMail;
use Core\Http\Response;
use App\Http\Actions\UserActions;
use App\Http\Actions\User\UpdateAction;

/**
* Manage email verification link
Expand All @@ -42,7 +42,7 @@ public function notify(Request $request, Response $response)
$response->redirectUrl('signup')->send(302);
}

public function verify(Request $request, Response $response)
public function verify(Request $request, Response $response, UpdateAction $updateAction)
{
if (!$request->hasQuery('email', 'token')) {
$response->data(__('bad_request'))->send(400);
Expand All @@ -60,7 +60,7 @@ public function verify(Request $request, Response $response)

$token->delete();

$user = UserActions::update(['email_verified' => Carbon::now()->toDateTimeString()], $request->queries('email'));
$user = $updateAction->handle(['email_verified' => Carbon::now()->toDateTimeString()], $request->queries('email'));

if (!$user) {
Alert::default(__('account_not_found'))->error();
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Auth/ForgotPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Core\Support\Mail\Mail;
use App\Database\Models\Token;
use Core\Http\Response;
use App\Http\Actions\UserActions;
use App\Http\Actions\User\UpdateAction;
use App\Http\Validators\Auth\LoginValidator;

/**
Expand Down Expand Up @@ -62,10 +62,10 @@ public function reset(Request $request, Response $response)
$response->redirectUrl("/password/new?email={$request->email}")->send(302);
}

public function update(Request $request, Response $response, LoginValidator $loginValidator)
public function update(Request $request, Response $response, LoginValidator $loginValidator, UpdateAction $updateAction)
{
$loginValidator->validate($request->inputs(), $response);
$user = UserActions::updatPassword($request->password, $request->email);
$user = $updateAction->handle(['password' => $request->password], $request->email);

if (!$user) {
Alert::default(__('password_not_reset'))->error();
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use App\Mails\WelcomeMail;
use Core\Support\Mail\Mail;
use Core\Http\Response;
use App\Http\Actions\UserActions;
use App\Http\Actions\User\StoreAction;
use App\Http\Validators\Auth\RegisterValidator;

class RegisterController
Expand All @@ -28,10 +28,10 @@ public function index(Request $request, Response $response)
$response->redirectUrl($uri)->send(302);
}

public function register(Request $request, Response $response, RegisterValidator $registerValidator)
public function register(Request $request, Response $response, RegisterValidator $registerValidator, StoreAction $storeAction)
{
$validator = $registerValidator->validate($request->inputs(), $response);
$user = UserActions::create($validator->validated());
$user = $storeAction->handle($validator->validated());

if (config('security.auth.email_verification')) {
$response->redirectUrl('/email/notify')->send(302);
Expand Down
29 changes: 19 additions & 10 deletions core/Console/Make/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,36 @@
*/
class Actions extends Command
{
protected static $defaultName = 'make:actions';
protected static $defaultName = 'make:action';

protected function configure()
{
$this->setDescription('Create new actions');
$this->addArgument('model', InputArgument::REQUIRED|InputArgument::IS_ARRAY, 'The name of model (separated by space if many)');
$this->setDescription('Create new action');
$this->addArgument('model', InputArgument::REQUIRED, 'The name of model');
$this->addOption('type', null, InputOption::VALUE_OPTIONAL|InputOption::VALUE_IS_ARRAY, 'Specify action type (index, show, store, update or destroy)');
$this->addOption('namespace', null, InputOption::VALUE_OPTIONAL, 'Specify namespace (base: App\Http\Actions)');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$models = $input->getArgument('model');
$types = $input->getOption('type');

foreach ($models as $model) {
list(, $class) = Make::generateClass($model, 'actions', true, true);
if (empty($types)) {
$types = ['index', 'show', 'store', 'update', 'destroy'];
}

if (!Make::createActions($model, $input->getOption('namespace'))) {
$output->writeln('<fg=yellow>Failed to create actions "' . $class . '"</fg>');
}
$types = array_map(fn ($type) => strtolower($type), $types);

foreach ($types as $type) {
list(, $class) = Make::generateClass($type, 'action', true, true);

$output->writeln('<info>Actions "' . $class . '" has been created</info>');
$class = str_replace(['Index', 'Show'], ['GetCollection', 'GetItem'], $class);

if (!Make::createAction($input->getArgument('model'), $type, $input->getOption('namespace'))) {
$output->writeln('<fg=yellow>Failed to create action "' . $class . '"</fg>');
} else {
$output->writeln('<info>Action "' . $class . '" has been created</info>');
}
}

return Command::SUCCESS;
Expand Down
Loading

0 comments on commit b4ab072

Please sign in to comment.