-
Notifications
You must be signed in to change notification settings - Fork 2
/
ApprovalHandler.php
68 lines (58 loc) · 1.82 KB
/
ApprovalHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace App\Actions\Log;
use App\Models\Company;
use Illuminate\Support\Facades\Auth;
use Spatie\Permission\Models\Role;
class ApprovalHandler
{
/**
* Handles the approval or rejection of the entities
* s
* @param $entity
* @param $isApproved
* @param $field
* @return void
* @throws \ReflectionException
*/
public function execute($entity, $isApproved, $field = 'is_approved')
{
$status = $isApproved ? 'approved' : 'rejected';
$entity->disableLogging();
$logMessage = '';
if ($field != 'is_approved') {
$logMessage = $entity->name . "'s sponsorship has been {$status} by " . Auth::user()->name;
} else {
$entityType = (new \ReflectionClass($entity))->getShortName();
$entityName = $entity->name ?? $entity->company->name;
$logMessage = "{$entityType} {$entityName} has been {$status} by " . Auth::user()->name;
}
activity()
->causedBy(Auth::user())
->performedOn($entity)
->event($status)
->log($logMessage);
if ($isApproved) {
$entity->$field = true;
$entity->save();
} elseif ($field == 'is_approved') {
$entity->delete();
if ($entity instanceof Company) {
$this->handleAdditionalCompanyRejection($entity);
}
}
$entity->enableLogging();
}
/**
* Helper method taking care of the additional company rejection
*
* @param $company
* @return void
*/
private function handleAdditionalCompanyRejection($company)
{
$participantRole = Role::findByName('participant', 'web');
foreach ($company->users as $user) {
$user->syncRoles($participantRole);
}
}
}