Skip to content

Commit

Permalink
Update whole framework architecture
Browse files Browse the repository at this point in the history
Improve code readability
Remove unnecessary code
Add new features
  • Loading branch information
eliseekn committed Aug 13, 2023
1 parent 28c4b19 commit b9f998d
Show file tree
Hide file tree
Showing 48 changed files with 412 additions and 466 deletions.
4 changes: 1 addition & 3 deletions app/Database/Factories/TokenFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@

class TokenFactory extends Factory
{
protected static $model = Token::class;

public function __construct(int $count = 1)
{
parent::__construct($count);
parent::__construct(Token::class, $count);
}

public function data(): array
Expand Down
4 changes: 1 addition & 3 deletions app/Database/Factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@

class UserFactory extends Factory
{
protected static $model = User::class;

public function __construct(int $count = 1)
{
parent::__construct($count);
parent::__construct(User::class, $count);
}

public function data(): array
Expand Down
28 changes: 27 additions & 1 deletion app/Database/Models/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,31 @@

class Token extends Model
{
protected static $table = 'tokens';
public function __construct()
{
parent::__construct('tokens');
}

public static function findByValue(string $value): Model|false
{
return (new self())->findBy('value', $value);
}

public static function exists(string $email, string $description): Model|false
{
$token = (new self())
->where('email', $email)
->and('description', $description);

return !$token->exists() ? false : $token->first();
}

public static function findLatest(string $email, string $description): Model|false
{
return (new self())
->where('email', $email)
->and('description', $description)
->newest()
->first();
}
}
15 changes: 14 additions & 1 deletion app/Database/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,18 @@

class User extends Model
{
protected static $table = 'users';
public function __construct()
{
parent::__construct('users');
}

public static function findByEmail(string $email): Model|false
{
return (new self())->findBy('email', $email);
}

public static function findAllWhereEmailLike(string $email): array|false
{
return (new self())->where('email', 'like', $email)->getAll();
}
}
2 changes: 1 addition & 1 deletion app/Enums/TokenDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
enum TokenDescription: string
{
case PASSWORD_RESET_TOKEN = 'password_reset_token';
case EMAIL_VERIFICATION_TOKEN = 'email_verifications_token';
case EMAIL_VERIFICATION_TOKEN = 'email_verification_token';

public static function values(): array
{
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Actions/User/StoreAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class StoreAction
public function handle(array $data): Model|false
{
$data['password'] = hash_pwd($data['password']);
return User::create($data);
return (new User())->create($data);
}
}
7 changes: 3 additions & 4 deletions app/Http/Actions/User/UpdateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ class UpdateAction
{
public function handle(array $data, string $email): Model|false
{
$user = User::findBy('email', $email);
$user = User::findByEmail($email);

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

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

$user->fill($data);
return $user->save();
return $user->fill($data)->save();
}
}
27 changes: 10 additions & 17 deletions app/Http/Controllers/Auth/EmailVerificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,24 @@ class EmailVerificationController extends Controller
{
public function notify(): void
{
$token = generate_token();
$tokenValue = generate_token();

if (!Mail::send(new VerificationMail($this->request->queries('email'), $token))) {
if (!Mail::send(new VerificationMail($this->request->queries('email'), $tokenValue))) {
Alert::default(__('email_verification_link_not_sent'))->error();
$this->render('auth.signup');
}

if (
!Token::where('email', $this->request->queries('email'))
->and('description', TokenDescription::EMAIL_VERIFICATION_TOKEN->value)
->exists()
) {
Token::create([
$token = Token::exists($this->request->queries('email'), TokenDescription::EMAIL_VERIFICATION_TOKEN->value);

if ($token) {
$token->update(['value' => $tokenValue]);
} else {
$token->fill([
'email'=> $this->request->queries('email'),
'value' => $token,
'expire' => Carbon::now()->addDay()->toDateTimeString(),
'description' => TokenDescription::EMAIL_VERIFICATION_TOKEN->value
]);
} else {
Token::where('email', $this->request->queries('email'))
->and('description', TokenDescription::EMAIL_VERIFICATION_TOKEN->value)
->update(['value' => $token]);
])->save();
}

Alert::default(__('email_verification_link_sent'))->success();
Expand All @@ -59,10 +55,7 @@ public function verify(UpdateAction $action): void
$this->response(__('bad_request'), 400);
}

$token = Token::where('email', $this->request->queries('email'))
->and('description', TokenDescription::EMAIL_VERIFICATION_TOKEN->value)
->newest()
->first();
$token = Token::findLatest($this->request->queries('email'), TokenDescription::EMAIL_VERIFICATION_TOKEN->value);

if (!$token || $token->attribute('value') !== $this->request->queries('token')) {
$this->response(__('invalid_password_reset_link'), 400);
Expand Down
47 changes: 20 additions & 27 deletions app/Http/Controllers/Auth/ForgotPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,27 @@ class ForgotPasswordController extends Controller
{
public function notify(): void
{
$token = generate_token();
$tokenValue = generate_token();

if (Mail::send(new TokenMail($this->request->get('email'), $token))) {
if (
!Token::where('email', $this->request->inputs('email'))
->and('description', TokenDescription::PASSWORD_RESET_TOKEN->value)
->exists()
) {
Token::create([
'email'=> $this->request->inputs('email'),
'value' => $token,
'expire' => Carbon::now()->addHour()->toDateTimeString(),
'description' => TokenDescription::PASSWORD_RESET_TOKEN->value
]);
} else {
Token::where('email', $this->request->inputs('email'))
->and('description', TokenDescription::PASSWORD_RESET_TOKEN->value)
->update(['value' => $token]);
}
if (!Mail::send(new TokenMail($this->request->get('email'), $tokenValue))) {
Alert::default(__('password_reset_link_not_sent'))->error();
$this->redirectBack();
}

Alert::default(__('password_reset_link_sent'))->success();
$this->redirectBack();
}

Alert::default(__('password_reset_link_not_sent'))->error();
$token = Token::exists($this->request->queries('email'), TokenDescription::PASSWORD_RESET_TOKEN->value);

if ($token) {
$token->update(['value' => $tokenValue]);
} else {
$token->fill([
'email'=> $this->request->queries('email'),
'value' => $token,
'expire' => Carbon::now()->addHour()->toDateTimeString(),
'description' => TokenDescription::PASSWORD_RESET_TOKEN->value
])->save();
}

Alert::default(__('password_reset_link_sent'))->success();
$this->redirectBack();
}

Expand All @@ -59,10 +55,7 @@ public function reset(): void
$this->response(__('bad_request'), 400);
}

$token = Token::where('email', $this->request->queries('email'))
->and('description', TokenDescription::PASSWORD_RESET_TOKEN->value)
->newest()
->first();
$token = Token::findLatest($this->request->queries('email'), TokenDescription::PASSWORD_RESET_TOKEN->value);

if (!$token || $token->attribute('value') !== $this->request->queries('token')) {
$this->response(__('invalid_password_reset_link'), 400);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Middlewares/RememberUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RememberUser
public function handle(): void
{
if (Cookies::has('user')) {
$user = User::findBy('email', Cookies::get('user'));
$user = (new User())->findBy('email', Cookies::get('user'));

if ($user !== false) {
Session::create('user', $user);
Expand Down
4 changes: 2 additions & 2 deletions app/Mails/WelcomeMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
*/
class WelcomeMail extends Mailer
{
public function __construct(string $email, string $username)
public function __construct(string $email, string $name)
{
parent::__construct();

$this->to($email)
->from(config('mailer.sender.email'), config('mailer.sender.name'))
->reply(config('mailer.sender.email'), config('mailer.sender.name'))
->subject('Welcome')
->body(View::getContent('emails.welcome', compact('username')));
->body(View::getContent('emails.welcome', compact('name')));
}
}
4 changes: 2 additions & 2 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* Setup application
*/

define('DS', DIRECTORY_SEPARATOR);
define('APP_ROOT', __DIR__ . DS);
const DS = DIRECTORY_SEPARATOR;
const APP_ROOT = __DIR__ . DS;

set_time_limit(0);

Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "eliseekn/tinymvc",
"description": "TinyMVC is a PHP framework based on MVC architecture that helps you build easily and quickly powerful web applications and REST API.",
"description": "TinyMVC is a PHP framework based on MVC architecture that helps you build easily and quickly powerful web applications and RESTful API.",
"type": "project",
"license": "MIT",
"keywords": ["php", "framework", "mvc"],
"keywords": ["php-framework", "mvc"],
"authors": [
{
"name": "N'Guessan Kouadio Elisée",
Expand All @@ -20,7 +20,8 @@
],
"psr-4": {
"App\\" : "app/",
"Core\\" : "core/"
"Core\\" : "core/",
"Tests\\" : "tests/"
}
},
"config": {
Expand Down
5 changes: 2 additions & 3 deletions config/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Console commands
*/

return [
return [
'core' => [
new \Core\Console\Database\Create(),
new \Core\Console\Database\Delete(),
Expand All @@ -28,7 +28,6 @@
new \Core\Console\Make\Validator(),
new \Core\Console\Make\Seed(),
new \Core\Console\Make\Factory(),
new \Core\Console\Make\Repository(),
new \Core\Console\Make\View(),
new \Core\Console\Make\Mail(),
new \Core\Console\Make\Middleware(),
Expand Down Expand Up @@ -56,4 +55,4 @@
'app' => [
//
]
];
];
1 change: 1 addition & 0 deletions config/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'log' => true,

'views' => [
'403' => 'errors' . DS . '403',
'404' => 'errors' . DS . '404',
'500' => 'errors' . DS . '500'
]
Expand Down
2 changes: 1 addition & 1 deletion config/mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

'sender' => [
'name' => config('app.name'),
'email' => '[email protected]',
'email' => '[email protected]',
],

'smtp' => [
Expand Down
2 changes: 1 addition & 1 deletion config/security.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'auth' => [
'max_attempts' => false,
'unlock_timeout' => 1, //in minute
'email_verification' => true,
'email_verification' => false,
],

'session' => [
Expand Down
9 changes: 7 additions & 2 deletions core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ public function run(): void
try {
Router::dispatch(new Request(), $response);
} catch (Exception $e) {
if (config('errors.log')) save_log('Exception: ' . $e);
if (config('errors.display')) die($e);
if (config('errors.log')) {
save_log('Exception: ' . $e);
}

if (config('errors.display')) {
die($e);
}

$response->view(config('errors.views.500'))->send(500);
}
Expand Down
18 changes: 0 additions & 18 deletions core/Console/Make/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,25 +163,7 @@ public static function createFactory(string $factory, ?string $namespace = null)

return $storage->writeFile(self::fixPluralTypo($class, true) . '.php', $data);
}

public static function createRepository(string $repository, ?string $namespace = null): bool
{
list($name, $class) = self::generateClass($repository, 'repository', true, true);

$data = self::stubs()->readFile('Repository.stub');
$data = self::addNamespace($data, 'App\Database\Repositories', $namespace);
$data = str_replace('CLASSNAME', self::fixPluralTypo($class, true), $data);
$data = str_replace('MODELNAME', self::fixPluralTypo(ucfirst($name), true), $data);

$storage = Storage::path(config('storage.repositories'));

if (!is_null($namespace)) {
$storage = $storage->addPath(str_replace('\\', '/', $namespace));
}

return $storage->writeFile(self::fixPluralTypo($class, true) . '.php', $data);
}

public static function createHelper(string $helper): bool
{
list(, $class) = self::generateClass($helper, 'helper', true);
Expand Down
Loading

0 comments on commit b9f998d

Please sign in to comment.