Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jcergolj committed Sep 17, 2020
1 parent 7f99d2b commit 18e3a76
Show file tree
Hide file tree
Showing 23 changed files with 276 additions and 353 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ jobs:
- name: Run insights
run: php artisan insights --no-interaction --min-quality=90 --min-complexity=85 --min-architecture=90 --min-style=95
- name: Run tests
run: ./vendor/bin/paratest --processes 2 --runner=WrapperRunner
run: ./vendor/bin/phpunit
env:
APP_ENV: testing
26 changes: 0 additions & 26 deletions app/Http/Controllers/AcceptedInvitationController.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Http\Request;
use Livewire\Component;

class AcceptedInvitation extends Component
class AcceptedInvitationComponent extends Component
{
use AcceptedInvitationAuth;

Expand All @@ -35,6 +35,17 @@ public function mount(Request $request)
$this->authorizeInvitation($request, $this->user);
}

/**
* Render the component view.
*
* @return \Illuminate\View\View
*/
public function render()
{
return view('accepted-invitations.create')
->extends('layouts.guest-app');
}

/**
* Submit the form.
*
Expand All @@ -49,14 +60,4 @@ public function submit()

return redirect()->to('/home');
}

/**
* Render the component view.
*
* @return \Illuminate\View\View
*/
public function render()
{
return view('livewire.accepted-invitation');
}
}
43 changes: 12 additions & 31 deletions app/Http/Livewire/CreateRoleComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,12 @@ class CreateRoleComponent extends Component
{
use LivewireAuth;

/** @var string */
public $email;

/** @var string */
public $roleId;

/** @var \Illuminate\Database\Eloquent\Collection */
public $roles;

/** @var string */
public $routeName = 'roles.create';

/** @var \App\Models\Role */
public $role;

/** @var string */
public $action;

/** @var string */
public $name;

/** @var string */
public $label;

/** @var \Illuminate\Database\Eloquent\Collection */
public $permissions;

Expand All @@ -59,8 +41,7 @@ public function render()
{
return view('roles.create', [
'permissionGroups' => SaveRoleViewModel::groupPermissions($this->permissions),
])
->extends('layouts.app');
])->extends('layouts.app');
}

/**
Expand All @@ -70,11 +51,11 @@ public function render()
*/
public function store()
{
$this->runValidation();
$this->validate(null, [], $this->attributes());

$role = Role::create([
'name' => $this->name,
'label' => $this->label,
'name' => $this->role['name'],
'label' => $this->role['label'],
]);

$role->createPermissions($this->permissions);
Expand All @@ -85,18 +66,18 @@ public function store()
}

/**
* Run validation.
* Validation rules.
*
* @return \Illuminate\Http\Response
* @return array
*/
private function runValidation()
protected function rules()
{
$this->validate([
'name' => [
return [
'role.name' => [
'required',
Rule::unique('roles')->ignore($this->role->id ?? ''),
Rule::unique('roles', 'name'),
],
'label' => [
'role.label' => [
'required',
],
'permissions.*.allowed' => [
Expand All @@ -107,7 +88,7 @@ private function runValidation()
'boolean',
new OwnerRestrictedRule($this->permissions),
],
], [], $this->attributes());
];
}

/**
Expand Down
45 changes: 25 additions & 20 deletions app/Http/Livewire/CreateUserComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,22 @@
use App\Models\User;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Mail;
use Illuminate\Validation\Rule;
use Livewire\Component;

class CreateUserComponent extends Component
{
use LivewireAuth;

/** @var string */
public $email;

/** @var string */
public $roleId;
/** @var \App\Models\User */
public $user;

/** @var \Illuminate\Database\Eloquent\Collection */
public $roles;

/** @var string */
public $routeName = 'users.create';

/** @var array */
protected $rules = [
'email' => [
'required',
'email',
'unique:users',
],
'roleId' => [
'required',
'exists:roles,id',
],
];

/**
* Render the component view.
*
Expand All @@ -61,8 +46,8 @@ public function store()
$this->validate();

$user = User::create([
'email' => $this->email,
'role_id' => $this->roleId,
'email' => $this->user['email'],
'role_id' => $this->user['role_id'],
]);

msg_success('User has been successfully created.');
Expand All @@ -72,4 +57,24 @@ public function store()

return redirect()->route('users.index');
}

/**
* Validation rules.
*
* @return array
*/
protected function rules()
{
return [
'user.email' => [
'required',
'email',
Rule::unique('users', 'email'),
],
'user.role_id' => [
'required',
Rule::exists('roles', 'id'),
],
];
}
}
54 changes: 31 additions & 23 deletions app/Http/Livewire/EditRoleComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@ class EditRoleComponent extends Component
use LivewireAuth;

/** @var \App\Models\Role */
public $role;
public Role $role;

/** @var string */
public $routeName = 'roles.edit';

/** @var string */
public $name;

/** @var string */
public $label;

/** @var \Illuminate\Database\Eloquent\Collection */
public $permissions;

Expand All @@ -37,10 +31,6 @@ class EditRoleComponent extends Component
public function mount(Role $role)
{
$this->permissions = SaveRoleViewModel::buildRolePermissions($role->id);

$this->role = $role;
$this->name = $role->name;
$this->label = $role->label;
}

/**
Expand All @@ -63,11 +53,11 @@ public function render()
*/
public function update()
{
$this->runValidation();
$this->validate($this->validationRules(), [], $this->attributes());

$this->role->update([
'name' => $this->name,
'label' => $this->label,
'name' => $this->role->name,
'label' => $this->role->label,
]);

if (! $this->role->isAdmin()) {
Expand All @@ -80,18 +70,19 @@ public function update()
}

/**
* Run validation.
* Validation rules.
*
* @return \Illuminate\Http\Response
* @return array
*/
private function runValidation()
protected function validationRules()
{
$this->validate([
'name' => [
return [
'role.name' => [
'required',
Rule::unique('roles')->ignore($this->role->id ?? ''),
Rule::unique('roles', 'name')->ignore($this->role->id),
'unique:roles,id,'.$this->role->id,
],
'label' => [
'role.label' => [
'required',
],
'permissions.*.allowed' => [
Expand All @@ -102,15 +93,32 @@ private function runValidation()
'boolean',
new OwnerRestrictedRule($this->permissions),
],
], [], $this->attributes());
];
}

/**
* Validation rules.
*
* @return array
*/
protected function rules()
{
return [
'role.name' => [
'required',
],
'role.label' => [
'required',
],
];
}

/**
* Rename attributes.
*
* @return array
*/
private function attributes()
protected function attributes()
{
$attributes = [];
$iteration = 1;
Expand Down
Loading

0 comments on commit 18e3a76

Please sign in to comment.