-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathCreateContact.php
More file actions
114 lines (96 loc) · 3.47 KB
/
Copy pathCreateContact.php
File metadata and controls
114 lines (96 loc) · 3.47 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace App\Actions\Contacts;
use App\Actions\Utilities\FormatPhoneForE164;
use App\Enums\Contactable;
use App\Http\Resources\ContactResource;
use App\Models\Contact;
use App\Rules\ValidPhoneNumber;
use Illuminate\Database\Eloquent\Model;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class CreateContact
{
use AsAction;
public function handle(
string $name,
string $contact_type,
?string $title,
?string $email,
?string $mobile_phone,
?string $office_phone,
?string $office_phone_extension,
Model $contactFor,
): Contact
{
return Contact::create([
'contact_type' => $contact_type,
'title' => $title,
'name' => $name,
'email' => $email,
'mobile_phone' => $mobile_phone,
'office_phone' => $office_phone,
'office_phone_extension' => $office_phone_extension,
'contact_for_id' => $contactFor->getKey(),
'contact_for_type' => $contactFor->getMorphClass(),
]);
}
public function asController(ActionRequest $request): Contact
{
$contactForClass = Contactable::from(
$request->validated('contact_for_type')
)->getClassName();
$contactFor = $contactForClass::find($request->validated('contact_for_id'));
$mobilePhone = $request->validated('mobile_phone');
$officePhone = $request->validated('office_phone');
if ($mobilePhone) {
$mobilePhone = FormatPhoneForE164::handle($mobilePhone);
}
if ($officePhone) {
$officePhone = FormatPhoneForE164::handle($officePhone);
}
$contact = $this->handle(
name: $request->validated('name'),
contact_type: $request->validated('contact_type'),
title: $request->validated('title'),
email: $request->validated('email'),
mobile_phone: $mobilePhone,
office_phone: $officePhone,
office_phone_extension: $request->validated('office_phone_extension'),
contactFor: $contactFor,
);
return $contact;
}
public function jsonResponse(Contact $contact)
{
return ContactResource::make($contact);
}
public function htmlResponse(Contact $contact)
{
return redirect()->back()->with('success', 'Contact added successfully');
}
public function rules(): array
{
return [
'contact_type' => ['required', 'string'],
'title' => ['nullable', 'string', 'min:3', 'max:255'],
'name' => ['required', 'string', 'min:3', 'max:255'],
'email' => ['nullable', 'email', 'max:255'],
'mobile_phone' => ['nullable', 'string', 'max:255', new ValidPhoneNumber],
'office_phone' => ['nullable', 'string', 'max:255', new ValidPhoneNumber],
'office_phone_extension' => ['nullable', 'string', 'max:255'],
'contact_for_id' => ['required'],
'contact_for_type' => ['required', 'string'],
];
}
public function authorize(ActionRequest $request): bool
{
$contactForClass = Contactable::from(
$request->input('contact_for_type')
)->getClassName();
$contactFor = $contactForClass::find($request->input('contact_for_id'));
if (!$contactFor) {
return false;
}
return $request->user()->can('update', $contactFor);
}
}