Skip to content

Commit ce242d5

Browse files
committed
Move logic to actions
1 parent 567cf43 commit ce242d5

File tree

3 files changed

+82
-35
lines changed

3 files changed

+82
-35
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Actions\Inventory;
6+
7+
use App\Models\Item;
8+
use App\Models\Team;
9+
use Illuminate\Http\UploadedFile;
10+
use Illuminate\Support\Arr;
11+
use Illuminate\Support\Str;
12+
13+
class CreateItem
14+
{
15+
/**
16+
* @param array<string, mixed> $data
17+
*/
18+
public function handle(Team $team, array $data): Item
19+
{
20+
$photo = Arr::pull($data, 'photo');
21+
22+
$item = $team->items()->create($data);
23+
24+
if ($photo instanceof UploadedFile) {
25+
$filename = Str::slug($item->name) . '-' . $item->id . '.' . $photo->getClientOriginalExtension();
26+
27+
$path = $photo->storeAs("teams/{$team->id}/items", $filename);
28+
29+
$item->update(['photo_path' => $path]);
30+
}
31+
32+
return $item;
33+
}
34+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Actions\Inventory;
6+
7+
use App\Models\Item;
8+
use Illuminate\Http\UploadedFile;
9+
use Illuminate\Support\Arr;
10+
use Illuminate\Support\Facades\Storage;
11+
use Illuminate\Support\Str;
12+
13+
class UpdateItem
14+
{
15+
/**
16+
* @param array<string, mixed> $data
17+
*/
18+
public function handle(Item $item, array $data, bool $removePhoto = false): Item
19+
{
20+
$photo = Arr::pull($data, 'photo');
21+
22+
$item->update($data);
23+
24+
if ($removePhoto && ! $photo instanceof UploadedFile) {
25+
if ($item->photo_path) {
26+
Storage::delete($item->photo_path);
27+
}
28+
29+
$item->update(['photo_path' => null]);
30+
} elseif ($photo instanceof UploadedFile) {
31+
if ($item->photo_path) {
32+
Storage::delete($item->photo_path);
33+
}
34+
35+
$filename = Str::slug($item->name) . '-' . $item->id . '.' . $photo->getClientOriginalExtension();
36+
37+
$path = $photo->storeAs("teams/{$item->team_id}/items", $filename);
38+
39+
$item->update(['photo_path' => $path]);
40+
}
41+
42+
return $item;
43+
}
44+
}

app/Livewire/Forms/Inventory/ItemForm.php

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace App\Livewire\Forms\Inventory;
66

7+
use App\Actions\Inventory\CreateItem;
8+
use App\Actions\Inventory\UpdateItem;
79
use App\Enums\ItemType;
810
use App\Models\Item;
9-
use Illuminate\Http\UploadedFile;
1011
use Illuminate\Support\Facades\Auth;
1112
use Illuminate\Support\Facades\Storage;
12-
use Illuminate\Support\Str;
1313
use Illuminate\Validation\Rule;
1414
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
1515
use Livewire\Form;
@@ -70,15 +70,10 @@ public function save(): void
7070
->mapWithKeys(fn (array $pair) => [$pair['key'] => $pair['value']])
7171
->all() ?: null;
7272

73-
$photo = $data['photo'] ?? null;
74-
unset($data['photo']);
75-
7673
if ($this->editingItem) {
77-
$this->editingItem->update($data);
78-
$this->handlePhotoUpdate($this->editingItem, $photo);
74+
(new UpdateItem)->handle($this->editingItem, $data, $this->removePhoto);
7975
} else {
80-
$item = Auth::user()->currentTeam->items()->create($data);
81-
$this->handlePhotoStore($item, $photo);
76+
(new CreateItem)->handle(Auth::user()->currentTeam, $data);
8277
}
8378

8479
$this->reset();
@@ -97,30 +92,4 @@ protected function rules(): array
9792
'photo' => ['nullable', 'image', 'max:5120'],
9893
];
9994
}
100-
101-
private function handlePhotoStore(Item $item, mixed $photo): void
102-
{
103-
if ($photo instanceof UploadedFile) {
104-
$filename = Str::slug($item->name) . '.' . $photo->getClientOriginalExtension();
105-
$path = $photo->storeAs("teams/{$item->team_id}/items", $filename);
106-
$item->update(['photo_path' => $path]);
107-
}
108-
}
109-
110-
private function handlePhotoUpdate(Item $item, mixed $photo): void
111-
{
112-
if ($this->removePhoto && ! $photo instanceof UploadedFile) {
113-
if ($item->photo_path) {
114-
Storage::delete($item->photo_path);
115-
}
116-
$item->update(['photo_path' => null]);
117-
} elseif ($photo instanceof UploadedFile) {
118-
if ($item->photo_path) {
119-
Storage::delete($item->photo_path);
120-
}
121-
$filename = Str::slug($item->name) . '.' . $photo->getClientOriginalExtension();
122-
$path = $photo->storeAs("teams/{$item->team_id}/items", $filename);
123-
$item->update(['photo_path' => $path]);
124-
}
125-
}
12695
}

0 commit comments

Comments
 (0)