Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
76.36% |
42 / 55 |
|
42.86% |
3 / 7 |
CRAP | |
0.00% |
0 / 1 |
| InfluencerController | |
76.36% |
42 / 55 |
|
42.86% |
3 / 7 |
8.85 | |
0.00% |
0 / 1 |
| index | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| create | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| store | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
1 | |||
| edit | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| update | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| destroy | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| validated | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\Admin; |
| 4 | |
| 5 | use App\Enums\Status; |
| 6 | use App\Enums\UserRole; |
| 7 | use App\Http\Controllers\Concerns\ResolvesContext; |
| 8 | use App\Http\Controllers\Controller; |
| 9 | use App\Models\Influencer; |
| 10 | use App\Models\User; |
| 11 | use Illuminate\Http\RedirectResponse; |
| 12 | use Illuminate\Http\Request; |
| 13 | use Illuminate\Support\Facades\DB; |
| 14 | use Illuminate\Validation\Rule; |
| 15 | use Illuminate\View\View; |
| 16 | |
| 17 | class InfluencerController extends Controller |
| 18 | { |
| 19 | use ResolvesContext; |
| 20 | |
| 21 | public function index(): View |
| 22 | { |
| 23 | return view('admin.influencers.index'); |
| 24 | } |
| 25 | |
| 26 | public function create(): View |
| 27 | { |
| 28 | return view('admin.influencers.form', ['influencer' => new Influencer()]); |
| 29 | } |
| 30 | |
| 31 | public function store(Request $request): RedirectResponse |
| 32 | { |
| 33 | $store = $this->currentStore(); |
| 34 | $data = $this->validated($request); |
| 35 | |
| 36 | DB::transaction(function () use ($data, $store) { |
| 37 | $user = User::create([ |
| 38 | 'tenant_id' => $store->tenant_id, |
| 39 | 'store_id' => $store->id, |
| 40 | 'name' => $data['name'], |
| 41 | 'email' => $data['email'], |
| 42 | 'password' => $data['password'], |
| 43 | 'role' => UserRole::Influencer, |
| 44 | 'status' => Status::Active, |
| 45 | ]); |
| 46 | |
| 47 | Influencer::create([ |
| 48 | 'tenant_id' => $store->tenant_id, |
| 49 | 'store_id' => $store->id, |
| 50 | 'user_id' => $user->id, |
| 51 | 'name' => $data['name'], |
| 52 | 'email' => $data['email'], |
| 53 | 'instagram' => $data['instagram'] ?? null, |
| 54 | 'tiktok' => $data['tiktok'] ?? null, |
| 55 | 'cpf_cnpj' => $data['cpf_cnpj'] ?? null, |
| 56 | 'pix_key' => $data['pix_key'] ?? null, |
| 57 | 'base_commission_percentage' => $data['base_commission_percentage'], |
| 58 | 'status' => Status::Active, |
| 59 | 'internal_notes' => $data['internal_notes'] ?? null, |
| 60 | ]); |
| 61 | }); |
| 62 | |
| 63 | return redirect()->route('admin.influencers.index')->with('success', 'Influenciador criado com sucesso.'); |
| 64 | } |
| 65 | |
| 66 | public function edit(Influencer $influencer): View |
| 67 | { |
| 68 | return view('admin.influencers.form', compact('influencer')); |
| 69 | } |
| 70 | |
| 71 | public function update(Request $request, Influencer $influencer): RedirectResponse |
| 72 | { |
| 73 | $data = $this->validated($request, $influencer); |
| 74 | |
| 75 | DB::transaction(function () use ($data, $influencer) { |
| 76 | $influencer->update(collect($data)->except('password')->toArray()); |
| 77 | $influencer->user->update(array_filter([ |
| 78 | 'name' => $data['name'], |
| 79 | 'email' => $data['email'], |
| 80 | 'password' => $data['password'] ?? null, |
| 81 | ])); |
| 82 | }); |
| 83 | |
| 84 | return redirect()->route('admin.influencers.index')->with('success', 'Influenciador atualizado.'); |
| 85 | } |
| 86 | |
| 87 | public function destroy(Influencer $influencer): RedirectResponse |
| 88 | { |
| 89 | $influencer->update(['status' => Status::Inactive]); |
| 90 | $influencer->user->update(['status' => Status::Inactive]); |
| 91 | |
| 92 | return back()->with('success', 'Influenciador inativado.'); |
| 93 | } |
| 94 | |
| 95 | private function validated(Request $request, ?Influencer $influencer = null): array |
| 96 | { |
| 97 | return $request->validate([ |
| 98 | 'name' => ['required', 'string', 'max:255'], |
| 99 | 'email' => ['required', 'email', 'max:255', Rule::unique('users', 'email')->ignore($influencer?->user_id)], |
| 100 | 'password' => [$influencer ? 'nullable' : 'required', 'string', 'min:8'], |
| 101 | 'instagram' => ['nullable', 'string', 'max:255'], |
| 102 | 'tiktok' => ['nullable', 'string', 'max:255'], |
| 103 | 'cpf_cnpj' => ['nullable', 'string', 'max:30'], |
| 104 | 'pix_key' => ['nullable', 'string', 'max:255'], |
| 105 | 'base_commission_percentage' => ['required', 'numeric', 'min:0', 'max:100'], |
| 106 | 'internal_notes' => ['nullable', 'string'], |
| 107 | ]); |
| 108 | } |
| 109 | } |