Skip to content

Commit

Permalink
refactor(global): Adding Larastan + refactor user module using SOLID
Browse files Browse the repository at this point in the history
- Fix some phpstan issues
- Fix user save
  • Loading branch information
gnovaro committed Mar 18, 2024
1 parent 239cd41 commit ace9b76
Show file tree
Hide file tree
Showing 18 changed files with 413 additions and 121 deletions.
2 changes: 1 addition & 1 deletion app/Helpers/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class File
/**
* Return human formated file size
*
* @param int $size bytes
* @param int $size bytes
* @return string formated size for humans
*/
public static function formatSize($size)
Expand Down
5 changes: 3 additions & 2 deletions app/Helpers/Linux.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Linux
public static function getUptime(): string
{
$str = @file_get_contents('/proc/uptime');
$num = floatval($str);
$num = intval($str);
$secs = fmod($num, 60);
$num = intdiv($num, 60);
$mins = $num % 60;
Expand All @@ -36,7 +36,7 @@ public static function getUptime(): string
/**
* Add Linux user to the system
*
* @param string $username linux username
* @param string $username linux username
*/
public static function userAdd(string $username = ''): string
{
Expand All @@ -54,6 +54,7 @@ public static function userAdd(string $username = ''): string

return $output;
}

return 'ERROR: username can\'t be empty';
}
}
19 changes: 19 additions & 0 deletions app/Http/Controllers/User/UserCreateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\User;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\View\View;

class UserCreateController
{
public function create(Request $request): View
{
$data['user'] = new User();

return view('user.user', $data);
}
}
17 changes: 17 additions & 0 deletions app/Http/Controllers/User/UserIndexController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Http\Controllers\User;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\View\View;

class UserIndexController
{
public function index(Request $request): View
{
$data['users'] = User::all();

return view('user.index', $data);
}
}
32 changes: 32 additions & 0 deletions app/Http/Controllers/User/UserSaveController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\User;

use App\Http\Controllers\MainController;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class UserSaveController extends MainController
{
public function save(Request $request)
{
if (empty($request->id)) {
$user = new User();
$user->created_at = now();
} else {
$user = User::find($request->id);
}
$user->name = $request->name;
$user->email = $request->email;
if (! empty($request->password)) {
$user->password = Hash::make($request->password);
}
$user->updated_at = now();
$user->save();

return redirect('/user');
}
}
16 changes: 16 additions & 0 deletions app/Http/Controllers/User/UserUpdateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Http\Controllers\User;

use App\Models\User;
use Illuminate\Http\Request;

class UserUpdateController
{
public function update(Request $request, int $id)
{
$data['user'] = User::find($id);

return view('user.user', $data);
}
}
47 changes: 0 additions & 47 deletions app/Http/Controllers/UserController.php

This file was deleted.

6 changes: 3 additions & 3 deletions app/Http/Middleware/TrustHosts.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use Illuminate\Http\Middleware\TrustHosts as Middleware;
Expand All @@ -8,10 +10,8 @@ class TrustHosts extends Middleware
{
/**
* Get the host patterns that should be trusted.
*
* @return array
*/
public function hosts()
public function hosts(): array
{
return [
$this->allSubdomainsOfApplicationUrl(),
Expand Down
17 changes: 2 additions & 15 deletions app/Models/User.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -14,32 +16,17 @@ class User extends Authenticatable

protected $table = 'user';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
];

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"require-dev": {
"fakerphp/faker": "^1.23",
"larastan/larastan": "^2.0",
"laravel/pint": "^1.13",
"laravel/sail": "^1.26",
"mockery/mockery": "^1.6",
Expand Down
Loading

0 comments on commit ace9b76

Please sign in to comment.