/ app / Http / Livewire / Keys / Index.php
Index.php
 1  <?php
 2  
 3  namespace App\Http\Livewire\Keys;
 4  
 5  use App\Http\Services\GpgService;
 6  use App\Http\Services\KeyService;
 7  use Illuminate\Support\Facades\Storage;
 8  use Illuminate\Support\Str;
 9  use Livewire\Component;
10  use WireUi\Traits\Actions;
11  use Crypt_GPG_KeyNotFoundException;
12  
13  class Index extends Component
14  {
15      use Actions;
16  
17      public int $entityId;
18  
19      public string $entityType;
20  
21      protected function getListeners()
22      {
23          return ['refresh' => '$refresh'];
24      }
25  
26      public function getKeysProperty()
27      {
28          return KeyService::getKeys($this->entityId, $this->entityType);
29      }
30  
31      public function getActiveKeyProperty()
32      {
33          return $this->keys->first();
34      }
35  
36      public function downloadPublicGpgKey(GpgService $service)
37      {
38          try {
39              return Storage::download(
40                  $service->exportPublic($this->activeKey),
41                  $this->getKeyName('public')
42              );
43          } catch (Crypt_GPG_KeyNotFoundException $e) {
44              $this->notification()->error('Error', $e->getMessage());
45              logger()->error($e);
46          }
47      }
48  
49      public function downloadPrivateGpgKey(GpgService $service)
50      {
51          try {
52              return Storage::download(
53                  $service->exportPrivate($this->activeKey),
54                  $this->getKeyName('private')
55              );
56          } catch (Crypt_GPG_KeyNotFoundException $e) {
57              $this->notification()->error('Error', $e->getMessage());
58              logger()->error($e);
59          }
60      }
61  
62      private function getKeyName(string $suffix): string
63      {
64          $filename = implode('_', [$this->activeKey->key_name, $this->activeKey->key_email]);
65          $filename .= '_' . $suffix . '.asc';
66          return Str::snake($filename);
67      }
68  
69      public function render()
70      {
71          return view('livewire.keys.index');
72      }
73  }