/ app / Http / Services / ProtectionProfileService.php
ProtectionProfileService.php
 1  <?php
 2  
 3  namespace App\Http\Services;
 4  
 5  use App\Models\ProtectionProfile;
 6  
 7  class ProtectionProfileService
 8  {
 9      public static function search(string $searchString, string $sortField, string $sortDirection, array|null $filters, int $dossierId = null)
10      {
11          $protectionProfilesQuery = ProtectionProfile::query();
12  
13          $protectionProfilesQuery
14              ->when($searchString, function ($query, $searchString) {
15                  $query->where('name', 'like', '%' . $searchString . '%');
16              });
17  
18          if ($filters && $filters['created_at_since']) {
19              $protectionProfilesQuery->where('created_at', '>=', $filters['created_at_since']);
20          }
21  
22          if ($filters && $filters['created_at_to']) {
23              $protectionProfilesQuery->where('created_at', '<=', $filters['created_at_to']);
24          }
25  
26          if ($sortField && $sortDirection) {
27              $protectionProfilesQuery->orderBy($sortField, $sortDirection);
28          }
29  
30          return $protectionProfilesQuery;
31      }
32  
33      public static function find(int $protectionProfileId): ProtectionProfile
34      {
35          return ProtectionProfile::find($protectionProfileId);
36      }
37  
38      public static function create(array $data)
39      {
40          return ProtectionProfile::create($data);
41      }
42  
43      public static function update(int $id, array $data)
44      {
45          $protectionProfile = ProtectionProfile::findOrFail($id);
46  
47          return $protectionProfile->update($data);
48      }
49  
50      public static function remove(int $id)
51      {
52          $protectionProfile = ProtectionProfile::findOrFail($id);
53  
54          return $protectionProfile->delete();
55      }
56  }